Kaelinator
Kaelinator

Reputation: 470

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--

Upvotes: 2

Views: 3601

Answers (3)

Andrew Li
Andrew Li

Reputation: 57964

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.

Upvotes: 3

FLP
FLP

Reputation: 2047

Player p = Bukkit.getPlayer(uuid);

source

You can use

UUID.fromString("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")

or get it from player.getUUID()

Upvotes: 0

Victor Olaitan
Victor Olaitan

Reputation: 79

A much simpler method works like this

UUID myUUID = myPlayer.getUniqueID();
String configLine = myUUID.toString();

Then

Player newPlayer = Bukkit.getPlayer(myUUID);

Or

Player newPlayer = Bukkit.getPlayer(UUID.fromString(configLine));

Upvotes: 3

Related Questions