Command Question, Probably Obvious :P

Discussion in 'Plugin Development' started by Greyson, Dec 10, 2011.

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

    Greyson

    Hi,
    So I'm working on a test plugin in which when you type the command /redeem it gives you 5 diamonds. This is just a testing plugin and I thought I did everything right but whenever I type the command in game it just prints /redeem in the chat :/ What did I do wrong?

    Code:
    public class CouponsCommandExecutor implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command command,
                String commandLayout, String[] args) {
            if(commandLayout.equalsIgnoreCase("redeem")){
            Player p = (Player)sender;
            p.getInventory().addItem(new ItemStack(Material.DIAMOND, 5));
            return true;
        }
    
            return false;
        }
    
    }
    
     
  2. Offline

    Kierrow

    Well, first of all, please format your code better in the future.

    I'm guessing your problem is that you have,
    in your command declaration in the "plugin.yml" file,
    put "/redeem" as "usage". This causes, whenever
    the onCommand method returns false, Bukkit to display /redeem to
    the player.

    The reason why it returns false is because, normally,
    you shouldn't use "commandLayout" for your if-clause.
    I'd recommend using "command.getName()", so it looks like this:

    Code:
    public class CouponsCommandExecutor implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command command, String commandLayout, String[] args) {
            if(command.getName().equalsIgnoreCase("redeem")) {
                Player p = (Player)sender;
                p.getInventory().addItem(new ItemStack(Material.DIAMOND, 5));
                return true;
            }
            return false;
        }
    
    }
    I haven't tested it, but it should work fine.
     
  3. Offline

    Greyson

    I have the plugin.yml correct, so that's not it. And I tried adjusting it to look like your code but that's not it either :/
    And sorry about the messy code :p lol
     
  4. Offline

    Kierrow

    I didn't say your plugin.yml was incorrect.
    That's probably just the reason why it displays "/redeem" to the player...

    Try completely leaving out the if clause and have it always return true.
    Like this:

    Code:
    public class CouponsCommandExecutor implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command command, String commandLayout, String[] args) {
            Player p = (Player)sender;
            p.getInventory().addItem(new ItemStack(Material.DIAMOND, 5));
            return true;
        }
    
    }
    Of course, this is not what it should look like in the end for clean coding,
    but it should do what you want and is perfectly acceptable if you only use that method
    for one command AND you only run the command in-game.

    Please try copying the code and test it out.
     
  5. Offline

    Greyson

    I'm terribly sorry but it seems I've wasted your time :p
    It was acting up because I had
    getCommand("redeem").setExecutor(new CouponsCommandExecutor());
    under onDisable not onEnable :/
    Sorry.
    But while you're here, how might I use multiple commands under the same method?

    Edit: NVM GOT THAT TOO :p lol Thanks anyways ^_^
     
Thread Status:
Not open for further replies.

Share This Page