From 131cdf1cef62857a070eb1586635cac7f83ce787 Mon Sep 17 00:00:00 2001 From: Tanguygab Date: Sat, 23 Jan 2021 15:00:08 +0100 Subject: [PATCH] reworked nametag in range + added tablist names radius --- .../tabadditions/shared/SharedEvents.java | 8 ++++ .../tabadditions/shared/SharedTA.java | 39 +++++++++++++++++-- .../spigot/Features/NametagInRange.java | 28 +++++-------- .../spigot/Features/TablistNamesRadius.java | 36 +++++++++++++++++ 4 files changed, 90 insertions(+), 21 deletions(-) create mode 100644 src/main/java/io/github/tanguygab/tabadditions/spigot/Features/TablistNamesRadius.java diff --git a/src/main/java/io/github/tanguygab/tabadditions/shared/SharedEvents.java b/src/main/java/io/github/tanguygab/tabadditions/shared/SharedEvents.java index 1e14c4f..733b8c5 100644 --- a/src/main/java/io/github/tanguygab/tabadditions/shared/SharedEvents.java +++ b/src/main/java/io/github/tanguygab/tabadditions/shared/SharedEvents.java @@ -6,6 +6,7 @@ import me.neznamy.tab.api.TabPlayer; import me.neznamy.tab.shared.Shared; import me.neznamy.tab.shared.packets.PacketPlayOutChat; +import me.neznamy.tab.shared.packets.PacketPlayOutPlayerInfo; import java.util.ArrayList; import java.util.List; @@ -41,6 +42,13 @@ public static void JoinEvent(String name) { p.hideNametag(p2.getUniqueId()); p2.hideNametag(p.getUniqueId()); } + if (SharedTA.tablistNamesRadius != 0) + for (TabPlayer p2 : Shared.getPlayers()) { + if (p != p2) { + p.sendCustomPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, new PacketPlayOutPlayerInfo.PlayerInfoData(p2.getUniqueId()))); + p2.sendCustomPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, new PacketPlayOutPlayerInfo.PlayerInfoData(p.getUniqueId()))); + } + } }, 1); } diff --git a/src/main/java/io/github/tanguygab/tabadditions/shared/SharedTA.java b/src/main/java/io/github/tanguygab/tabadditions/shared/SharedTA.java index 612b8db..b10847d 100644 --- a/src/main/java/io/github/tanguygab/tabadditions/shared/SharedTA.java +++ b/src/main/java/io/github/tanguygab/tabadditions/shared/SharedTA.java @@ -8,9 +8,12 @@ import io.github.tanguygab.tabadditions.shared.features.chat.ChatManager; import io.github.tanguygab.tabadditions.shared.features.layouts.LayoutManager; import io.github.tanguygab.tabadditions.spigot.Features.NametagInRange; +import io.github.tanguygab.tabadditions.spigot.Features.TablistNamesRadius; import me.neznamy.tab.api.TabPlayer; import me.neznamy.tab.shared.Shared; import me.neznamy.tab.shared.config.YamlConfigurationFile; +import me.neznamy.tab.shared.packets.IChatBaseComponent; +import me.neznamy.tab.shared.packets.PacketPlayOutPlayerInfo; import org.bukkit.Bukkit; import javax.script.ScriptException; @@ -35,6 +38,7 @@ public class SharedTA { public static boolean layoutEnabled; public static boolean sneakhideEnabled = false; public static int nametagInRange = 0; + public static int tablistNamesRadius = 0; public static Map tasks = new HashMap<>(); public static boolean isCompatible() { @@ -68,6 +72,7 @@ public static void reload(File dataFolder) { if (platform.type().equals("Spigot")) { sneakhideEnabled = config.getBoolean("features.sneak-hide-nametags", false); nametagInRange = config.getInt("features.nametag-in-range", 0); + tablistNamesRadius = config.getInt("features.tablist-names-radius", 0); } for (TabPlayer p : Shared.getPlayers()) { @@ -77,6 +82,8 @@ public static void reload(File dataFolder) { loadLayout(); loadChat(); loadNametagInRange(); + loadTablistNamesRadius(); + refresh(); } catch (IOException e) { e.printStackTrace(); @@ -90,6 +97,7 @@ protected static void loadProps(TabPlayer p) { p.loadPropertyFromConfig("customchatname", p.getName()); p.loadPropertyFromConfig("chatsuffix"); } + private static void loadLists() { List temp = new ArrayList<>(); for (Object key : actionbarConfig.getConfigurationSection("bars").keySet()) @@ -122,11 +130,13 @@ private static void loadChat() { } private static void loadNametagInRange() { if (nametagInRange != 0) { - for (TabPlayer p : Shared.getPlayers()) - for (TabPlayer p2 : Shared.getPlayers()) + for (TabPlayer p : Shared.getPlayers()) { + for (TabPlayer p2 : Shared.getPlayers()) { if (p != p2) p.hideNametag(p2.getUniqueId()); - if (tasks.containsKey("Nametag-In-Range")) { + } + } + if (!tasks.containsKey("Nametag-In-Range")) { tasks.put("Nametag-In-Range",new NametagInRange().load()); } } else if (tasks.containsKey("Nametag-In-Range")) { @@ -138,6 +148,29 @@ private static void loadNametagInRange() { p.showNametag(p2.getUniqueId()); } } + private static void loadTablistNamesRadius() { + if (tablistNamesRadius != 0) { + if (tablistNamesRadius < 100) + tablistNamesRadius = 100; + for (TabPlayer p : Shared.getPlayers()) { + for (TabPlayer p2 : Shared.getPlayers()) { + if (p != p2) + p.sendCustomPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, new PacketPlayOutPlayerInfo.PlayerInfoData(p2.getUniqueId()))); + } + } + if (!tasks.containsKey("Tablist-Names-Radius")) { + tasks.put("Tablist-Names-Radius",new TablistNamesRadius().load()); + } + } else if (tasks.containsKey("Tablist-Names-Radius")) { + Bukkit.getServer().getScheduler().cancelTask(tasks.get("Tablist-Names-Radius")); + tasks.remove("Tablist-Names-Radius"); + for (TabPlayer p : Shared.getPlayers()) + for (TabPlayer p2 : Shared.getPlayers()) + if (p != p2) + p.sendCustomPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, new PacketPlayOutPlayerInfo.PlayerInfoData(p2.getUniqueId()))); + } + + } private static void refresh() { tasks.put("Global-Refresh",platform.AsyncTask(()-> { diff --git a/src/main/java/io/github/tanguygab/tabadditions/spigot/Features/NametagInRange.java b/src/main/java/io/github/tanguygab/tabadditions/spigot/Features/NametagInRange.java index 6b3a5d4..a0f4335 100644 --- a/src/main/java/io/github/tanguygab/tabadditions/spigot/Features/NametagInRange.java +++ b/src/main/java/io/github/tanguygab/tabadditions/spigot/Features/NametagInRange.java @@ -14,28 +14,20 @@ public class NametagInRange { - private final Map> list = new HashMap<>(); - public int load() { - return Bukkit.getScheduler().scheduleSyncRepeatingTask((Plugin) SharedTA.plugin, () -> { - int range = SharedTA.nametagInRange; + return Bukkit.getScheduler().scheduleAsyncRepeatingTask((Plugin) SharedTA.plugin, () -> { + int zone = (int) Math.pow(SharedTA.nametagInRange, 2); for (Player p : Bukkit.getServer().getOnlinePlayers()) { - ArrayList entities = (ArrayList) p.getNearbyEntities(range, range, range); - entities.removeIf(entity -> !(entity instanceof Player)); - for (Entity otherp : entities) { - Shared.getPlayer(otherp.getUniqueId()).showNametag(p.getUniqueId()); - list.put(p,entities); + for (Player player : Bukkit.getServer().getOnlinePlayers()) { + + if (p != player && p.getWorld().equals(player.getWorld()) && player.getLocation().distanceSquared(p.getLocation()) < zone) { + Shared.getPlayer(player.getUniqueId()).showNametag(p.getUniqueId()); + } + else if (Shared.getPlayer(player.getUniqueId()) != null) { + Shared.getPlayer(player.getUniqueId()).hideNametag(p.getUniqueId()); + } } - if (list.containsKey(p)) - list.get(p).removeIf(entity -> { - if (!entities.contains(entity)) { - if (Shared.getPlayer(entity.getUniqueId()) != null) - Shared.getPlayer(entity.getUniqueId()).hideNametag(p.getUniqueId()); - return true; - } - return false; - }); } }, 0L, 10L); } diff --git a/src/main/java/io/github/tanguygab/tabadditions/spigot/Features/TablistNamesRadius.java b/src/main/java/io/github/tanguygab/tabadditions/spigot/Features/TablistNamesRadius.java new file mode 100644 index 0000000..b6ec307 --- /dev/null +++ b/src/main/java/io/github/tanguygab/tabadditions/spigot/Features/TablistNamesRadius.java @@ -0,0 +1,36 @@ +package io.github.tanguygab.tabadditions.spigot.Features; + +import io.github.tanguygab.tabadditions.shared.SharedTA; +import me.neznamy.tab.api.TabPlayer; +import me.neznamy.tab.shared.Shared; +import me.neznamy.tab.shared.packets.PacketPlayOutPlayerInfo; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class TablistNamesRadius { + + public int load() { + + return Bukkit.getScheduler().scheduleAsyncRepeatingTask((Plugin) SharedTA.plugin, () -> { + int zone = (int) Math.pow(SharedTA.tablistNamesRadius, 2); + for (Player p : Bukkit.getServer().getOnlinePlayers()) { + for (Player player : Bukkit.getServer().getOnlinePlayers()) { + + if (p != player && p.getWorld().equals(player.getWorld()) && player.getLocation().distanceSquared(p.getLocation()) < zone) { + TabPlayer p2 = Shared.getPlayer(player.getUniqueId()); + Shared.getPlayer(p.getUniqueId()).sendCustomPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, new PacketPlayOutPlayerInfo.PlayerInfoData(p2.getName(), p2.getTablistUUID(), p2.getSkin(), (int)p2.getPing(), PacketPlayOutPlayerInfo.EnumGamemode.CREATIVE, null))); + } + else if (p != player) { + Shared.getPlayer(p.getUniqueId()).sendCustomPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, new PacketPlayOutPlayerInfo.PlayerInfoData(player.getUniqueId()))); + } + } + } + }, 0L, 10L); + } +}