Search code examples
minecraftbukkit

Bukkit - UUID to Player method?


So, I'm fairly new to the Bukkit API, and to be honest, the Java class I took last year didn't help me as much as it should have. I'm going to refer to a post from StackExchange[URL]Bukkit - Using static variables causing problems throughout this post. The nice user who answered my question told me to use a HashMap, with the UUID of each pair of players for the keys and values. He said to not use Player variables, instead use their UUIDs. My question is, how can I use methods on players by specifying their UUID? Is there some sort of UUID.toPlayer(UUID) method I could use?

Thanks in advance :D

--Noone has replied to this post on the Bukkit forums, that's why I'm here--


Solution

  • You could loop through all players on the server and try to match their UUID.

     public Player getPlayerByUuid(UUID uuid) {
          for(Player p : getServer().getOnlinePlayers()) {
              if(p.getUniqueId().equals(uuid)
                  return p;
              }
          }
    
     }
    

    This was found here.

    This will loop through all players online and match UUID.

    You could store them in a hashmap of type UUID, Player or string to store the players' names. Then access by,

    Player p = hashMap.get(uuid here (key) );
    

    You'd probably add the players once they join and add to a hashmap.

    hashMap.put(UUID (key), Player (value) );
    

    If you want to access UUID by player, just switch it around.