What is wrong with my onCommand?

Discussion in 'Plugin Development' started by Dexeron, Mar 3, 2012.

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

    Dexeron

    Here is the code:

    Code:
    public class WolvesCommandExecutor implements CommandExecutor {
        public boolean onCommand (CommandSender sender, Command command, String commandLabel, String[] args) {
            Player player = null;
            if (sender instanceof Player) {
                player = (Player) sender;
            }
            if(commandLabel.equalsIgnoreCase("wolves")) {
                if(sender.hasPermission("werevolves.basic") || (sender.hasPermission("werevolves.admin"))) {
                    sender.sendMessage(ChatColor.YELLOW + "----------" + ChatColor.RED + "=[Werewolves]=" + ChatColor.YELLOW + "----------");
                    sender.sendMessage(ChatColor.GOLD + "/wolves - " + ChatColor.AQUA + "Displays this help page");
                    sender.sendMessage(ChatColor.GOLD + "/wolves infect <playername> - " + ChatColor.AQUA + "Infect a Player by Command");
                    sender.sendMessage(ChatColor.GOLD + "/wolves c,cure <playername> - " + ChatColor.AQUA + "Cure a Player by Command");
                    sender.sendMessage(ChatColor.GOLD + "/wolves i,infect <playername> - " + ChatColor.AQUA + "Infect a Player by Command");
                    sender.sendMessage(ChatColor.GOLD + "/wolves w,who <playername> - " + ChatColor.AQUA + "Check if a player is a Werewolf");
                    sender.sendMessage(ChatColor.GOLD + "/wolves r,reload - " + ChatColor.AQUA + "Reload the Plugin");
                }
                else {
                    sender.sendMessage(ChatColor.RED + "You do not have access to that Command");
                }
                if(sender.hasPermission("werewolves.admin")) {
                    if(args[0] == "i" || args[0] == "infect") {
                        sender.sendMessage("Not Available yet.");
                    }
                }
                else {
                    sender.sendMessage(ChatColor.RED + "You do not have access to that Command");
                }
                if(args[0] == "beta") {
                    boolean exists = (new File(sender + ".yml")).exists();
                    if (exists) {
                        sender.sendMessage("You have an existing Config File. The Plugin is Working.");
                    }
               
                }
            }
            return false;
        }
    }
    
    When I type /wolves or even /wolves beta it doesn't want to work. Here is the Console Error:
    Code:
    18:55:21 [INFO] ----------=[Werewolves]=----------
    18:55:21 [INFO] /wolves - Displays this help page
    18:55:21 [INFO] /wolves infect <playername> - Infect a Player by Command
    18:55:21 [INFO] /wolves c,cure <playername> - Cure a Player by Command
    18:55:21 [INFO] /wolves i,infect <playername> - Infect a Player by Command
    18:55:21 [INFO] /wolves w,who <playername> - Check if a player is a Werewolf
    18:55:21 [INFO] /wolves r,reload - Reload the Plugin
    18:55:21 [WARNING] Unexpected exception while parsing console command
    org.bukkit.command.CommandException: Unhandled exception executing command 'wolv
    es' in plugin Werewolves v0.1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:16
    8)
            at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:3
    86)
            at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:3
    82)
            at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:574)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:551)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:435)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
            at me.dexeron.werewolves.WolvesCommandExecutor.onCommand(WolvesCommandEx
    ecutor.java:31)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
            ... 7 more
    >

    Thanks for the help!
     
  2. Check the array length before using it's indexes.
     
  3. Offline

    messageofdeath

    add @Override on the top of oncommand
     
  4. check its command instead of commandlabel
     
  5. Offline

    Dexeron

    Digi Thanks, but I don't know how to do that.

    Can you explain or give me a snippet of code? Ty
     
  6. Offline

    Lolmewn

    why?

    EDIT: It's args.length.
    If it's 0, there are no arguments.
    If it's 1, it has one (get it by doing args[0])
    If it's 2, it has two (get it by doing args[1]) etc.
     
  7. Offline

    Dexeron

    Lolmewn I used args[0] do I do like:

    Code:
    if args.length == 1 && args[0] == "Whatever" {
     
    }
     
  8. Offline

    theguynextdoor

    Also i see
    if(args[0] == "i" || args[0] == "infect") {
    sender.sendMessage("Not Available yet.");
    }

    When comparing strings, use
    .equalsIgnoreCase()

    So if(args[0].equalsIgnoreCase("i") || args[0].equalsIgnoreCase("infect"){

    This most probably wont fix you problem, but im pretty sure using == when comparing strings doesn't work.
     
  9. Offline

    Dexeron

    How does args.length work then? Is it like what I said ^^

    Ok I have this now:
    Code:
                if(sender.hasPermission("werewolves.admin")) {
                    if(args[0].equalsIgnoreCase("i")|| args[0].equalsIgnoreCase("infect") && args.length == 1) {
                        sender.sendMessage("Not Available yet.");
                    }
                }
    But that doesn't solve my problem. Can someone help me?

    Can someone maybe Explain to me what the problem is, I don't quite understand errors yet.

    What does the log mean? I tried the args.length thing, Did I do that correctly?

    theguynextdoor Digi

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

    JagdCoder

    Instead of if(commandLabel.equalsIgnoreCase("wolves")) { use if(command.getName().equalsIgnoreCase("wolves")){
     
  11. Offline

    Dexeron

    JagdCoder I already changed it to that, It still does not solve my problem. Any Ideas?
     
  12. Offline

    JagdCoder

    Try to use return statement so when the plugin should stop use return true;
     
  13. Offline

    Dexeron

    Like Return False? What are you asking me to do? Can you explain in more detail please?


    JagdCoder
     
  14. Offline

    JagdCoder

    When a boolean returns true it stops executing at that exact line.

    It would be inefficient to execute the whole task

    If u use return false it does the same but sends the sender the usage of the command written in plugin.yml
     
  15. Offline

    Dexeron

    Send me a snippet of code, You aren't really telling me what to do here.


    JagdCoder
     
  16. Basically you want to check how many arguments you have and then do stuff accordingly.

    Code:
    if(args.length > 0) // arguments number above 0
    {
          // check your individual subcommands/arguments
    }
    else // no arguments
    {
          // print sub-commands list
    }
    You should learn about arrays, it might be less confusing then.
     
Thread Status:
Not open for further replies.

Share This Page