diff --git a/src/main/java/com/technicjelle/bluemapplayercontrol/commands/BMPC.java b/src/main/java/com/technicjelle/bluemapplayercontrol/commands/BMPC.java index 2609dbe..9391440 100644 --- a/src/main/java/com/technicjelle/bluemapplayercontrol/commands/BMPC.java +++ b/src/main/java/com/technicjelle/bluemapplayercontrol/commands/BMPC.java @@ -2,6 +2,7 @@ import de.bluecolored.bluemap.api.BlueMapAPI; import org.bukkit.ChatColor; +import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -10,9 +11,13 @@ import org.jetbrains.annotations.NotNull; import java.util.*; +import java.util.stream.Collectors; public class BMPC implements CommandExecutor, TabCompleter { + private List playerNames; + private long cacheAge; + public BMPC() { } @@ -43,8 +48,10 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command } // === OTHER === - Player targetPlayer = sender.getServer().getPlayer(args[args.length - 1]); //if the last argument is a player name - if (targetPlayer == null) { + //Suppressing deprecation warnings for Server::getOfflinePlayer(String), because we are only using the Name to look up the UUID, not as a unique identifier + @SuppressWarnings("deprecation") + OfflinePlayer targetPlayer = sender.getServer().getOfflinePlayer(args[args.length - 1]); //if the last argument is a player name + if (!targetPlayer.hasPlayedBefore()) { //targetPlayer can't be null so this is the easiest way to verify that a player with the given name actually exists if(othersAllowed(sender)) { sender.sendMessage(ChatColor.YELLOW + "Player not found"); } else { @@ -53,6 +60,10 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command return true; } else { if(othersAllowed(sender)) { + String displayName = targetPlayer.getName(); + Player onlinePlayer = targetPlayer.getPlayer(); + if(onlinePlayer != null) + displayName = onlinePlayer.getDisplayName(); UUID targetUUID = targetPlayer.getUniqueId(); if (args.length == 1) { //TODO: Toggle other @@ -60,11 +71,11 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command return true; } else if (args[0].equalsIgnoreCase("show")) { api.getWebApp().setPlayerVisibility(targetUUID, true); - sender.sendMessage(targetPlayer.getDisplayName() + " is now visible on the map"); + sender.sendMessage(displayName + " is now visible on the map"); return true; } else if (args[0].equalsIgnoreCase("hide")) { api.getWebApp().setPlayerVisibility(targetUUID, false); - sender.sendMessage(targetPlayer.getDisplayName() + " is now invisible on the map"); + sender.sendMessage(displayName + " is now invisible on the map"); return true; } } else { @@ -94,9 +105,15 @@ public List onTabComplete(CommandSender sender, Command command, String || args[0].equalsIgnoreCase("hide") || args[0].isBlank()) { if(args.length <= 2) { - for (Player player : sender.getServer().getOnlinePlayers()) { - completions.add(player.getName()); + if(playerNames == null|| cacheAge < System.currentTimeMillis() - 60000){ + playerNames = Arrays.stream(sender.getServer().getOfflinePlayers()) + .map(OfflinePlayer::getName) + .collect(Collectors.toList()); + cacheAge = System.currentTimeMillis(); } + completions.addAll(playerNames.stream() + .filter(name -> name.toLowerCase().startsWith(args[args.length - 1].toLowerCase())) + .collect(Collectors.toList())); } } }