Solved Need help understanding problem

Discussion in 'Plugin Development' started by YURInsane, Aug 16, 2016.

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

    YURInsane

    I'm new to working with Java in general, just started 4 days ago. I'm trying to create a custom infection plugin. But I keep getting this error in my console.

    Code:
    [17:19:28] [Server thread/INFO]: YURInsane issued server command: /infsetmap
    [17:19:28] [Server thread/ERROR]: null 
    org.bukkit.command.CommandException: Unhandled exception executing command 'infsetmap' in plugin Infected v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.10.2-R0.1.jar:git-Bukkit-5e5cf84] 
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:139) ~[craftbukkit-1.10.2-R0.1.jar:git-Bukkit-5e5cf84]
    at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:625) ~[craftbukkit-1.10.2-R0.1.jar:git-Bukkit-5e5cf84]
    at net.minecraft.server.v1_10_R1.PlayerConnection.handleCommand(PlayerConnection.java:1299) [craftbukkit-1.10.2-R0.1.jar:git-Bukkit-5e5cf84]
    at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:1159) [craftbukkit-1.10.2-R0.1.jar:git-Bukkit-5e5cf84]
    at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.10.2-R0.1.jar:git-Bukkit-5e5cf84]
    at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.10.2-R0.1.jar:git-Bukkit-5e5cf84]
    at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit-1.10.2-R0.1.jar:git-Bukkit-5e5cf84]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_92] 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_92]
    at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [craftbukkit-1.10.2-R0.1.jar:git-Bukkit-5e5cf84] 
    at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:688) [craftbukkit-1.10.2-R0.1.jar:git-Bukkit-5e5cf84]
    at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:361) [craftbukkit-1.10.2-R0.1.jar:git-Bukkit-5e5cf84]
    at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:643) [craftbukkit-1.10.2-R0.1.jar:git-Bukkit-5e5cf84]
    at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:547) [craftbukkit-1.10.2-R0.1.jar:git-Bukkit-5e5cf84]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_92]
     Caused by: java.lang.ArrayIndexOutOfBoundsException: 0 
    at infected.mcraft.pro.infected.Infection.onCommand(Infection.java:91) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.10.2-R0.1.jar:git-Bukkit-5e5cf84]
    ... 15 more
    This is the code I'm using:

    Code:
    package infected.mcraft.pro.infected;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.logging.Logger;
    
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Infection extends JavaPlugin {
        public List<String> players = new ArrayList<String>();
        String map1 = "map1";
        String map2 = "map2";
        String map3 = "map3";
        String map4 = "map4";
        String map5 = "map5";
        String map6 = "map6";
        String map7 = "map7";
        String map8 = "map8";
        String map9 = "map9";
        String map10 = "map10";
        String mapName1 = null;
        String mapName2 = null;
        String mapName3 = null;
        String mapName4 = null;
        String mapName5 = null;
        String mapName6 = null;
        String mapName7 = null;
        String mapName8 = null;
        String mapName9 = null;
        String mapName10 = null;
        int mapLocX1 = 0;
        int mapLocX2 = 0;
        int mapLocX3 = 0;
        int mapLocX4 = 0;
        int mapLocX5 = 0;
        int mapLocX6 = 0;
        int mapLocX7 = 0;
        int mapLocX8 = 0;
        int mapLocX9 = 0;
        int mapLocX10 = 0;
        int mapLocY1 = 0;
        int mapLocY2 = 0;
        int mapLocY3 = 0;
        int mapLocY4 = 0;
        int mapLocY5 = 0;
        int mapLocY6 = 0;
        int mapLocY7 = 0;
        int mapLocY8 = 0;
        int mapLocY9 = 0;
        int mapLocY10 = 0;
        int mapLocZ1 = 0;
        int mapLocZ2 = 0;
        int mapLocZ3 = 0;
        int mapLocZ4 = 0;
        int mapLocZ5 = 0;
        int mapLocZ6 = 0;
        int mapLocZ7 = 0;
        int mapLocZ8 = 0;
        int mapLocZ9 = 0;
        int mapLocZ10 = 0;
        public String[] mapNames = { mapName1, mapName2, mapName3, mapName4, mapName5, mapName6, mapName7, mapName8,
                mapName9, mapName10 };
        public String[] mapOrder = { map1, map2, map3, map4, map5, map6, map7, map8, map9, map10 };
        public int[] mapLocX = { mapLocX1, mapLocX2, mapLocX3, mapLocX4, mapLocX5, mapLocX6, mapLocX7, mapLocX8, mapLocX9, mapLocX10 };
        public int[] mapLocY = { mapLocY1, mapLocY2, mapLocY3, mapLocY4, mapLocY5, mapLocY6, mapLocY7, mapLocY8, mapLocY9, mapLocY10 };
        public int[] mapLocZ = { mapLocZ1, mapLocZ2, mapLocZ3, mapLocZ4, mapLocZ5, mapLocZ6, mapLocZ7, mapLocZ8, mapLocZ9, mapLocZ10 };
    
        public void onEnable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();
    
            logger.info(pdfFile.getName() + "has been enabled (V." + pdfFile.getVersion() + ")");
        }
    
        public void onDisable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();
    
            logger.info(pdfFile.getName() + "has been disabled!");
        }
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player player = (Player) sender;
            if ((label.equalsIgnoreCase("infsetmap")) && player.isOp()) {
                for (int i = 0; i <= mapOrder.length; i++) {
                    if (args[0].equalsIgnoreCase(mapOrder[i])) {
                        mapNames[i] = args[1];
                        Location playerLoc = player.getLocation();
                        mapLocX[i] = playerLoc.getBlockX();
                        mapLocY[i] = playerLoc.getBlockY();
                        mapLocZ[i] = playerLoc.getBlockZ();
                        player.sendMessage("Map " + mapNames[i] + " set");
                    } else if ((!(args[0].equalsIgnoreCase(mapOrder[i]))) && (i == mapOrder.length)) {
                        player.sendMessage("Invalid argument or trying to exceed 10 maps");
                    } else {
                        player.sendMessage("loop" + i);
                    }
                 
                }
    
            } else {
                player.sendMessage("You do not have permission to do this!");
            }
            if (label.equalsIgnoreCase("infjoin")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("You must be a player to use this Command");
                }
    
                else {
                    for (int i = 0; i <= players.size(); i++) {
                        if (player.getName() == players.get(i)) {
    
                        } else if ((player.getName() != players.get(i)) && (i == players.size())) {
                            players.add(player.getName());
    
                        } else {
    
                        }
                    }
                    player.sendMessage("Joined Gamemode Infection");
                }
            }
            else {
             
            }
    
            return false;
        }
    
    }
    And please don't criticize my method for storing location data, unless it is relevant to the error I'm getting. I'm new as I said and other methods of doing this that I might not understand would just make a problem for me latter if I needed to call the variable later. And obviously this plugin is no where near done, I was just testing it to debug errors before I got to far into it.
     
    Last edited: Aug 16, 2016
  2. @YURInsane
    ArrayIndexOutOfBoundsException means that you are trying to access an index of an array which does not exist.
     
  3. Offline

    YURInsane

    But where I have looked over it like a hundred times and cant figure out which array isn't already defined. And this is taking into account you can ignore the second command I was working on "infjoin", tried removing all of that to see if it had to do with the arrayList() I was using, but I did /infsetmap and it still gave me the error
     
  4. Offline

    szalonczuker

    I think I saw the problem. Why do you use
    ? It should be
    PHP:
    if(command.getName().equalsIgnoreCase("...")) {
    ...
    }
    As I know, this is the way to do it. Just in case, if this is not the problem, could you try to execute '/infjoin'? If it throws the same error, then this is your problem.
    Hope it helped. :)

    Edit: No, I'm really sorry... The problem is you try to get args[1] without checking if it exists. Put it into an IF expression and check (args.lenght > 0) if there are any arguments.
     
    YURInsane likes this.
  5. Offline

    YURInsane

    Thank you, this did work, but what I am not understanding is why does it require checking if args[] has any values in order to run?
     
  6. Offline

    MCMastery

    what if the player doesn't give any arguments
     
  7. Offline

    szalonczuker

    You can put in this code:
    Code:
            if(args.length == 1            
         /* This command
            needs only one argument */) {
               
                //Do your code here
               
            } else {
               
                sender.sendMessage(this.getCommand(cmd.getName()).getUsage());
               
                // When the argument count is not
                // correct, it prints the usage.
               
            }
     
  8. Offline

    YURInsane

    well what i did actually was
    Code:
    if(label.equalsIgnoreCase("infsetmap"){
            if(args.length > 0){
            }
    }
    and this worked, but thanks for the help
     
  9. Offline

    ArsenArsen

    @YURInsane use Command#getName() instead, and just compare with your command name from plugin.yml. Aliases are handled correctly that way.
     
Thread Status:
Not open for further replies.

Share This Page