Error on A For Loop

Discussion in 'Plugin Development' started by RDNachoz, Jun 1, 2014.

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

    RDNachoz

    Code:
    [11:42:42] [Server thread/ERROR]: Could not pass event InventoryClickEvent to Hub vB1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:1364) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInWindowClick.a(SourceFile:32) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInWindowClick.handle(SourceFile:10) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at me.rdnachoz.hub.listeners.HubSelector.onInventoryClick(HubSelector.java:110) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_60]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_60]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_60]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_60]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    ... 13 more
    [11:42:44] [Server thread/INFO]: RDNachoz lost connection: Disconnected
    Hello, So I am getting this error and it's on this set of lines:

    Code:java
    1. for(int a = 1; a < Core.flooderServers; ++a) {
    2. String ip = ips[a-1].split(":")[0];
    3. int port = Integer.valueOf(ips[a - 1].split(":")[1]);
    4. Pinger ping = new Pinger(ip, port);


    Please help.

    Thanks,
    Nick
     
  2. This seems dodgy since it looks like a flooder... but I'll help anyway.
    Firstly:
    You're using something like ".get(1)" or "[1]".
    What is line 110 of HubSelector.java?
    This is where I see you using [1]. Although, it may be that your flooderServers integer is greater than 1, which means it'll do ips[a - 1] and so ips[1].
     
  3. Offline

    RDNachoz



    Line 110 is: String ip = ips[a-1].split(":")[0];

    And flooderServers is 9

    And btw no, flooderServers is not a Flooder. Flooder is a new minigame I'm in the process of developing.
     
  4. Code:
    for (int a = 0; a < ips.length; a++) {
        String server = ips[a];
        String[] serverSplit = server.contains(":") ? server.split(":") : new String[] { "", "" };
        String ip = serverSplit.length > 0 ? serverSplit[0] : "";
        int port = serverSplit.length > 1 ? Integer.valueOf(serverSplit[1]) : "";
        Pinger ping = new Pinger(ip, port);
    
    Note the important change from the flooderServers to "ips.length". I believe the reason for your error was because of the array "ips"' length is smaller than flooderServers (not 9). So instead of going up to flooderServers, go up to the length of "ips". The rest is optional, you can use your code, but make sure not to use "a -1", use "a" since I changed a to 0 and not 1. I don't know why you would start with 1 and then minus 1 each time from the object "a" whenever you want to get the String...
     
  5. Offline

    RDNachoz


    I'm using your code and everything seems to be working fine. The only problem is that the inventory that opens closes/crashes after a couple of the servers show up in it. Is there something wrong in this that would cause that?

    Code:java
    1. for (int a = 0; a < ips.length; a++) {
    2. String server = ips[a];
    3. String[] serverSplit = server.contains(":") ? server.split(":") : new String[] { "", "" };
    4. String ip = serverSplit.length > 0 ? serverSplit[0] : "";
    5. int port = (Integer) (serverSplit.length > 1 ? Integer.valueOf(serverSplit[1]) : "");
    6. Pinger ping = new Pinger(ip, port);
    7.  
    8. if(ping.isOnline()) {
    9. ItemStack item = online.clone();
    10. ItemMeta meta = item.getItemMeta();
    11. meta.setDisplayName(ChatColor.BLUE + "Flooder " + a + ChatColor.GREEN + " (" + ping.getOnline() + "/18)");
    12. item.setItemMeta(meta);
    13. flooderInv.addItem(item);
    14. } else {
    15. ItemStack item = offline.clone();
    16. ItemMeta meta = item.getItemMeta();
    17. meta.setDisplayName(ChatColor.BLUE + "Flooder " + a + ChatColor.RED + " (0/18)");
    18. item.setItemMeta(meta);
    19. flooderInv.addItem(item);
    20. }
    21. player.openInventory(flooderInv);
    22. event.setCancelled(true);
    23. }
     
  6. Well, yes. You're opening the inventory and setting the event to be cancelled in the loop, which should loop 9 times in less than a second.
    Code:
                    for (int a = 0; a < ips.length; a++) {
                        String server = ips[a];
                        String[] serverSplit = server.contains(":") ? server.split(":") : new String[] { "", "" };
                        String ip = serverSplit.length > 0 ? serverSplit[0] : "";
                        int port = (Integer) (serverSplit.length > 1 ? Integer.valueOf(serverSplit[1]) : "");
                        Pinger ping = new Pinger(ip, port);
     
                        if(ping.isOnline()) {
                            ItemStack item = online.clone();
                            ItemMeta meta = item.getItemMeta();
                            meta.setDisplayName(ChatColor.BLUE + "Flooder " + a + ChatColor.GREEN + "  (" + ping.getOnline() + "/18)");
                            item.setItemMeta(meta);
                            flooderInv.addItem(item);
                        } else {
                            ItemStack item = offline.clone();
                            ItemMeta meta = item.getItemMeta();
                            meta.setDisplayName(ChatColor.BLUE + "Flooder " + a + ChatColor.RED + "  (0/18)");
                            item.setItemMeta(meta);
                            flooderInv.addItem(item);
                        }
                    }
                    event.setCancelled(true);
                    player.openInventory(flooderInv);
    
     
Thread Status:
Not open for further replies.

Share This Page