IndexOutOfBoundsException while teleporting

Discussion in 'Spigot Plugin Development' started by Fluoro, Apr 16, 2017.

  1. Hello there,
    I'm getting an IndexOutOfBoundsException while cycling through players to teleport to an arena.
    Here's how it works. An arena can hold 4 players, I have 4 arenas, and I got 8 players that need to join, those 8 players should fill up 2 arena, however, the first arena gets filled and the rest doesn't get tped and I get an IndexOutOfBoundsException.

    Here's my Game class:
    Code (Text):
    package net.gamer818;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.Player;
    import org.bukkit.scheduler.BukkitScheduler;

    public class Game {
        static int timer = 0;
        public static ArrayList<Location> tp1 = new ArrayList();
        public static ArrayList<Location> tp2 = new ArrayList();
        public static ArrayList<Location> tp3 = new ArrayList();
        public static ArrayList<Location> tp4 = new ArrayList();
        int task;

        boolean game1, game2, game3, game4;

        int rating1, rating2, rating3, rating4;

        public Game() {
            World w = Bukkit.getWorld("world");
            tp1.add(new Location(w, -2498.977D, 17.0D, 779.953D));
            tp1.add(new Location(w, -2498.977D, 17.0D, 779.953D));

            tp1.add(new Location(w, -2443.894D, 17.0D, 802.975D));
            tp1.add(new Location(w, -2443.894D, 17.0D, 802.975D));

            tp2.add(new Location(w, -2530.024D, 17.0D, 772.028D));
            tp2.add(new Location(w, -2530.024D, 17.0D, 772.028D));

            tp2.add(new Location(w, -2506.965D, 17.0D, 716.968D));
            tp2.add(new Location(w, -2506.965D, 17.0D, 716.968D));

            tp3.add(new Location(w, -2498.807D, 17.0D, 686.174D));
            tp3.add(new Location(w, -2498.807D, 17.0D, 686.174D));

            tp3.add(new Location(w, -2443.948D, 17.0D, 709.019D));
            tp3.add(new Location(w, -2443.948D, 17.0D, 709.019D));

            tp4.add(new Location(w, -2413.032D, 17.0D, 716.982D));
            tp4.add(new Location(w, -2413.032D, 17.0D, 716.982D));

            tp4.add(new Location(w, -2436.006D, 17.0D, 772.045D));
            tp4.add(new Location(w, -2436.006D, 17.0D, 772.045D));
        }

        @SuppressWarnings("deprecation")
        public void twovstwo(Player[] pl) {
           
            Location tps = null;
           
            int iterate = 0;


            for (int i = 0; i < pl.length; i++) {
               
                if (game1 == false && game2 == false && game3 == false && game4 == false) {
                    tps = (Location) tp1.get(i);
                    pl[i].teleport(tps);
                    Bukkit.getLogger().info("iterate " + iterate);
                    if (iterate == 4) {
                        game1 = true;
                        Bukkit.getLogger().info("game1: " + game1);
                    }
                } else if (game1 == true && game2 == false && game3 == false && game4 == false) {
                    tps = (Location) tp2.get(i);
                    pl[i].teleport(tps);
                    if (iterate == 4) {
                        game2 = true;
                    }
                } else if (game1 == true && game2 == true && game3 == false && game4 == false) {
                    tps = (Location) tp3.get(i);
                    pl[i].teleport(tps);
                    if (iterate == 4) {
                        game3 = true;
                    }
                } else if (game1 == true && game2 == true && game3 == true && game4 == false) {
                    tps = (Location) tp4.get(i);
                    pl[i].teleport(tps);
                    if (iterate == 4) {
                        game4 = true;
                    }
                } else if (game1 == true && game2 == true && game3 == true && game4 == true) {
                   
                }
               
                iterate++;

            }

            this.task = Bukkit.getScheduler().scheduleAsyncRepeatingTask(ArenaBrawl.getInstance(), new Runnable() {
                public void run() {
                    Game.timer += 1;
                    if (Game.timer == 300) {
                        new DoubleDamage();
                    }
                    if (Game.timer == 600) {
                        new Draw();
                    }
                }
            }, 20L, 20L);
        }

    }
    Here's my Locations class:

    Code (Text):
    package net.gamer818;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.Player;

    public class Locations {
        public static ArrayList<Location> tp = new ArrayList();

        public Locations() {
            World w = Bukkit.getWorld("world");
            tp.add(new Location(w, -2498.977D, 17.0D, 779.953D));
            tp.add(new Location(w, -2498.977D, 17.0D, 779.953D));
            tp.add(new Location(w, -2443.894D, 17.0D, 802.975D));
            tp.add(new Location(w, -2443.894D, 17.0D, 802.975D));
            tp.add(new Location(w, -2530.024D, 17.0D, 772.028D));
            tp.add(new Location(w, -2530.024D, 17.0D, 772.028D));
            tp.add(new Location(w, -2506.965D, 17.0D, 716.968D));
            tp.add(new Location(w, -2506.965D, 17.0D, 716.968D));
            tp.add(new Location(w, -2498.807D, 17.0D, 686.174D));
            tp.add(new Location(w, -2498.807D, 17.0D, 686.174D));
            tp.add(new Location(w, -2443.948D, 17.0D, 709.019D));
            tp.add(new Location(w, -2443.948D, 17.0D, 709.019D));
            tp.add(new Location(w, -2413.032D, 17.0D, 716.982D));
            tp.add(new Location(w, -2413.032D, 17.0D, 716.982D));
            tp.add(new Location(w, -2436.006D, 17.0D, 772.045D));
            tp.add(new Location(w, -2436.006D, 17.0D, 772.045D));
        }

        public List<String> sortPlayers(List<String> strings) {
            Collections.sort(strings, new Comparator<String>() {
                public int compare(String o1, String o2) {
                    return extractInt(o1) - extractInt(o2);
                }

                int extractInt(String s) {
                    String num = s.replaceAll("\\D", "");
                    // return 0 if no digits found
                    return num.isEmpty() ? 0 : Integer.parseInt(num);
                }
            });

            return strings;

        }

        public static void teleportPlayers() {
            Game g = new Game();
            Locations L = new Locations();
            List<String> ppl = new ArrayList<>();
            List<Player> pl = new ArrayList<>();
            Player[] simpleArray = new Player[pl.size()];

            Rating r = new Rating();
            for (int i = 0; i < ArenaBrawl.getInstance().playersList.size(); i++) {
                Player player = (Player) ArenaBrawl.getInstance().playersList.get(i);
                ppl.add(player.getName() + "*" + r.getRating(player.getName()));
            }

            ppl = L.sortPlayers(ppl);

            for (int i = 0; i < ArenaBrawl.getInstance().playersList.size(); i++) {
                String[] lol = ppl.get(i).split("\\*");

                // Player p = Bukkit.getServer().getPlayer(lol[0]);
                // Bukkit.getLogger().info("lol1 " + lol[1]);
                // Bukkit.getLogger().info("lol0 " + lol[0]);
                // Bukkit.getLogger().info("ppl stuff " + ppl.get(i).replace(lol[1],
                // "").replaceAll("\\*", ""));
                //
                // Location tps = (Location) tp.get(i);
                // p.teleport(tps);

                pl.add(Bukkit.getServer().getPlayer(lol[0]));
            }
            Player[] player = new Player[8];
            player = Bukkit.getOnlinePlayers().toArray(new Player[Bukkit.getOnlinePlayers().size()]);
            // g.twovstwo(pl.toArray(simpleArray));
            g.twovstwo(player);

        }
    }
     
    And here's the error:
    Code (Text):
    [21:21:59] [Craft Scheduler Thread - 62/WARN]: Exception in thread "Craft Scheduler Thread - 62"
    [21:21:59] [Craft Scheduler Thread - 62/WARN]: org.apache.commons.lang.UnhandledException: Plugin ArenaBrawl v1.0 generated an exception while executing task 93
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:56)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
        at java.util.ArrayList.rangeCheck(Unknown Source)
        at java.util.ArrayList.get(Unknown Source)
        at net.gamer818.Game.twovstwo(Game.java:70)
        at net.gamer818.Locations.teleportPlayers(Locations.java:86)
        at net.gamer818.ArenaJoin$1.run(ArenaJoin.java:72)
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71)
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
        ... 3 more
     
  2. What is line 70 of Game.java?
     
    • Friendly Friendly x 1
  3. Sorry for not including it!!

    Line 70 is
    Code (Text):
    tps = (Location) tp2.get(i);
     
  4. IndexOutOfBounds exception means you're trying to access something from an array or a list that does not exist, for instance trying to get String[2] from an array that contains one string. Keep in mind lists and arrays are 0 based meaning in an array of 5 strings to get the fifth one you would do String[4], likewise to get the first one you would do String[0]
     
    • Informative Informative x 1
  5. pl.length check in the for loop does not ensure that tp1 array will have less than pl array.

    Your pl array is bigger than your tp1 array. You should add some checks to make sure that i is not higher than tp1's length.

    You can't do tp1.get(15) if the array has only 2 things in it
     
  6. but don't I already check during the chain of if loops?
     
  7. No you never even call tp1.size() anywhere
     
  8. I added
    Code (Text):
    if (i <= tp1.size()) {

    }
    before the game1 ==...s but the same error occures
     
  9. Well...
    I'm no expert!
     
  10. It must be < not <=. Java index for types start at 0 not 1. So either < or <= tp1's.size()-1
     
    • Useful Useful x 1
  11. the same error occurs :(
     
  12. Alright I've done what you guys said but I still get the error. :(