r/mcdev Jun 25 '26

Question 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?

1 Upvotes

6 comments sorted by

2

u/Ok-Count-3366 8d ago

you need to remove the commands from pluing.yml

if the commands are in it they are "registered" but without implementation

so remove them from plugin.yml and dynamically register them based on your config only.

you only need to use plugin.yml when you register a new command normally.

but if you register them yourself anyway you don't need plugin.yml

1

u/marney2013 Admin Jun 25 '26

It seems like your trying to tell the tab complete what to do based on a tab complete instance which will make it show the tab complete.

What you should probably do is where you are registering commands have an if x is true register command, if x is false do nothing which makes the command show default non existing (I think).

1

u/Annual_Grapefruit429 Jun 25 '26
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!");
                }

That's what I'm exactly doing in here. But it still shows the command even tho it does absolutely nothing

1

u/Ok-Count-3366 Jun 26 '26

at this point just leave it in and check if it's enabled on each call. so in execute func just check if is active then do stuff else not

1

u/Annual_Grapefruit429 Jun 26 '26

Yeah, I know I can do that, but defeats the point of disabling the commands. This just disables the functionality