CommandLabel or Command?

Discussion in 'Plugin Development' started by Sorroko, Mar 12, 2012.

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

    Sorroko

    I am curious to see which is best and what you think. Let me explain a default on command might look like this:
    Code:
    public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
      //command handling
    }
    If you use commandLabel to check the command used it doesn't seem to work with aliases in plugin.yml. Whereas if you use command.getName() and the compare it works fine. Which is best to use? Or both are the same. I should think that using command.getName() is better.
    - ryan7745
     
  2. Offline

    theguynextdoor

  3. Offline

    SgtStud

    I always just used commandLabel as I was lazy. I guess I will use the other one now :p

    Last thing, could you give an example of where it would make a difference? I'm curious.
     
  4. Offline

    BloodShura

    I always use if (commandLabel.equalsIgnoreCase("mycommand")) and it works fine. D:
     
  5. Offline

    Sorroko

    BloodShura SgtStud I suppose command.getName() is better then. It means you don't need to listen for each alias
     
  6. Offline

    BloodShura

    Well, I use commandlabel because sometimes I use various commands in only one, which I use if (l.startsWith("anti")) (l = commandlabel). Because I use Antiblock, anticreeper, antimove, antichat, antisheep, antienderman, antipick, antiphysics, to block somethings temporarily, and use this nine times using cmd.getName() would be boring.
     
  7. Offline

    nisovin

    But using commandLabel means that if someone tries to set up aliases for your commands the aliases won't work. Using command.getName() is the correct way to do this.

    Edit: And what in the world do you mean by it would be "boring"?
     
  8. Offline

    SgtStud

    Lol. I guess I will go and change this too.

    Last thing, what do you mean by aliases? For example if someone powertooled a command (I'm assuming you know what that it), would that command not work if the code used commandLabel?
     
  9. Offline

    Sorroko

    SgtStud you define aliases in your plugin.yml for example it might look like this:
    Code:
    commands:
        example:
            usage: /<command>
            description: An example command.
            aliases: [ex]
    using /example or /ex trigger the same command
     
  10. Offline

    nisovin

    It is also possible for a server admin to define aliases in the bukkit.yml file.
     
  11. Offline

    SgtStud

    Oh yea I remember those. Quick question. If you only add the alias to the plugin.yml file, but not to the code, would it still work?

    Example plugin.yml:

    commands:
    attack:
    usage: /<command>
    description: kill someone
    aliases: [kill]

    example code:

    Code:
    if (cmd.getName().equalsIgnoreCase("attack")){
    player.setHealth(0);}
    even though the alias kill is used in the yml, I never mentioned it in the code, as in an or statements. If I used /kill, would the plugin be smart enough to run the code?
     
  12. Offline

    Sorroko

    SgtStud that's what we where saying ;) if you use command.getName() then it will :p
     
    SgtStud likes this.
  13. Offline

    SgtStud

    Oh sweet that makes my life so much easier :p

    ryan7745 thanks for the help!
     
    ryan7745 likes this.
Thread Status:
Not open for further replies.

Share This Page