Command priority?

Discussion in 'Plugin Development' started by Streammz, Jul 26, 2011.

Thread Status:
Not open for further replies.
  1. Is there any way to make sure the command of my plugin is being handled instead of the same command in another plugin?

    I'm currently experiencing conflicts on an rather unused command, and my command is being used alot on the server.
    Yeah, I know i can change the command in the plugin.yml, but its rather more useful to at least know how to change the priority's
     
  2. Offline

    Uhehesh

    I have the same problem. My plugin over9000homes uses getCommands("...").setExecutor(), but priority of my handler is less than Essentials'.
    So how I can fix this problem or I must rewrite this with raw onCommand?
     
  3. You can't set priorities for commands, I think it depends on when the plugin loads (thus who registers the commands first).
    To force the command, you could use PLAYER_COMMAND_PREPROCESS-event, but that is a really bad way of doing it.
    So rather change the cmd-name (and use the old name as an alias), that is far easier and makes it more compatible.
     
  4. Offline

    bergerkiller

    Yup, I made a plugin called "area chatter" and it was run firstly. You can try to name your plugin starting with a (or a digit).
     
  5. Offline

    Uhehesh

    That sucks... :(
    Maybe idea with "mhome" command name and alias is the best now.

    P.S. I hope Bukkit Team will add priority features. [skeleton]
     
  6. Offline

    Tster

    I would use on command preprocess with highest priority
     
  7. Offline

    Uhehesh

    No.
    It's very bad way, read manuals. ;)

    Um... why doesn't Bukkit support aliases for commands?
    My problem is still alive (sry for my bad English :D), I don't want other commands like /msethome or /mhome, I want /sethome and /home, but I can't because Essentials aren't compatible.
    I believe that way must be.

    P.S. I hate plugins like Essentials (lol sry developer), that provide all the features but still many players use them. :(
    P.P.S. So maybe really use commands preprocess? Dunno...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 18, 2016
  8. Offline

    Dark_Balor

    I read the bukkit source code to understand how it deal with commands.
    For the aliases, first registered is the only registered.

    For command, if the same command is found (imagine Essentials with his /home is registered before your /home command), the command is renamed to PluginName:home.
    ONLY for command, there is no auto-rename for aliases.

    About Essentials, if he detect that a another plugin have registered the same command has it, it fallback to the plugin. Meaning, if you /home is a command, Essentials will take the "lead" see that your plugin have that command and launch YOUR command instead of its.

    You can easily check if your command is registered and have the priority :
    Code:java
    1.  
    2. plugin.getServer().getPluginCommand("CommandName").equals(plugin.getCommand("CommandName")) ;
    3.  


    You can't register or unregister command (too bad).
     
    Batman500 and Uhehesh like this.
  9. Offline

    Uhehesh

    No, it doesn't work with Essentials... It still uses Essentials' default /home.
     
  10. Offline

    Dark_Balor

    Strange because in the Essentials Source Code you can see that :
    Code:java
    1.  
    2. if (!getSettings().isCommandOverridden(command.getName()) && !commandLabel.startsWith("e"))
    3. {
    4. for (Plugin p : getServer().getPluginManager().getPlugins())
    5. {
    6. if (p.getDescription().getMain().contains("com.earth2me.essentials"))
    7. {
    8. continue;
    9. }
    10.  
    11. PluginDescriptionFile desc = p.getDescription();
    12. if (desc == null)
    13. {
    14. continue;
    15. }
    16.  
    17. if (desc.getName() == null)
    18. {
    19. continue;
    20. }
    21.  
    22. PluginCommand pc = getServer().getPluginCommand(desc.getName() + ":" + commandLabel);
    23. if (pc != null)
    24. {
    25. return pc.execute(sender, commandLabel, args);
    26. }
    27. }
    28. }
    29.  

    Meaning that if you command as not be configured in Essentials to be only used with it, it's falling back to your plugin. Only if it was the same command (and not an alias)
     
    Batman500 likes this.
  11. Offline

    Uhehesh

    I don't know... Here is my plugin.yml:
    Code:
    name: over9000homes
    main: me.uhehesh.over9000homes.over9000homes
    version: 1.13
    commands:
       sethome:
          description: Set home.
          usage: /sethome (home) (player)
       home:
          description: Go home.
          usage: /home (home) (player)
       deletehome:
          description: Delete home.
          usage: /deletehome (home) (player)
       over9000homes:
          description: hehe.
          usage: /over9000homes
       transferhomes:
          description: transfer all homes, MultipleHomes to over9000homes.
          usage: /transferhomes <path>
       homelist:
          description: list all homes.
          usage: /homelist (page) (player)
    And I use getCommand(...).setExecutor(...).
     
  12. You could require instead on just / make them put // before a command like in world edit.
     
  13. Offline

    Uhehesh

    No, thanks. There must be better way.
     
  14. Aliases
     
  15. Offline

    Uhehesh

    But why Essentials doesn't do what it has to: use other plugin if found?
     
  16. What do you mean?
     
  17. Offline

    Uhehesh

    Essentials must use other plugin if analogue for command is found.
     
  18. Offline

    bassfader

    I have successfully overwritten the /afk command of Essentials in my SimpleAFK Plugin. I just had to make sure that I have an additional onCommand() method in my main class (in addition to my CommandHandler class).

    So I am registering commands in my onEnable() method (as you can see assigned to the "sAfkCommandHandler" class which does the actual command handling):
    Code:
    public void onEnable()
    {
        ...
        getCommand("afk").setExecutor(new sAfkCommandHandler(this));
        getCommand("isafk").setExecutor(new sAfkCommandHandler(this));
        getCommand("afklist").setExecutor(new sAfkCommandHandler(this));
        ...
    }
    Then I just needed another onCommand method in my Main Class which "reregisters" and executes a command. That way it runs my command instead of the Essentials one:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[])
    {
        if(cmd.getName().equalsIgnoreCase("afk") || cmd.getName().equalsIgnoreCase("isafk") || cmd.getName().equalsIgnoreCase("afklist"))
        {
            try
            {
                PluginCommand command = getCommand(cmd.getName().toLowerCase());
                command.setExecutor(new sAfkCommandHandler(this));
                command.execute(sender, commandLabel, args);
            }
            catch(CommandException e)
            {
                log.warning(e.getMessage());
            }
            return true;
        } else
        {
            return false;
        }
    }
    EDIT: I know I have to optimize my code a bit so it doesn't create new CommandHandler objects all the time... Thats what I am (amongst others) currently working on :)
     
    Uhehesh likes this.
  19. Offline

    Dark_Balor

    @bassfader
    I have an other idea that could works :
    Add a softdepend that don't exists (like BLAH) to your plugin, to be sure it will be the last loaded.

    do
    Code:java
    1. PluginCommand plgCmd = plugin.getServer().getPluginCommand("commandYouWantToOverride");
    2. if(plgCmd.getPlugin().getDescription().getName().equal("Essentials"))
    3. plgCmd.setExecutor(CommandExecutorClass);


    and then every time the Essentials command is used, it will automatically use your command instead. Because the only check done before executing the command, is to check if the owning plugin is enable.

    @Adamki11s : aliases is not a real solution, command have the priority on alias.
     
    Batman500 and Uhehesh like this.
  20. Offline

    Uhehesh

    Thanks for answers!
    It seems that my plugin isn't conflicting with Essentials. :oops:
    I'll try this later on other server later.
     
Thread Status:
Not open for further replies.

Share This Page