mirror of
https://github.com/YouHaveTrouble/NotJustNameplates.git
synced 2026-05-11 22:16:57 +00:00
add a command to allow toggling the visibility of nameplates of other players
This commit is contained in:
@@ -32,6 +32,7 @@ public final class NotJustNameplates extends JavaPlugin {
|
||||
DefaultPermissions.registerPermission("notjustnameplates.seeown", "Allows a player to see their own nameplate", PermissionDefault.FALSE);
|
||||
DefaultPermissions.registerPermission("notjustnameplates.command", "Allows a player to use the /njn command", PermissionDefault.TRUE);
|
||||
DefaultPermissions.registerPermission("notjustnameplates.command.reload", "Allows a player to use the /njn reload command", PermissionDefault.OP);
|
||||
DefaultPermissions.registerPermission("notjustnameplates.command.toggle", "Allows a player to use the /njn toggle command", PermissionDefault.TRUE);
|
||||
|
||||
this.nameplateManager = new NameplateManager(this);
|
||||
|
||||
|
||||
@@ -2,9 +2,11 @@ package me.youhavetrouble.notjustnameplates.commands;
|
||||
|
||||
|
||||
import me.youhavetrouble.notjustnameplates.NotJustNameplates;
|
||||
import me.youhavetrouble.notjustnameplates.nameplates.NameplateManager;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -37,6 +39,21 @@ public class MainCommand extends Command {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args[0].equalsIgnoreCase("toggle")) {
|
||||
if (!(sender instanceof Player player)) {
|
||||
sender.sendMessage(Component.text("Only players can use this command"));
|
||||
return true;
|
||||
}
|
||||
if (!player.hasPermission("notjustnameplates.command.toggle")) {
|
||||
player.sendMessage(Component.text("You do not have permission to use this command"));
|
||||
return true;
|
||||
}
|
||||
NameplateManager nameplateManager = NotJustNameplates.getInstance().getNameplateManager();
|
||||
nameplateManager.nameplatesToggle(player, !nameplateManager.getPlayersWithNameplatesOff().contains(player.getUniqueId()));
|
||||
sender.sendMessage(Component.text("Toggled nameplates"));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -47,6 +64,9 @@ public class MainCommand extends Command {
|
||||
if (args.length == 1 && sender.hasPermission("notjustnameplates.command.reload")) {
|
||||
completions.add("reload");
|
||||
}
|
||||
if (args.length == 1 && sender.hasPermission("notjustnameplates.command.toggle")) {
|
||||
completions.add("toggle");
|
||||
}
|
||||
return StringUtil.copyPartialMatches(args[args.length - 1], completions, new ArrayList<>());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ import org.bukkit.potion.PotionEffectType;
|
||||
import org.bukkit.util.Transformation;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.joml.AxisAngle4f;
|
||||
import org.joml.Vector3f;
|
||||
import org.kitteh.vanish.VanishPlugin;
|
||||
|
||||
import java.util.UUID;
|
||||
@@ -29,7 +28,7 @@ public class Nameplate {
|
||||
|
||||
protected boolean forceHide = false;
|
||||
private DisplayContent content;
|
||||
private final UUID playerUuid;
|
||||
public final UUID playerUuid;
|
||||
private TextDisplay.TextAlignment alignment = TextDisplay.TextAlignment.CENTER;
|
||||
private boolean visibleForOwner = false;
|
||||
|
||||
@@ -80,6 +79,14 @@ public class Nameplate {
|
||||
content.getCurrentFrame().scale(),
|
||||
new AxisAngle4f(0, 0, 0, 0) // right rotation
|
||||
));
|
||||
|
||||
NotJustNameplates.getInstance().getNameplateManager().getPlayersWithNameplatesOff().forEach(uuid -> {
|
||||
Player playerWithNameplatesOff = Bukkit.getPlayer(uuid);
|
||||
if (playerWithNameplatesOff != null) {
|
||||
playerWithNameplatesOff.hideEntity(NotJustNameplates.getInstance(), textDisplay);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
if (!this.visibleForOwner) {
|
||||
player.hideEntity(NotJustNameplates.getInstance(), textDisplay);
|
||||
|
||||
@@ -14,14 +14,12 @@ import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.*;
|
||||
import org.purpurmc.purpur.event.entity.EntityTeleportHinderedEvent;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
|
||||
public class NameplateManager implements Listener {
|
||||
|
||||
private final HashMap<UUID, Nameplate> nameplates = new HashMap<>();
|
||||
private final Map<UUID, Nameplate> nameplates = new HashMap<>();
|
||||
private final Set<UUID> playersWithNameplatesOff = new HashSet<>();
|
||||
|
||||
public NameplateManager(NotJustNameplates plugin) {
|
||||
reloadNameplates();
|
||||
@@ -121,4 +119,33 @@ public class NameplateManager implements Listener {
|
||||
return Collections.unmodifiableMap(nameplates);
|
||||
}
|
||||
|
||||
public Set<UUID> getPlayersWithNameplatesOff() {
|
||||
return playersWithNameplatesOff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide or show nameplates of other players for a player
|
||||
* @param player the player to hide nameplates for
|
||||
* @param hide true to hide, false to show
|
||||
*/
|
||||
public void nameplatesToggle(Player player, boolean hide) {
|
||||
if (hide) {
|
||||
playersWithNameplatesOff.add(player.getUniqueId());
|
||||
nameplates.values().forEach(nameplate -> {
|
||||
if (nameplate.playerUuid.equals(player.getUniqueId())) return;
|
||||
TextDisplay display = nameplate.getEntity();
|
||||
if (display == null) return;
|
||||
player.hideEntity(NotJustNameplates.getInstance(), display);
|
||||
});
|
||||
return;
|
||||
}
|
||||
playersWithNameplatesOff.remove(player.getUniqueId());
|
||||
nameplates.values().forEach(nameplate -> {
|
||||
if (nameplate.playerUuid.equals(player.getUniqueId())) return;
|
||||
TextDisplay display = nameplate.getEntity();
|
||||
if (display == null) return;
|
||||
player.showEntity(NotJustNameplates.getInstance(), display);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user