Array not Cycling through player

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

  1. Hello, in my plugin, there is 4 arenas, each arena can fit 4 players (2v2), I have 8 players, the 8 players should be teleported to fill 2 arenas, however, only 4 players are being teleported. I am not getting any error.

    The teleporting code is in the twovstwo method.

    I have a for loop, within are 4 main if commands that do the teleporting, the first one required all arenas to not be full, and teleports players to the first. The second required the first arena to be full, and the rest to full, and teleports players to the second arena... and so on.

    Code for 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> bridge1 = new ArrayList();
        public static ArrayList<Location> bridge2 = new ArrayList();
        public static ArrayList<Location> bridge3 = new ArrayList();
        public static ArrayList<Location> bridge4 = new ArrayList();
        int task;

        boolean game1 = false;
        boolean game2 = false;
        boolean game3 = false;
        boolean game4 = false;

        int rating1, rating2, rating3, rating4;

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

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

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

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

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

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

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

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

        @SuppressWarnings("deprecation")
        public void twovstwo(Player[] pl) {

            Location tps = null;

            int iterate = 1;

            for (Player p : pl) {
                Bukkit.getLogger().info("All names: " + p.getName());
            }

            for (int i = 0; i <= pl.length; i++) {

                if (game1 == false && game2 == false && game3 == false && game4 == false) {
                    if (i < bridge1.size()) {
                        tps = (Location) bridge1.get(i);
                        pl[i].teleport(tps);
                        Bukkit.getLogger().info("Name: " + pl[i].getName());
                        Bukkit.getLogger().info("iterate " + iterate);
                        if (iterate == 4) {
                            game1 = true;
                            iterate = 1;
                            Bukkit.getLogger().info("game1: " + game1);
                        }
                    }
                } else

                if (game1 == true && game2 == false && game3 == false && game4 == false) {
                    if (i < bridge2.size()) {
                        tps = (Location) bridge2.get(i);
                        pl[i].teleport(tps);
                        Bukkit.getLogger().info("Name: " + pl[i].getName());
                        Bukkit.getLogger().info("iterate " + iterate);
                        if (iterate == 4) {
                            game2 = true;
                            iterate = 1;
                            Bukkit.getLogger().info("game2: " + game2);
                        }
                    }
                } else

                if (game1 == true && game2 == true && game3 == false && game4 == false) {
                    if (i < bridge2.size()) {
                        tps = (Location) bridge3.get(i);
                        pl[i].teleport(tps);
                        Bukkit.getLogger().info("Name: " + pl[i].getName());
                        Bukkit.getLogger().info("iterate " + iterate);
                        if (iterate == 4) {
                            game3 = true;
                            iterate = 1;
                            Bukkit.getLogger().info("game3: " + game3);
                        }
                    }
                } else

                if (game1 == true && game2 == true && game3 == true && game4 == false) {
                    if (i < bridge2.size()) {
                        tps = (Location) bridge4.get(i);
                        pl[i].teleport(tps);
                        Bukkit.getLogger().info("Name: " + pl[i].getName());
                        Bukkit.getLogger().info("iterate " + iterate);
                        if (iterate == 4) {
                            game4 = true;
                            iterate = 1;
                            Bukkit.getLogger().info("game4: " + game4);
                        }
                    }
                } else

                if (game1 == true && game2 == true && game3 == true && game4 == true) {
                    Bukkit.getLogger().info("[Game] All queues are currently full");
                    break;
                }

                iterate++;

            }

            // for (int i = 0; i <= pl.length; i++) {
            //
            // if (game1 == false && game2 == false && game3 == false && game4 ==
            // false) {
            // if (i < bridge1.size()) {
            // tps = (Location) bridge1.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) {
            // if (i < bridge2.size()) {
            // tps = (Location) bridge2.get(i);
            // pl[i].teleport(tps);
            // if (iterate == 4) {
            // game2 = true;
            // }
            // }
            // } else if (game1 == true && game2 == true && game3 == false && game4
            // == false) {
            // tps = (Location) bridge3.get(i);
            // pl[i].teleport(tps);
            // if (iterate == 4) {
            // game3 = true;
            // }
            // } else if (game1 == true && game2 == true && game3 == true && game4
            // == false) {
            // tps = (Location) bridge4.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);
        }

    }
     
    #1 Fluoro, Apr 17, 2017
    Last edited: Apr 17, 2017
  2. Mas

    Mas

    I'm not exactly sure of your problem, could you elaborate?

    Also, you should take some time to learn Object Oriented principles, because at the moment you are just static-abusing without knowing the proper use.
     
  3. I'll elaborate, check the edit.
    The static part is written by other members on my team, I mostly only did the twovstwo method.
     
  4. Mas

    Mas

    I'll take a look now.

    You should consider finding team members who know Java :p
     
    • Agree Agree x 1
    • Funny Funny x 1
  5. haha I guess so, it's hard finding team members I know who know Java tho
     
  6. Have you tried debugging the code?
     
  7. I'm not fully sure how to use the debugging console. I did add code to print out variables to the console.
     
  8. Well, printing is the best way of debugging :), found anything?
     
    • Funny Funny x 1
  9. not enough to solve my problem
    If you see the code, you'll see that I've already did printing 3 hours ago

    See the Bukkit.getLogger.info part?
     
  10. OOP will save you time and you're not groping in the dark.
     
    • Agree Agree x 1
    • Informative Informative x 1
  11. Have you tried printing out "pl.length", cause if that's 0, then you know why it isn't cycling trough, or manipulate it to see if the array is indeed just not working
     
  12. well in the code, I printed out all the elements in the array, which is 8 players, so it isn't 0.
    Uh have you read my code?
     
  13. yes I did, but It's very difficult to find such a problem, because if it was a little easier, wpeople would have found a solution already...
     
    • Like Like x 1
  14. yea, it puzzled me and my team too
     
    • Like Like x 1
  15. The bug is a pretty stupid one, caused by not splitting this code up properly.
    • Properly indent your code.
    • Don't repeat yourself 4 times, create a game class where only one instance is managed:
      • Remove all the bridgeX fields (leave one single 'bridge' field).
      • Remove all gameX fields (leave one single 'running' field to hold the enabled-less).
      • Remove all ratingX fields (leave one 'rating' field)
      • Remove static from all fields, it's not required.
      • Use BukkitTasks instead of int task IDs, by using runTask methods instead of schedule. Also, use sync tasks instead of async tasks.
      • For clarity, don't just instantiate classes like 'DoubleDamage', but do something with it (like registering listeners? I've no idea what it does)
    • For the sake of mentioning it, you're not supposed to use Bukkit::getLogger(), but use your plugin's Logger instead)
    Now for the actual problem that's been bugging you: for players 4-7 (using i, which starts at 0), it'll use the second game, but the size of bridges2 is 4. 4 < 4 is false, 5 < 4 is false, etc.
     
  16. Have you been able to solve my problem?
     
  17. Mas

    Mas

    Yes, the code never continues past the first lot of teleporting because of this line:
    Code (Java):
    if (i < bridge2.size())
    which will always be evaluating to false.

    Also, not trying to offend you, but pretty much all your code is extremely inefficient and will be very difficult to scale. It's your choice of course, however I would definitely recommend creating some sort of Game API allowing you to manage arenas, players, spawning, game mechanics, etc much more easily.
     
    • Useful Useful x 1
  18. Don't worry, I'm not offended, you're correct.

    After looking at your answer I feel kind of stupid :) The answer was so simple. Thanks for all the help guys!
     
  19. Mas

    Mas

    It took me about 5 minutes to find it, so don't feel stupid. This is all part of learning. Sorry it took me so long to respond also.
     
    • Like Like x 1
  20. okay for some reason, when I remove the if command you told me to remove, I can an IndexOutOfBounds exception.