mirror of
https://github.com/YouHaveTrouble/NotJustNameplates.git
synced 2026-05-11 22:16:57 +00:00
refactor
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
package me.youhavetrouble.notjustnameplates;
|
package me.youhavetrouble.notjustnameplates;
|
||||||
|
|
||||||
import me.youhavetrouble.notjustnameplates.commands.MainCommand;
|
import me.youhavetrouble.notjustnameplates.commands.MainCommand;
|
||||||
import me.youhavetrouble.notjustnameplates.nameplates.TeamManagementListener;
|
import me.youhavetrouble.notjustnameplates.nameplates.NameplateManager;
|
||||||
import org.bukkit.permissions.PermissionDefault;
|
import org.bukkit.permissions.PermissionDefault;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
import org.bukkit.util.permissions.DefaultPermissions;
|
import org.bukkit.util.permissions.DefaultPermissions;
|
||||||
@@ -12,7 +12,8 @@ public final class NotJustNameplates extends JavaPlugin {
|
|||||||
private static NJNConfig config;
|
private static NJNConfig config;
|
||||||
private static long time = Long.MIN_VALUE;
|
private static long time = Long.MIN_VALUE;
|
||||||
|
|
||||||
private TeamManagementListener teamManagementListener = null;
|
private final TeamManager teamManager = new TeamManager();
|
||||||
|
private NameplateManager nameplateManager = null;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
@@ -23,9 +24,9 @@ public final class NotJustNameplates extends JavaPlugin {
|
|||||||
DefaultPermissions.registerPermission("notjustnameplates.command", "Allows a player to use the /njn command", PermissionDefault.TRUE);
|
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.reload", "Allows a player to use the /njn reload command", PermissionDefault.OP);
|
||||||
|
|
||||||
this.teamManagementListener = new TeamManagementListener(this);
|
this.nameplateManager = new NameplateManager(this);
|
||||||
|
|
||||||
getServer().getPluginManager().registerEvents(this.teamManagementListener, this);
|
getServer().getPluginManager().registerEvents(this.nameplateManager, this);
|
||||||
|
|
||||||
new MainCommand(this);
|
new MainCommand(this);
|
||||||
|
|
||||||
@@ -43,8 +44,15 @@ public final class NotJustNameplates extends JavaPlugin {
|
|||||||
|
|
||||||
public void reloadPluginConfig() {
|
public void reloadPluginConfig() {
|
||||||
config = new NJNConfig(this);
|
config = new NJNConfig(this);
|
||||||
teamManagementListener.reloadTeams();
|
nameplateManager.reloadNameplates();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TeamManager getTeamManager() {
|
||||||
|
return teamManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NameplateManager getNameplateManager() {
|
||||||
|
return nameplateManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NotJustNameplates getInstance() {
|
public static NotJustNameplates getInstance() {
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
package me.youhavetrouble.notjustnameplates;
|
||||||
|
|
||||||
|
import net.minecraft.network.protocol.game.ClientboundSetPlayerTeamPacket;
|
||||||
|
import net.minecraft.world.scores.PlayerTeam;
|
||||||
|
import net.minecraft.world.scores.Scoreboard;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.OfflinePlayer;
|
||||||
|
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.EventPriority;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.player.PlayerJoinEvent;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class TeamManager implements Listener {
|
||||||
|
|
||||||
|
private final Scoreboard scoreboard = new Scoreboard();
|
||||||
|
private final PlayerTeam team = new PlayerTeam(scoreboard, "notjustnameplates");
|
||||||
|
|
||||||
|
private final Set<String> players = new HashSet<>();
|
||||||
|
|
||||||
|
public TeamManager() {
|
||||||
|
team.setNameTagVisibility(PlayerTeam.Visibility.NEVER);
|
||||||
|
|
||||||
|
for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
|
||||||
|
// put player names that played in the last week in the team
|
||||||
|
if (player.getLastSeen() > System.currentTimeMillis() - 604800000L) {
|
||||||
|
players.add(player.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
|
||||||
|
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||||
|
Player player = event.getPlayer();
|
||||||
|
if (!players.add(player.getName())) {
|
||||||
|
Bukkit.getOnlinePlayers().forEach(onlinePlayer -> sendAddPlayerToTeam(player, onlinePlayer));
|
||||||
|
}
|
||||||
|
sendTeamCreatePacket(player);
|
||||||
|
Bukkit.getScheduler().runTaskAsynchronously(NotJustNameplates.getInstance(), () -> sendTeamMembers(player));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendTeamCreatePacket(@NotNull Player target) {
|
||||||
|
CraftPlayer craftPlayer = (CraftPlayer) target;
|
||||||
|
ClientboundSetPlayerTeamPacket teamCreatePacket = ClientboundSetPlayerTeamPacket
|
||||||
|
.createAddOrModifyPacket(team, true);
|
||||||
|
craftPlayer.getHandle().connection.send(teamCreatePacket);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendAddPlayerToTeam(@NotNull Player player, @NotNull Player target) {
|
||||||
|
CraftPlayer craftPlayer = (CraftPlayer) target;
|
||||||
|
ClientboundSetPlayerTeamPacket packet = ClientboundSetPlayerTeamPacket
|
||||||
|
.createPlayerPacket(team, player.getName(), ClientboundSetPlayerTeamPacket.Action.ADD);
|
||||||
|
craftPlayer.getHandle().connection.send(packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendRemovePlayerFromTeam(@NotNull Player playerToRemove, @NotNull Player target) {
|
||||||
|
ClientboundSetPlayerTeamPacket packet = ClientboundSetPlayerTeamPacket
|
||||||
|
.createPlayerPacket(team, playerToRemove.getName(), ClientboundSetPlayerTeamPacket.Action.REMOVE);
|
||||||
|
CraftPlayer craftPlayer = (CraftPlayer) target;
|
||||||
|
craftPlayer.getHandle().connection.send(packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendTeamMembers(@NotNull Player target) {
|
||||||
|
ClientboundSetPlayerTeamPacket packet = ClientboundSetPlayerTeamPacket
|
||||||
|
.createMultiplePlayerPacket(team, players, ClientboundSetPlayerTeamPacket.Action.ADD);
|
||||||
|
CraftPlayer craftPlayer = (CraftPlayer) target;
|
||||||
|
craftPlayer.getHandle().connection.send(packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
package me.youhavetrouble.notjustnameplates.nameplates;
|
||||||
|
|
||||||
|
import me.youhavetrouble.notjustnameplates.NotJustNameplates;
|
||||||
|
import me.youhavetrouble.notjustnameplates.displays.DisplayContent;
|
||||||
|
import net.minecraft.world.phys.AABB;
|
||||||
|
import org.bukkit.*;
|
||||||
|
import org.bukkit.block.Block;
|
||||||
|
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.EventPriority;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.player.*;
|
||||||
|
import org.purpurmc.purpur.event.entity.EntityTeleportHinderedEvent;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class NameplateManager implements Listener {
|
||||||
|
|
||||||
|
private final HashMap<UUID, Nameplate> nameplates = new HashMap<>();
|
||||||
|
|
||||||
|
public NameplateManager(NotJustNameplates plugin) {
|
||||||
|
reloadNameplates();
|
||||||
|
Bukkit.getScheduler().runTaskTimer(plugin, () -> nameplates.values().forEach(Nameplate::update), 0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
||||||
|
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||||
|
UUID joinerUuid = event.getPlayer().getUniqueId();
|
||||||
|
DisplayContent displayContent = NotJustNameplates.getPluginConfig().getDisplayContent("default");
|
||||||
|
if (displayContent == null) return;
|
||||||
|
nameplates.put(joinerUuid, new Nameplate(joinerUuid, displayContent));
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
||||||
|
public void onPlayerTeleport(PlayerTeleportEvent event) {
|
||||||
|
Nameplate nameplate = nameplates.get(event.getPlayer().getUniqueId());
|
||||||
|
if (nameplate == null) return;
|
||||||
|
nameplate.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
||||||
|
public void onPlayerMove(PlayerMoveEvent event) {
|
||||||
|
Player player = event.getPlayer();
|
||||||
|
|
||||||
|
if (!event.hasChangedPosition()) return;
|
||||||
|
|
||||||
|
Nameplate nameplate = nameplates.get(event.getPlayer().getUniqueId());
|
||||||
|
if (nameplate == null) return;
|
||||||
|
|
||||||
|
CraftPlayer craftPlayer = (CraftPlayer) player;
|
||||||
|
AABB playerBox = craftPlayer.getHandle().getBoundingBox();
|
||||||
|
World world = player.getWorld();
|
||||||
|
Location loc1 = new Location(world, playerBox.maxX, playerBox.maxY, playerBox.maxZ);
|
||||||
|
Location loc2 = loc1.clone().subtract(0, 1, 0);
|
||||||
|
Location loc3 = new Location(world, playerBox.minX, playerBox.minY, playerBox.minZ);
|
||||||
|
Location loc4 = loc3.clone().add(0, 1, 0);
|
||||||
|
|
||||||
|
boolean inPortal = false;
|
||||||
|
for (Location loc : new Location[]{loc1, loc2, loc3, loc4}) {
|
||||||
|
Block block = loc.getBlock();
|
||||||
|
if (
|
||||||
|
block.getType() == Material.NETHER_PORTAL
|
||||||
|
|| block.getType() == Material.END_PORTAL
|
||||||
|
|| block.getType() == Material.END_GATEWAY
|
||||||
|
) {
|
||||||
|
inPortal = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inPortal) {
|
||||||
|
nameplate.remove();
|
||||||
|
nameplate.forceHide = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
nameplate.forceHide = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
||||||
|
public void onPlayerTeleportHindered(EntityTeleportHinderedEvent event) {
|
||||||
|
if (!(event.getEntity() instanceof Player player)) return;
|
||||||
|
if (event.getReason() != EntityTeleportHinderedEvent.Reason.IS_VEHICLE) return;
|
||||||
|
Nameplate nameplate = nameplates.get(player.getUniqueId());
|
||||||
|
if (nameplate == null) return;
|
||||||
|
nameplate.remove();
|
||||||
|
event.setShouldRetry(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reloadNameplates() {
|
||||||
|
this.nameplates.values().forEach(Nameplate::remove);
|
||||||
|
this.nameplates.clear();
|
||||||
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||||
|
DisplayContent displayContent = NotJustNameplates.getPluginConfig().getDisplayContent("default");
|
||||||
|
nameplates.put(player.getUniqueId(), new Nameplate(player.getUniqueId(), displayContent != null ? displayContent : new DisplayContent()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
-155
@@ -1,155 +0,0 @@
|
|||||||
package me.youhavetrouble.notjustnameplates.nameplates;
|
|
||||||
|
|
||||||
import me.youhavetrouble.notjustnameplates.NotJustNameplates;
|
|
||||||
import me.youhavetrouble.notjustnameplates.displays.DisplayContent;
|
|
||||||
import net.minecraft.network.protocol.game.ClientboundSetPlayerTeamPacket;
|
|
||||||
import net.minecraft.world.phys.AABB;
|
|
||||||
import net.minecraft.world.scores.PlayerTeam;
|
|
||||||
import net.minecraft.world.scores.Scoreboard;
|
|
||||||
import org.bukkit.*;
|
|
||||||
import org.bukkit.block.Block;
|
|
||||||
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.EventHandler;
|
|
||||||
import org.bukkit.event.EventPriority;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.event.player.*;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.purpurmc.purpur.event.entity.EntityTeleportHinderedEvent;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
public class TeamManagementListener implements Listener {
|
|
||||||
|
|
||||||
private final Scoreboard scoreboard = new Scoreboard();
|
|
||||||
private final PlayerTeam team = new PlayerTeam(scoreboard, "notjustnameplates");
|
|
||||||
private final HashMap<String, Nameplate> players = new HashMap<>();
|
|
||||||
|
|
||||||
public TeamManagementListener(NotJustNameplates plugin) {
|
|
||||||
team.setNameTagVisibility(PlayerTeam.Visibility.NEVER);
|
|
||||||
team.setCollisionRule(PlayerTeam.CollisionRule.ALWAYS);
|
|
||||||
Bukkit.getScheduler().runTaskTimer(plugin, () -> players.values().forEach(Nameplate::update), 0, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
|
||||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
|
||||||
Player joiner = event.getPlayer();
|
|
||||||
DisplayContent displayContent = NotJustNameplates.getPluginConfig().getDisplayContent("default");
|
|
||||||
players.put(joiner.getName(), new Nameplate(joiner.getUniqueId(), displayContent != null ? displayContent : new DisplayContent()));
|
|
||||||
for (Player player : event.getPlayer().getServer().getOnlinePlayers()) {
|
|
||||||
addPlayerToTeam(joiner, player);
|
|
||||||
}
|
|
||||||
sendTeamMembers(joiner);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
|
||||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
|
||||||
Player leaver = event.getPlayer();
|
|
||||||
Nameplate nameplate = players.get(leaver.getName());
|
|
||||||
nameplate.remove();
|
|
||||||
players.remove(leaver.getName());
|
|
||||||
for (Player player : event.getPlayer().getServer().getOnlinePlayers()) {
|
|
||||||
removePlayerFromTeam(leaver, player);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
|
||||||
public void onPlayerTeleport(PlayerTeleportEvent event) {
|
|
||||||
Nameplate nameplate = players.get(event.getPlayer().getName());
|
|
||||||
if (nameplate == null) return;
|
|
||||||
nameplate.remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
|
||||||
public void onPlayerMove(PlayerMoveEvent event) {
|
|
||||||
Player player = event.getPlayer();
|
|
||||||
if (player.getGameMode() == GameMode.SPECTATOR) {
|
|
||||||
Nameplate nameplate = players.get(player.getName());
|
|
||||||
if (nameplate == null) return;
|
|
||||||
nameplate.forceHide = true;
|
|
||||||
nameplate.remove();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!event.hasChangedPosition()) return;
|
|
||||||
|
|
||||||
Nameplate nameplate = players.get(player.getName());
|
|
||||||
if (nameplate == null) return;
|
|
||||||
|
|
||||||
CraftPlayer craftPlayer = (CraftPlayer) player;
|
|
||||||
AABB playerBox = craftPlayer.getHandle().getBoundingBox();
|
|
||||||
World world = player.getWorld();
|
|
||||||
Location loc1 = new Location(world, playerBox.maxX, playerBox.maxY, playerBox.maxZ);
|
|
||||||
Location loc2 = loc1.clone().subtract(0, 1, 0);
|
|
||||||
Location loc3 = new Location(world, playerBox.minX, playerBox.minY, playerBox.minZ);
|
|
||||||
Location loc4 = loc3.clone().add(0, 1, 0);
|
|
||||||
|
|
||||||
boolean inPortal = false;
|
|
||||||
for (Location loc : new Location[]{loc1, loc2, loc3, loc4}) {
|
|
||||||
Block block = loc.getBlock();
|
|
||||||
if (
|
|
||||||
block.getType() == Material.NETHER_PORTAL
|
|
||||||
|| block.getType() == Material.END_PORTAL
|
|
||||||
|| block.getType() == Material.END_GATEWAY
|
|
||||||
) {
|
|
||||||
inPortal = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inPortal) {
|
|
||||||
nameplate.remove();
|
|
||||||
nameplate.forceHide = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
nameplate.forceHide = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
|
||||||
public void onPlayerTeleportHindered(EntityTeleportHinderedEvent event) {
|
|
||||||
if (!(event.getEntity() instanceof Player player)) return;
|
|
||||||
if (event.getReason() != EntityTeleportHinderedEvent.Reason.IS_VEHICLE) return;
|
|
||||||
Nameplate nameplate = players.get(player.getName());
|
|
||||||
if (nameplate == null) return;
|
|
||||||
nameplate.remove();
|
|
||||||
event.setShouldRetry(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void reloadTeams() {
|
|
||||||
this.players.values().forEach(Nameplate::remove);
|
|
||||||
this.players.clear();
|
|
||||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
|
||||||
DisplayContent displayContent = NotJustNameplates.getPluginConfig().getDisplayContent("default");
|
|
||||||
players.put(player.getName(), new Nameplate(player.getUniqueId(), displayContent != null ? displayContent : new DisplayContent()));
|
|
||||||
}
|
|
||||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
|
||||||
sendTeamMembers(player);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addPlayerToTeam(@NotNull Player player, @NotNull Player target) {
|
|
||||||
CraftPlayer craftPlayer = (CraftPlayer) target;
|
|
||||||
ClientboundSetPlayerTeamPacket teamCreatePacket = ClientboundSetPlayerTeamPacket
|
|
||||||
.createAddOrModifyPacket(team, true);
|
|
||||||
craftPlayer.getHandle().connection.send(teamCreatePacket);
|
|
||||||
ClientboundSetPlayerTeamPacket packet = ClientboundSetPlayerTeamPacket
|
|
||||||
.createPlayerPacket(team, player.getName(), ClientboundSetPlayerTeamPacket.Action.ADD);
|
|
||||||
craftPlayer.getHandle().connection.send(packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removePlayerFromTeam(@NotNull Player player, @NotNull Player target) {
|
|
||||||
ClientboundSetPlayerTeamPacket packet = ClientboundSetPlayerTeamPacket
|
|
||||||
.createPlayerPacket(team, player.getName(), ClientboundSetPlayerTeamPacket.Action.REMOVE);
|
|
||||||
CraftPlayer craftPlayer = (CraftPlayer) target;
|
|
||||||
craftPlayer.getHandle().connection.send(packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendTeamMembers(@NotNull Player target) {
|
|
||||||
ClientboundSetPlayerTeamPacket packet = ClientboundSetPlayerTeamPacket
|
|
||||||
.createMultiplePlayerPacket(team, players.keySet(), ClientboundSetPlayerTeamPacket.Action.ADD);
|
|
||||||
CraftPlayer craftPlayer = (CraftPlayer) target;
|
|
||||||
craftPlayer.getHandle().connection.send(packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user