Solved Grabbing player names by UUID

Discussion in 'Spigot Plugin Development' started by DisCoder212, Aug 8, 2018.

  1. My plugin loops through all yml files to grab players balances and stores them, then sorts them and orders them from the person containing the highest balance to the lowest balance, I was just wondering on how I could go about converting their UUID's to an actual name because if I define "Player" or "Offlineplayer" it will throw an NPE due to the players either being online or offline, any ideas on what I could do?

    Code (Text):

           balances.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                    .limit(amount)
                    .forEach(s -> {

                        OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(s.getKey());

                        sender.sendMessage(offlinePlayer.getName() + ChatColor.GREEN + " -> " + ChatColor.WHITE + "$" + s.getValue());
                    });
        }
    }
     
     
  2. If you get an NPE then whatever s.getKey() is likely an invalid UUID Make sure you are storing the UUIDs properly. Also, add a check that offlinePlayer != null.
     
  3. You should simply be able to use:
    Code (Java):
    UUID uuid = UUID.fromString("String of UUID");

    /* Get online player */
    Player player = Bukkit.getPlayer(uuid);
    /* Get offline player */
    OfflinePlayer player = Bukkit.getOfflinePlayer(uuid);
     
    • Agree Agree x 1
  4. FrostedSnowman

    Resource Staff

    debug