user6442331
user6442331

Reputation:

Replacing UUID of player to player's name

I am trying to send a message to the player with his saved friends. These friends are saved in a .yml file, but only the UUIDS of each individual player.

I am then trying to replace the UUID or convert it to the players name when the message is displayed (if that made sense)

CODE:

p.sendMessage("§7▄▄▄▄▄▄▄▄▄▄▄▄§aFriend System - page 1 of 1§7▄▄▄▄▄▄▄▄▄▄▄▄");

          int i = 1;
          int length = cfg.getList(p.getUniqueId() + ".Friends").size();
          if (length != 0)
          {
            while (i <= length)
            {
              String uuid = (String)cfg.getList(p.getUniqueId() + ".Friends").get(i - 1);
              ProxiedPlayer p2 = ProxyServer.getInstance().getPlayer(UUID.fromString(uuid));
              if (p2 != null)
              {
                TextComponent prefix = new TextComponent(Main.prefix);

                TextComponent join = new TextComponent("§a§lONLINE");



                prefix.addExtra("§9" + p2.getName());
                prefix.addExtra(" ");
                prefix.addExtra(join);

                p.sendMessage(prefix);
              }
              else
              {
                String name = getNamebyUUID(uuid);
                if (name != null)
                {
                  p.sendMessage(Main.prefix + "§9" + name + " §c§lOFFLINE");
                  Main.names.put(uuid, name);

                }
                else if (Main.names.containsKey(uuid))
                {
                  p.sendMessage(Main.prefix + "§9" + (String)Main.names.get(uuid) + " §8[§c§lOFFLINE§8]");
                }
                else
                {
                  p.sendMessage(Main.prefix + "§cThis is not a valid player!");
                }
              }
              i++;
            }
          }
          else
          {
            p.sendMessage(Main.prefix + "§cYou don't have any friends.");
          }
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
      }

Upvotes: 1

Views: 4884

Answers (1)

LeoColman
LeoColman

Reputation: 7143

Related Question: Get Offline Player by UUID


If the Player is online:


String playerUUID;
Bukkit.getPlayer(playerUUID).getDisplayName();

If the Player is offline:


Not trully possible. The Player can change names at any time, and Bukkit can't keep that information and keep updating all the player's name whenever they change. You can either use the online-player-only method above, or store the player's name together with the uuid.

@UPDATE

As stated by the user Pokechu22, Bukkit does save the last name the user used before logging out. It can be retrieved this way:

OfflinePlayer off = Bukkit.getOfflinePlayer(String uuid);
String lastKnownName = off.getName();

But be very careful! It might not be the up-to-date name of the player.


Upvotes: 1

Related Questions