Solved Help in getting online players in a certian arraylist

Discussion in 'Spigot Plugin Development' started by ImJustHere, Feb 12, 2020.

  1. For some reason the only way that I get the full list or if the msg sends twice or I only get 1 player from the top of the player's list
    Code (Text):
        public static void Check(Player player)
        {
            for(UUID uuid : Staff)
            {
                if(Bukkit.getPlayer(uuid).isOnline())
                {
                    if(Bukkit.getPlayer(uuid).getWorld() == player.getWorld())
                    {
                        if(Staff.contains(uuid))
                        {
                            player.sendMessage(ChatColor.GRAY + "Online Staff that in your world!");
                            player.sendMessage(String.join(",", Bukkit.getPlayer(uuid).getName()));
                            player.sendMessage(String.join(",", uuid.toString()));
                            break;
                        } else {
                            player.sendMessage(ChatColor.RED + "No Staff!");
                        }
                    }
                }
            }
     
  2. How i understood you have a ArrayList with UUIDs of staff and that method must send to Player a list of online staff in world of player.


    Code (Java):

    public static void Check(Player player) {
        List<Player> online = new ArrayList<>();
        for (UUID uuid : Staff) {
            if(Bukkit.getPlayer(uuid) != null) {
                if (Bukkit.getPlayer(uuid).getWorld() == player.getWorld()) {
                    online.add(Bukkit.getPlayer(uuid));
                }
            }
        }
        if(online.size() > 0){
            player.sendMessage(ChatColor.GRAY + "Online Staff that in your world!");
            for(Player p : online){
                player.sendMessage(p.getName());
            }
        } else {
            player.sendMessage(ChatColor.RED + "No Staff in your world!");
        }
    }
     
    EDIT: i forgot to check if online
     
  3. drives_a_ford

    Moderator

    I'd suggest you cache the result from the getPlayer method instead of calling the method (up to) 3 times for every UUID. You could also cache the World. In fact, I'd personally have a separate method that gets online staff in a specific world. Because what you're doing is a) finding online staff and then b) displaying their names.

    As for the OP, please read Beginner Programming Mistakes and Why You're Making Them.
    This method shouldn't be static, you're not adhering to the naming convention. Plus, the method name is in no way descriptive of what it does.
     
    • Agree Agree x 1
  4. oh, it is working now but how will I send the players in that world as a straight line ik I need to use
    Code (Text):
     String.join(", ", variable here);
     
    but it's not working when I try to send it like that
     
  5. FrostedSnowman

    Resource Staff

    Some things to consider before putting this into production:
    • Cache the result of Bukkit::getPlayer to avoid multiple map lookups
    • Use a Set implementation if you plan to hold some elements with no significant order
    • For method declaration, follow standard Java conventions; lowerCamelCase
    You're likely sending the message within the loop, send it outside. Additionally, the logic in what you're trying to do just seems odd. Based on what you provided it's as if you never remove elements from your Staff collection, which could imply the possibility of a memory leak in the future.

    Remove staff from the collection on quit, add them on join. You should then just be able to check if your staff list is empty, if it is, then no staff are online in any world.

    Your logic could be implemented in such a manner that is similar to:
    Code (Java):
    if (staff.isEmpty()) {
         sendMessage("No staff online");
         return;
    }
    Set<String> inWorld = new HashSet<>();
    for (UUID member : staff) {
         Player player = Bukkit.getPlayer(member);
         if (player.getWorld() == world) {
              inWorld.add(player.getName());
         }
    }
    sendMessage(inWorld.isEmpty() ? "No staff in your world" : "Staff: " + String.join(", ", inWorld));
    Stream API is also a possibility here. You can use Stream::map to map the UUIDs to players, then can use Stream::filter to check for staff in the world, then re-map again to player names, then collect them into a string.
     
    • Like Like x 2
  6. Thx for everyone it's working now