Solved Error when player uses incorrect syntax with command

Discussion in 'Plugin Development' started by Redstone_Pro_73, Jan 31, 2016.

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

    Redstone_Pro_73

    Hello everyone and thanks for you guys helping me out. So today I started this very simple plugin for a replacement of me typing /pex user <user> group set <rank> every single time I want to manually rank up a player. The command works perfectly fine with the correct syntax. My problem is that I want to display the the "Please use the correct syntax: ..." when me, or any other administrator on the server accidentally types it incorrectly, instead of it actually creating an internal error like this:

    Code:
     [INFO] Redstone_Pro_73 issued server command: /rankset Redstone_Pro_73
    10:41:17 AM [ERROR] null
    10:41:17 AM org.bukkit.command.CommandException: Unhandled exception executing command 'rankset' in plugin RankUpCMD v1.0
    10:41:17 AM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[Spigot.jar:git-Spigot-db6de12-18fbb24]
    10:41:17 AM at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[Spigot.jar:git-Spigot-db6de12-18fbb24]
    10:41:17 AM at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[Spigot.jar:git-Spigot-db6de12-18fbb24]
    10:41:17 AM at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    10:41:17 AM at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    10:41:17 AM at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    10:41:17 AM at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    10:41:17 AM at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    10:41:17 AM at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_65]
    10:41:17 AM at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_65]
    10:41:17 AM at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    10:41:17 AM at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    10:41:17 AM at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    10:41:17 AM at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    10:41:17 AM at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [Spigot.jar:git-Spigot-db6de12-18fbb24]
    10:41:17 AM at java.lang.Thread.run(Thread.java:745) [?:1.8.0_65]
    10:41:17 AM Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    10:41:17 AM at me.Redstone_Pro_73.RankUpCMD.Main.onCommand(Main.java:31) ~[?:?]
    10:41:17 AM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[Spigot.jar:git-Spigot-db6de12-18fbb24]
    10:41:17 AM ... 15 more
    
    This problem is really confusing me since I thought my else part of the command should take care of this. Thanks in advance! Here is my onCommand method:

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            Player player = (Player) sender;
           
            String error = "§7[§cRankUpCMD§7] §4§lError: §f";
            String syntax = "Please use the correct syntax: /rankup <username> <rank>";
           
            if(cmd.getName().equalsIgnoreCase("rankset")) {
               
                if (!args[0].isEmpty() && !args[1].isEmpty()) {
                   
                    //Run command after checking syntax
                    String rank = args[1].toLowerCase();
                   
                    sender.getServer().dispatchCommand(player.getServer().getConsoleSender(), "pex user " + args[0] + " group set " + rank);
                    sender.getServer().dispatchCommand(player.getServer().getConsoleSender(), "tellraw @a [\"\",{\"text\":\"Player \",\"color\":\"green\",\"bold\":true},{\"text\":\"" + args[0] + "\",\"color\":\"aqua\",\"bold\":true,\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"" + args[0] + "\",\"color\":\"yellow\"}]}}},{\"text\":\"  has been ranked up to \",\"color\":\"green\",\"bold\":true},{\"text\":\"" + args[1] + "\",\"color\":\"aqua\",\"bold\":true,\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"" + args[1] + "\",\"color\":\"aqua\"}]}}}]");
                   
                } else {
                    player.sendMessage(error + syntax);
                }
               
                return true;
               
            }
           
            return false;
           
           
        }
     
  2. Offline

    fatpigsarefat

    Try testing the length of the command:

    Code:
            if(cmd.getName().equalsIgnoreCase("rankset")) {
                Player player = (Player) sender;
                int length = args.length;
               
                if (length == 2) {
                  
                    //If synatax is correct
                  
                } else {
                    //Incorrect
                }
              
                return true;
              
            }
     
  3. Offline

    Zombie_Striker

    DON'T BLIND CAST! Check if the sender is actually a player before casting.
    Please create these instances after you know the command is what you are testing for.
    Why do you need two strings? They are only used once.

    Check if the args.length are greater than or equal to 2 before accessing those variables.
     
  4. Offline

    Redstone_Pro_73

    Well, I now a player is running it (solves problem 1). I also am going to add onto the plugin later (solves problem 2, because I will need to repeat stuff like that over and over). And my if statement should work exactly like what you mentions (solves problem 3). Anything else?

    Works! Thanks so much!


    (Merged by moderator: please Edit posts instead of posting back-to-back)
     
    Last edited by a moderator: Jan 31, 2016
  5. Offline

    Zombie_Striker

    @Redstone_Pro_73
    1. No, that's not the point. If you wish to be a competent programmer, you need to make sure there is no way whatsoever your code will spit out an error. If your code can, then you're overlooking something.
    2. Alright.
    3. Expect it isn't. You cant access a variable in an array that does not exist. You would get an AOOB exception if you use what you currently have.
     
Thread Status:
Not open for further replies.

Share This Page