onCommand is just really on my nerves

Discussion in 'Plugin Development' started by Geotheo70, Sep 16, 2012.

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

    Geotheo70

    onCommand will not execute my command! Upon using the command it can't find it! This is a little snippet of my code:

    Code:
    package net.sourceforge.p.bukkitweartear;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.entity.HumanEntity;
    import org.bukkit.event.EventHandler;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.command.*;
     
    public class BukkitWearTear extends JavaPlugin {
        public boolean leatherwear = false;
        public boolean ironwear = false;
        public boolean goldwear = false;
        public boolean chainmailwear = false;
        public boolean diamondwear = false;
        private HumanEntity player;
        private Plugin weartear;
       
        public void onEnable() {
            getLogger().info("Wear and Tear is now on!");
        }
       
        public void onDisable() {
            getLogger().info("Wear and Tear is now off!");
        }
        @EventHandler
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(player.hasPermission("weartear.leatherwearon")) {
                if(cmd.getName().equalsIgnoreCase("leatherwear on")){
                            Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "Leather wear is now on!");
                            leatherwear = true;
                            return true;
                        }
                        return false;
                        }
                        return false;
                        }
       
        public boolean onCommand1(CommandSender sender, Command cmd, String label, String[] args){
            if(player.hasPermission("weartear.leatherwearoff")) {
                if(cmd.getName().equalsIgnoreCase("leatherwear off")){
                            Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "Leather wear is now off!");
                            leatherwear = false;
                            return true;
                        }
                        }
                        return false;
                        }
    And here is my plugin.yml BTW just forget the other things besides the command and its permissions:

    Code:
    name: Wear and Tear
    main: net.sourceforge.p.bukkitweartear.BukkitWearTear
    version: 0.5
    permissions:
        weartear.*:
            description: Gives access to all Wear and Tear commands
            children:
                weartear.leatherwearon: true
                weartear.leatherwearoff: true
                weartear.ironwearon: true
                weartear.ironwearoff: true
                weartear.goldwearon: true
                weartear.goldwearoff: true
                weartear.chainmailwearon: true
                weartear.chainmailwearoff: true
                weartear.diamondwearon: true
                weartear.diamondwearoff: true
        weartear.leatherwearon:
            description: Allows you to enable leather armor deterioration
            default: op
        weartear.leatherwearoff:
            description: Allows you to disable leather armor deterioration
            default: op
        weartear.ironwearon:
            description: Allows you to enable iron armor deterioration
            default: op
        weartear.ironwearoff:
            description: Allows you to disable iron armor deterioration
            default: op
        weartear.goldwearon:
            description: Allows you to enable gold armor deterioration
            default: op
        weartear.goldwearoff:
            description: Allows you to disable gold armor deterioration
            default: op
        weartear.chainmailwearon:
            description: Allows you to enable chainmail armor deterioration
            default: op
        weartear.chainmailwearoff:
            description: Allows you to disable chainmail armor deterioration
            default: op
        weartear.diamondwearon:
            description: Allows you to enable diamond armor deterioration
            default: op
        weartear.diamondwearoff:
            description: Allows you to disable diamond armor deterioration
            default: op
    commands:
        leatherwear on:
            description: Allows you to enable leather armor deterioration
            usage: /leatherwear on
            permission: weartear.leatherwearon
            permission-message: You don't have the permission to do that
        leatherwear off:
            description: Allows you to disable leather armor deterioration
            usage: /leatherwear off
            permission: weartear.leatherwearoff
            permission-message: You don't have the permission to do that
        ironwear on:
            description: Allows you to enable iron armor deterioration
            usage: /ironwear on
            permission: weartear.ironwearon
            permission-message: You don't have the permission to do that
        ironwear off:
            description: Allows you to disable iron armor deterioration
            usage: /ironwear off
            permission: weartear.ironwearoff
            permission-message: You don't have the permission to do that
        goldwear on:
            description: Allows you to enable gold armor deterioration
            usage: /goldwear on
            permission: weartear.goldwearon
            permission-message: You don't have the permission to do that
        goldwear off:
            description: Allows you to disable gold armor deterioration
            usage: /goldwear off
            permission: weartear.goldwearoff
            permission-message: You don't have the permission to do that
        chainmailwear on:
            description: Allows you to enable chainmail armor deterioration
            usage: /chainmailwear on
            permission: weartear.chainmailwearon
            permission-message: You don't have the permission to do that
        chainmailwear off:
            description: Allows you to disable chainmail armor deterioration
            usage: /chainmailwear off
            permission: weartear.chainmailwearoff
            permission-message: You don't have the permission to do that
        diamondwear on:
            description: Allows you to enable diamond armor deterioration
            usage: /diamondwear on
            permission: weartear.diamondwearon
            permission-message: You don't have the permission to do that
        diamondwear off:
            description: Allows you to disable diamond armor deterioration
            usage: /diamondwear off
            permission: weartear.diamondwearoff
            permission-message: You don't have the permission to do that
    It is all parsed correctly so it cant be that. Does anybody know what I did wrong here?
     
  2. Offline

    Sabersamus

    1.) You didn't register them.
    2.) onCommand1(...) Probably wont work
     
  3. Offline

    kennylax12

  4. Offline

    KeybordPiano459

    Do it like this:
    Code:java
    1. onCommand(...) {
    2. if (cmd.getName().equalsIgnoreCase("leatherwearon") {
    3. // do stuff
    4. } else if (cmd.getName().equalsIgnoreCase("leatherwearoff") {
    5. // do other stuff
    6. }
    7. return false;
    8. }
    If you want it to be 'leatherwear on' and 'leatherwear off' you have to use args, I'll get into that if you want me to.
     
    zack6849 likes this.
  5. Offline

    Sabersamus

  6. Offline

    kennylax12

    I have never ran into a single conflict with the PlayerCommandPreprocessEvent and another plugin, but I do understand your position.
     
  7. Offline

    Geotheo70

    Thanks t0 all of you guys! I think I will go with KeybordPiano's way due to the nature of my plugin(there are many commands and I can use this to store all of em in one public void! TY
     
  8. Offline

    KeybordPiano459

    It's easier to do it all with the onCommand.
     
  9. Offline

    kennylax12

    I think it is just personal preference.
     
  10. Offline

    theguynextdoor

    No, no, and one last no.

    1 good reason not to: You can't do commands in the console that way
    Another reason is that it is disorganised. It is much easier to maintain a class for each command, than doing it in a listener method.

    For doing commands, the best way is to do a separate command executor for each commands. But, you should know that "leatherwork on" would not be a valid command name for a command. What you would need to do is make a command called 'leatherwork' and then use the args[] part of the command to catch subcommands. Let me show you.


    To register a new class for each command, you would do:

    Code:
    public void onEnable() {
        getCommand("leatherwork").setExecutor(new LeatherworkCommand(), this);
    }
    And then you would have a new class, called LeatherworkCommand in this example, and this class would implement CommandExecutor

    So the base of the command would look like

    Code:
    public class LeatherworkCommand implements CommandExecutor {
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (args[0].equalsIgnoreCase("on") {
            // on subcommand code goes here
            }
            else if (args[0].equalsIgnoreCase("off") {
            // off command code goes here
            }
        }
     
    }
    So then you would simply

    Code:
    name: PluginName
    main: package.path
    version: 9001
    commands:
        leatherwork:
            description: leatherwork command
            usage: /<command>

    You don't need to register each sub command


    Or if you do have lots of sub commands, then you could do what i do in my plugin:
    https://github.com/theguynextdoor/T...ity/tribesnextdoor/commands/TribeCommand.java
     
    Zidkon likes this.
  11. Offline

    kennylax12

    You are right. PlayerCommandPreprocess does not allow console commands. Also it can get disorganized, I know this from using it for a while. I believe a lot of the times it is the simple way out which may or may not be a good thing.
     
  12. Offline

    Sabersamus

    This is directly from the java docs.

    Detailed Description

    Called early in the command handling process.
    This event is only for very exceptional cases and you should not normally use it.
     
  13. Offline

    kennylax12

    Wow, you are absolutely right. I should have looked more into it rather than simply following what my friend did back when I was learning. I definitely will be needing to switch my plugins over to onCommand. This post wasn't originally intended to help me, but I do thank you for your help!
     
Thread Status:
Not open for further replies.

Share This Page