CommandExecutor Not Working

Discussion in 'Plugin Development' started by KaiBB, May 9, 2012.

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

    KaiBB

    So, for some reason, commands won't work in my Core plugin... I put them in a CommandExecutor, and I included them in the plugin.yml... No errors, so I can't show you those.

    CommandExecutor:
    Code:
    package kai.core;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
     
    public class Commands implements CommandExecutor {
     
        @Override
        public boolean onCommand(CommandSender arg0, Command arg1, String arg2,
                String[] arg3) {
            Player p = (Player) arg0;
            if(arg1.equals("spawn")){
                p.teleport(p.getWorld().getSpawnLocation());
            }
            if(arg1.equals("conjuration")){
                p.sendMessage(ChatColor.GOLD + "Type /conjure itemname to conjure that item.");
                p.sendMessage(ChatColor.GOLD + "These items can be conjured:");
                p.sendMessage(ChatColor.GOLD + "Sword");
                p.sendMessage(ChatColor.GOLD + "Bow");
            }
            if(arg1.equals("conjure")){
                if(arg2.equalsIgnoreCase("sword")){
                    ItemStack swordstack = new ItemStack(Material.DIAMOND_SWORD, 1);
                    p.getInventory().addItem(swordstack);
                }
                if(arg2.equalsIgnoreCase("bow")){
                    ItemStack bowstack = new ItemStack(Material.BOW, 1);
                    p.getInventory().addItem(bowstack);
                }
                if(!arg2.equalsIgnoreCase("sword") && !arg2.equalsIgnoreCase("bow")){
                    p.sendMessage(ChatColor.RED + "Invalid item! Type /conjuration to see what can be conjured!");
                }
            }
            return false;
        }
     
    }
    
    Plugin YAML:
    Code:
    name: KaiBB Core
    version: Release
    description: KaiBB's Sample Core
    author: KaiBB
     
    main: kai.core.Core
     
    commands:
        spawn:
            description: Return to the spawn!
        conjuration:
            description: Conjure help.
        conjure:
            description: Conjure an item!
     
  2. Offline

    Kanlaki101

    Are you making sure to register the command?

    Like in your onEnable() method:
    getCommand("command").setExecutor(new COMMANDCLASS(this));
     
    KaiBB likes this.
  3. Offline

    KaiBB

    Bump? I need this done pretty soon, I need to show it off to this server as soon as the Dev interviewing me gets on his computer! :'(

    Perfect timing. I reward you with both a Like, and an E-Hug. *hug* :3

    Kanlaki101
    I added that for each command, under the onEnable... Didn't make a difference T^T

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

    Blairjam

    Post the onEnable() then? It's the only thing you haven't shown.
     
  5. Offline

    KaiBB

    Code:
    public void onEnable() {
            log.info("Core loaded.");
            getServer().getPluginManager().registerEvents(new  ServerListener(), this);
            getCommand("spawn").setExecutor(new Commands());
            getCommand("conjuration").setExecutor(new Commands());
            getCommand("conjure").setExecutor(new Commands());
     
        }
    Blairjam

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

    Blairjam

    getCommand("spawn").setExecutor(new Commands(this));

    You need to add "this" in every getCommand().
     
  7. Offline

    KaiBB

    Hm. Weird. I had that at first, and it gave me errors. Now it's working fine. I probably had a typo. Thanks!
     
  8. Offline

    Blairjam

    lol, no problem!
     
  9. Offline

    KaiBB

    speak of the devil. Errors came back! I just added a constructor to "Commands", that should fix it. I'm checking it now!

    Ah. Adding a constructor did nothing. Here's the error:
    The constructor Commands(Core) is undefined.
    ((Core is my main class))

    Blairjam

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

    Blairjam

    Code:
    public Commands(MainClass instance){
            plugin = instance;
        }
    :O I probably should have looked for that...this might help though!
     
  11. Offline

    KaiBB

    This didn't work either :(
     
  12. Offline

    Darq

    He does not NEED to pass an instance of any class, unless said class has non-static methods that the Command class needs access to.
     
  13. Offline

    Blairjam

    Code:
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        if(label.equalsIgnoreCase("conjure")){
            if(args.length == 0){
                sender.sendMessage("Too few arguments"); //or whatever code
            }else if(args.length == 1){
                if(args[0].equalsIgnoreCase("sword")){
                    //give player sword
                }else if(args[0].equalsIgnoreCase("something")){
                    //give player something else
                }else{
                    //yeah, you should get it
                }
            }else{
                //too many args...
            }
        }else if(label.equalsIgnoreCase("otherCommand")){
            //stuff
        }
    }
    Oh, and after looking more at your Commands class, the conjure command won't work as I'm assuming it is supposed to. /conjure sword gives the player a sword? If so, you need to check if arg3[0] has something stored in it, and then check if it is equal to whatever item you want to give.

    What would you suggest he do?

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

    KaiBB

    I shouldn't need to check arg3... I've done this exact same command in the past. I even put in on a timer to take it away after 60 seconds. And I never typed arg3. ;_;
     
  15. Offline

    Darq

    I'm 100% certain you don't need to pass an instance of the main class, the need for doing so is dependant on your own code. setExecutor() only needs an instance of the CommandExeuctor class.

    The issue is he's doing .equals() to compare a string to a Command class, when he should be doing arg1.getName().equals("Whatever");

    KaiBB ^
     
  16. Offline

    KaiBB

    Darq
    Thank you, I will try this out. Btw, you don't need to tag me. I watch every thread I post in. :3
     
  17. Offline

    Darq

    Alrighty, and remember that .equals() is case sensitive on a String.
     
  18. Offline

    KaiBB

    Why does label have to be the same as cmd ?_?
     
  19. Offline

    Darq

    Afaik; Label is always the same as Command.getName(), Label is officially the alias of the command used.

    Don't quote me on that. Anywho, did using args1.getName() work out?
     
    KaiBB likes this.
  20. Offline

    KaiBB

    Yes, I just need to fix /conjure :3

    Darq
    Only thing left: If I type /conjure sign instead of bow or sword, I don't get the error message.
    Code:
    if(arg1.getName().equals("conjure")){
                if(arg3[0].equalsIgnoreCase("sword")){
                    ItemStack swordstack = new ItemStack(Material.DIAMOND_SWORD, 1);
                    p.getInventory().addItem(swordstack);
                }
                if(arg3[0].equalsIgnoreCase("bow")){
                    ItemStack bowstack = new ItemStack(Material.BOW, 1);
                    p.getInventory().addItem(bowstack);
                }
                if(!arg3[0].equalsIgnoreCase("sword") && !arg3[0].equalsIgnoreCase("bow")){
                    p.sendMessage(ChatColor.RED + "Invalid item! Type /conjuration to see what can be conjured!");
                }
    Darq
    Nevermind!

    Darq
    If arg3[0] == null, it gives me a large list of errors. I tried adding an if statement for if it returns null, it sends a message to the player, but it just says an internal error has occurred.

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

    Darq

    if (arg3.length < 1).
    Edit: Didn't realize you were checking an invididual arg. Use if (arg3[0] == ""), or if (arg3[0].isEmpty())
    Edit again: Realized you were probably doing that before to see if they'd entered an argument, if that's the case, use the bit I crossed out.

    And for stack traces, I suggest you read this: http://forums.bukkit.org/threads/ho...ubleshoot-your-own-plugins-by-yourself.32457/

    I haven't looked at it myself, but it's stickied, must not be too bad. It'll help you debug your plugin a lot, if you know how to read a stack trace.
     
  22. Offline

    Sabersamus


    The thing with using Label is you can add aliases, in bukkit.yml, so the label can change, but using Command.getName() the name only changes if you change it, but using getName() finds all aliases for the command as well.
     
    KaiBB likes this.
  23. Offline

    KaiBB

    Thanks, I'll try isEmpty. I was doing == null.
     
Thread Status:
Not open for further replies.

Share This Page