Simple command not working

Discussion in 'Spigot Plugin Development' started by 1337Caleb, Jul 14, 2015.

  1. I have a simple command here

    Code (Text):

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
              Player player = (Player)sender;
               if(cmd.getName().equalsIgnoreCase("bounty")) {
                   if(sender instanceof Player) {
                           if(args.length == 0) {
                               //what to do if they dont type a username or anything after the "duel"
                           } else if (args.length == 2) {
                               Player target = Bukkit.getServer().getPlayer(args[0]);
                               sender.sendMessage("You have put a bounty on" + target + "Subtracting 500 money");
                               EconomyResponse resp = econ.withdrawPlayer(player, 500);
                           
                           }
                   } else {
         
               
        }
               }
                return false;
               }
     
    but when I try to do /bounty It says it does not exist.

    Plugin.yml
    Code (Text):

    name: Bounty
    version: 0.1
    main: caleb.bounty.main.Main
    commands:
       bounty:
          description: This is a demo command.
          usage: /<command> [player]
          permission: <plugin name>.basic
          permission-message: You don't have <permission>
     
     
    #1 1337Caleb, Jul 14, 2015
    Last edited: Jul 14, 2015
  2. Use pastebin or code tags, indentation is important here.
     
  3. Added code tags
     
  4. Do you have @Override above your onCommand method?
     
    • Funny Funny x 1
  5. Where is your onCommand located? What's the input, as well as the output?

    Irrelevant, @Override is an optional annotation for syntax purposes.
     
  6. can you post all the code
     
  7. Make sure your class extends CommandExecutor.
     
  8. No if this is the JavaPlugin class it isn't required
     
  9. is this your exact actual plugin.yml ?
     
  10. First put the player initialization after you check if the sender is a player, because if you use the command from console it return an error: it can't use the console as a player.
    Then use
    Code (Text):
    if(args.length != 2)
    instead of
    Code (Text):
     if (args.length==0)
    so you can check if the player type more of 2 arguments.

    And, of course, he says that the command doesn't exists! You don't return true! The command works as well if you return false, but it return an error! This is an error that i often do, always check if you return true. Hope i've helped you and sorry for my bad english
     
  11. JustThiemo

    JustThiemo Previously TrinityDEV

    You check if length of arguments is 2, but you need to check if its 1, since you use /bounty player

    Also create player variable after you checked if sender is a player

    And last thing, check if player you want to put a bounty on with if(target == null). Would be useless to put bounty on a non existing player
     
  12. You have to register the command in your onEnable if the command is in a different class.
     
  13. JustThiemo

    JustThiemo Previously TrinityDEV

    Why would you check cmd.getName() if he made a commandexecutor.
     
    • Agree Agree x 1
  14. While this is correct, he didn't post if the command is actually in his main class.
     
  15. I'm so confused by this, The command would "register" if the plugin.yml was correct. Please post errors?