u/Annual_Grapefruit429

▲ 1 r/mcdev

How do I disable commands based on a config file?

I have a plugin, and I want its commands to be modular via a config.yml.

This is how I register commands:

    public class PluginUtils {
    
        public static void registerCommand(String command, CommandExecutor executor, InventoryCommands plugin) {
            if (plugin.getConfig().getBoolean("commands." + command, true)) {
                var cmd = plugin.getCommand(command);
                if (cmd != null) {
                    cmd.setExecutor(executor);
                    if (executor instanceof TabCompleter tc) {
                        cmd.setTabCompleter(tc);
                    }
                } else {
                    plugin.getLogger().warning("Command '" + command + "' specified on config.yml does not exist!");
                }
    
            } else {
                var commandMap = plugin.getServer().getCommandMap();
                var cmd = commandMap.getCommand(command);
                if (cmd != null) {
                    cmd.unregister(commandMap);
                    plugin.getLogger().info("Unregistered command '" + command + "'");
                }
            }
        }
        
    }

This is the default config.yml:

    commands: # Here you can disable commands that you don't want to use
      craftingtable: true
      enderchest: true
      invsee: true
      trash: true
      anvil: true

When I disable a command by doing command: false, it doesn't do anything when executed, but it's still shown as a valid command. I mean, it shows up on TAB completions and it, instead of being red, it's the default white.

What can I do?

reddit.com
u/Annual_Grapefruit429 — 11 days ago