Solved Checking if command is registered in my plugin

Discussion in 'Plugin Development' started by torpkev, Nov 13, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    torpkev

    I have a plugin with multiple commands, and they run depending on if a value in the configuration file is set or not.

    When a reload command is sent for my plugin, I'd like to reload the configuration and then either register or unregister the commands as needed.

    For example, I have the following in my Main.java file:

    getCommand("ipcheck").setExecutor(new IPCheck());

    This runs depending on if an item in my config is set to true or not.

    getCommand returns void, so not sure how to track this, or how to unregister (if that is even possible without a full /reload)

    Thanks
     
  2. Offline

    timtower Administrator Administrator Moderator

    @torpkev getCommand returns a Command. Not void
     
  3. Offline

    torpkev

    If I do

    import org.bukkit.command.Command;
    ...
    Command c = getCommand("ipcheck").setExecutor(new IPCheck());

    The code shows an error, when I hover over it, it says:

    Type mismatch: cannot convert from void to Command
     
  4. Offline

    timtower Administrator Administrator Moderator

    @torpkev That is due to the setExecutor part, not due to getCommand.
     
  5. Offline

    torpkev

    Fair point, thanks.

    So with getCommand().setExecutor returning void.. how can I track if they're registered?
     
  6. Offline

    The_Spaceman

    you could check when the command is run of the command is enabled...

    like:
    Code:
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!commandIsEnabled("thisCommand")) {
            sender.sendMessage("this command does not exist" or "this command is disabled");
            return;
        }
        //your command
    }
    make something like this, because as far as I know the command is enabled when its in the plugin.yml. When the executor is not set your Main.class is the command executor
    the default executor in the JavaPlugin.class:
    Code:
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        return false;
    }
     
  7. Offline

    torpkev

    Ah so basically always allow the command to be called and then just return them out if the command should be deactivated?

    Probably a much simpler approach.

    Thanks
     
  8. Offline

    The_Spaceman

    'Probably a much simpler approach.', I don't think so... and this way you can give a reason why this command doesn't work anymore (for players)

    and np
     
  9. Offline

    torpkev

    Sorry.. "this" is a much simpler approach (than what I was thinking of)

    appreciate it!
     
    The_Spaceman likes this.
Thread Status:
Not open for further replies.

Share This Page