From 9c8ce579863a81fece8511a7ede4e2e151c6c426 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 3 Jul 2024 12:00:55 +0200 Subject: [PATCH] Fix Tablist for 1.21. Signed-off-by: Lixfel --- .../velocitycore/tablist/Tablist.java | 50 ++----------------- .../velocitycore/tablist/TablistManager.java | 10 ++-- 2 files changed, 11 insertions(+), 49 deletions(-) diff --git a/src/de/steamwar/velocitycore/tablist/Tablist.java b/src/de/steamwar/velocitycore/tablist/Tablist.java index 61a62aa..59bba3b 100644 --- a/src/de/steamwar/velocitycore/tablist/Tablist.java +++ b/src/de/steamwar/velocitycore/tablist/Tablist.java @@ -26,12 +26,11 @@ import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.network.Connections; import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.packet.RemovePlayerInfoPacket; -import com.velocitypowered.proxy.protocol.packet.UpdateTeamsPacket; import com.velocitypowered.proxy.protocol.packet.UpsertPlayerInfoPacket; import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder; +import de.steamwar.messages.Chatter; import de.steamwar.persistent.Storage; import de.steamwar.velocitycore.VelocityCore; -import de.steamwar.messages.Chatter; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; @@ -41,19 +40,16 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import java.util.*; -import java.util.stream.Collectors; import java.util.stream.IntStream; @ChannelHandler.Sharable public class Tablist extends ChannelInboundHandlerAdapter { private static final UUID[] swUuids = IntStream.range(0, 80).mapToObj(i -> UUID.randomUUID()).toArray(UUID[]::new); - private static final String[] swNames = IntStream.range(0, 80).mapToObj(i -> " »SW« " + String.format("%02d", i)).toArray(String[]::new); - private static final String TAB_TEAM = "zzzzzsw-tab"; - private static final UpdateTeamsPacket createTeamPacket = new UpdateTeamsPacket(TAB_TEAM, UpdateTeamsPacket.Mode.CREATE, Component.empty(), Component.empty(), Component.empty(), UpdateTeamsPacket.NameTagVisibility.NEVER, UpdateTeamsPacket.CollisionRule.ALWAYS, 21, (byte)0x00, Arrays.stream(swNames).toList()); + public static final String[] swNames = IntStream.range(0, 80).mapToObj(i -> " »SW« " + String.format("%02d", i)).toArray(String[]::new); + public static final String TAB_TEAM = "zzzzzsw-tab"; private final Map directTabItems; - private final Set npcs = new HashSet<>(); private final List current = new ArrayList<>(); private final Player player; @@ -78,17 +74,11 @@ public class Tablist extends ChannelInboundHandlerAdapter { global.print(viewer, player, tablist, direct); // NPC handling - List addNpc = new ArrayList<>(); - List removeNpc = new ArrayList<>(); List update = new ArrayList<>(); - Set nonNPCs = direct.stream().map(TablistPart.Item::getUuid).collect(Collectors.toSet()); synchronized (directTabItems) { for (TablistPart.Item item : direct) { UpsertPlayerInfoPacket.Entry tabItem = directTabItems.get(item.getUuid()); - if(npcs.remove(item.getUuid())) - removeNpc.add(tabItem.getProfile().getName()); - if(tabItem == null) { tablist.add(0, item); } else if(!item.getDisplayName().equals(getDisplayName(tabItem))) { @@ -97,16 +87,7 @@ public class Tablist extends ChannelInboundHandlerAdapter { update.add(tabItem); } } - boolean playerNotOnTeamserver = !Storage.teamServers.containsValue(connection.getServerInfo()); - for(UpsertPlayerInfoPacket.Entry item : directTabItems.values()) { - if(playerNotOnTeamserver && !nonNPCs.contains(item.getProfileId()) && !npcs.contains(item.getProfileId()) && !player.getUniqueId().equals(item.getProfileId())) { - npcs.add(item.getProfileId()); - addNpc.add(item.getProfile().getName()); - } - } } - sendNpcPacket(addNpc, false); - sendNpcPacket(removeNpc, true); // Main list handling int i = 0; @@ -153,9 +134,7 @@ public class Tablist extends ChannelInboundHandlerAdapter { injection(); synchronized (directTabItems) { - sendNpcPacket(npcs.stream().map(npc -> directTabItems.get(npc).getProfile().getName()).toList(), true); directTabItems.clear(); - npcs.clear(); } } @@ -169,16 +148,11 @@ public class Tablist extends ChannelInboundHandlerAdapter { pipeline.remove("steamwar-tablist"); pipeline.addBefore(Connections.HANDLER, "steamwar-tablist", this); - sendPacket(player, createTeamPacket); } public void disable() { sendTabPacket(current, null); current.clear(); - synchronized (directTabItems) { - sendNpcPacket(npcs.stream().map(npc -> directTabItems.get(npc).getProfile().getName()).toList(), true); - npcs.clear(); - } if(connection != null) connection.getConnection().getChannel().pipeline().remove(this); @@ -226,15 +200,11 @@ public class Tablist extends ChannelInboundHandlerAdapter { } } } else if(msg instanceof RemovePlayerInfoPacket packet) { - List names = new ArrayList<>(); for(UUID uuid : packet.getProfilesToRemove()) { synchronized (directTabItems) { - UpsertPlayerInfoPacket.Entry directItem = directTabItems.remove(uuid); - if(npcs.remove(uuid)) - names.add(directItem.getProfile().getName()); + directTabItems.remove(uuid); } } - sendNpcPacket(names, true); } ctx.fireChannelRead(msg); @@ -255,16 +225,6 @@ public class Tablist extends ChannelInboundHandlerAdapter { )); } - private void sendNpcPacket(List names, boolean remove) { - if(!names.isEmpty()) { - UpdateTeamsPacket packet = new UpdateTeamsPacket(); - packet.setName(TAB_TEAM); - packet.setMode(remove ? UpdateTeamsPacket.Mode.REMOVE_PLAYER : UpdateTeamsPacket.Mode.ADD_PLAYER); - packet.setPlayers(names); - sendPacket(player, packet); - } - } - private Component header(Chatter p, int seconds) { int phase = (seconds % 10) / 5; if (phase == 0) @@ -292,7 +252,7 @@ public class Tablist extends ChannelInboundHandlerAdapter { return displayName.getComponent(); } - private static void sendPacket(Player player, MinecraftPacket packet) { + public static void sendPacket(Player player, MinecraftPacket packet) { ((ConnectedPlayer) player).getConnection().write(packet); } } diff --git a/src/de/steamwar/velocitycore/tablist/TablistManager.java b/src/de/steamwar/velocitycore/tablist/TablistManager.java index df0c590..4a8c621 100644 --- a/src/de/steamwar/velocitycore/tablist/TablistManager.java +++ b/src/de/steamwar/velocitycore/tablist/TablistManager.java @@ -25,17 +25,16 @@ import com.velocitypowered.api.event.connection.PostLoginEvent; import com.velocitypowered.api.event.player.ServerPostConnectEvent; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.server.RegisteredServer; +import com.velocitypowered.proxy.protocol.packet.UpdateTeamsPacket; import de.steamwar.persistent.Servertype; import de.steamwar.persistent.Storage; import de.steamwar.persistent.Subserver; import de.steamwar.velocitycore.VelocityCore; import de.steamwar.velocitycore.listeners.BasicListener; import de.steamwar.network.packets.common.FightInfoPacket; +import net.kyori.adventure.text.Component; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.TimeUnit; public class TablistManager extends BasicListener { @@ -47,6 +46,8 @@ public class TablistManager extends BasicListener { fightInfos.keySet().removeIf(serverInfo -> serverInfo.getPlayersConnected().isEmpty()); } + private static final UpdateTeamsPacket createTeamPacket = new UpdateTeamsPacket(Tablist.TAB_TEAM, UpdateTeamsPacket.Mode.CREATE, Component.empty(), Component.empty(), Component.empty(), UpdateTeamsPacket.NameTagVisibility.NEVER, UpdateTeamsPacket.CollisionRule.ALWAYS, 21, (byte)0x00, Arrays.stream(Tablist.swNames).toList()); + private final Map tablists = new HashMap<>(); private int seconds = 0; @@ -60,6 +61,7 @@ public class TablistManager extends BasicListener { @Subscribe public void onJoin(PostLoginEvent event) { + Tablist.sendPacket(event.getPlayer(), createTeamPacket); synchronized (tablists) { tablists.put(event.getPlayer(), new Tablist(event.getPlayer())); }