Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> playerNames;
private long cacheAge;

public BMPC() {
}

Expand Down Expand Up @@ -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 {
Expand All @@ -53,18 +60,22 @@ 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
sender.sendMessage(ChatColor.YELLOW + "This command is not yet implemented. Please specify show/hide");
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 {
Expand Down Expand Up @@ -94,9 +105,15 @@ public List<String> 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()));
}
}
}
Expand Down