Solved Arraylist.size() is bigger than two despite it containing nothing

Discussion in 'Plugin Development' started by xpaintall, Aug 31, 2022.

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

    xpaintall

    so basically I'm trying to code a minigame and I'm storing the players inside of an arraylist called "players".
    Code:
    private ArrayList<UUID> players = new ArrayList<>();
    
        public ArrayList<UUID> getPlayers() {
            return players;
        }
        public void setPlayers(ArrayList<UUID> players) {
            this.players = players;
        }
    but, inside of a void in the same class when I'm checking if the "getPlayers().size()" is less than 2, a message will popup:
    Code:
    player.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "The game is full, you can't join");
    Now my problem is that when I try and join the queue (calling that void) I keep getting the unable to join message, despite not storing anything in the arraylist beforehand.
    Code:
    if(getPlayers().size() > 2) {
                //code
            } else {
                player.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "The game is full, you can't join");
                player.sendMessage(ChatColor.GREEN + "" + getPlayers().size());
            }
    When I checked how many UUIDs were stored in the second message, it said "0".

    entire void:
    Code:
        //used to join the queue
        public void joinQueue(Player player) {
            if(getPlayers().size() > 2) {
                if (f.getState() == GameState.WAITING) {
                    if (!players.contains(player.getUniqueId())) {
                        getPlayers().add(player.getUniqueId());
                        player.sendMessage(ChatColor.GREEN + "Joined the game!" + " " + getPlayers().size() + "/2 players");
    
                        if (getPlayers().size() == 2) {
                            countdownGame();
                            Bukkit.broadcastMessage(ChatColor.RED + "Countdown requirements fulfilled! Game starting in 10 seconds");
                          
                        }
                    } else {
                        player.sendMessage(ChatColor.GREEN + "You can't join if you're already in the game");
                    }
    
                } else {
                    player.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "The game is already running, you can't join");
                }
            } else {
                player.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "The game is full, you can't join");
                player.sendMessage(ChatColor.GREEN + "" + getPlayers().size());
            }
        }
    the answer to this may be obvious but I have no idea as to what's causing the issue
    also, there are no errors in the console
     
  2. Offline

    KarimAKL

    @xpaintall You're checking if the size of the list is more than 2, not less than 2. I believe a simple change from '>' to '<' should fix it.
     
  3. Offline

    xpaintall

    @KarimAKL omg i am so sorry for being this stupid

    solved
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page