From f7fbefec82b1d11902c2a997cfa479099669b128 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 18 Jun 2022 14:15:48 +0200 Subject: [PATCH] Make Tablist softreloadable Signed-off-by: Lixfel --- .../steamwar/bungeecore/tablist/Tablist.java | 20 ++++++++++--------- .../bungeecore/tablist/TablistManager.java | 3 +++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/de/steamwar/bungeecore/tablist/Tablist.java b/src/de/steamwar/bungeecore/tablist/Tablist.java index c8a47e5..a382c8d 100644 --- a/src/de/steamwar/bungeecore/tablist/Tablist.java +++ b/src/de/steamwar/bungeecore/tablist/Tablist.java @@ -19,6 +19,7 @@ package de.steamwar.bungeecore.tablist; +import de.steamwar.bungeecore.Storage; import de.steamwar.messages.ChatSender; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; @@ -48,7 +49,7 @@ public class Tablist extends MessageToMessageDecoder { private static final String TAB_TEAM = "»SW-Tab"; private static final Team teamPacket = new Team(TAB_TEAM, (byte) 0, ComponentSerializer.toString(TextComponent.fromLegacyText("")), ComponentSerializer.toString(TextComponent.fromLegacyText("")), ComponentSerializer.toString(TextComponent.fromLegacyText("")), "never", "never", 21, (byte)0x00, names); - private final Map directlySent = new HashMap<>(); //TODO Persist over softreload + private final Map directTabItems; private final Set npcs = new HashSet<>(); private final List current = new ArrayList<>(); @@ -59,6 +60,7 @@ public class Tablist extends MessageToMessageDecoder { public Tablist(ProxiedPlayer player) { this.player = player; this.viewer = ChatSender.of(player); + this.directTabItems = Storage.directTabItems.computeIfAbsent(player, p -> new HashMap<>()); onServerSwitch(); } @@ -81,9 +83,9 @@ public class Tablist extends MessageToMessageDecoder { List update = new ArrayList<>(); for (TablistPart.Item item : direct) { if(npcs.remove(item.getUuid())) - removeNpc.add(directlySent.get(item.getUuid()).getUsername()); + removeNpc.add(directTabItems.get(item.getUuid()).getUsername()); - PlayerListItem.Item tabItem = directlySent.get(item.getUuid()); + PlayerListItem.Item tabItem = directTabItems.get(item.getUuid()); if(tabItem == null) { tablist.add(0, item); } else if(!tabItem.getDisplayName().equals(item.getDisplayName())) { @@ -92,7 +94,7 @@ public class Tablist extends MessageToMessageDecoder { } } Set nonNPCs = direct.stream().map(TablistPart.Item::getUuid).collect(Collectors.toSet()); - for(PlayerListItem.Item item : directlySent.values()) { + for(PlayerListItem.Item item : directTabItems.values()) { if(!nonNPCs.contains(item.getUuid()) && !npcs.contains(item.getUuid())) addNpc.add(item.getUsername()); } @@ -138,8 +140,8 @@ public class Tablist extends MessageToMessageDecoder { public void onServerSwitch() { connection = (ServerConnection) player.getServer(); - directlySent.clear(); - sendNpcPacket(npcs.stream().map(npc -> directlySent.get(npc).getUsername()).collect(Collectors.toList()), true); + directTabItems.clear(); + sendNpcPacket(npcs.stream().map(npc -> directTabItems.get(npc).getUsername()).collect(Collectors.toList()), true); npcs.clear(); if(connection != null) { @@ -151,7 +153,7 @@ public class Tablist extends MessageToMessageDecoder { public void remove() { sendTabPacket(current, PlayerListItem.Action.REMOVE_PLAYER); current.clear(); - sendNpcPacket(npcs.stream().map(npc -> directlySent.get(npc).getUsername()).collect(Collectors.toList()), true); + sendNpcPacket(npcs.stream().map(npc -> directTabItems.get(npc).getUsername()).collect(Collectors.toList()), true); npcs.clear(); } @@ -189,12 +191,12 @@ public class Tablist extends MessageToMessageDecoder { if(!player.getUniqueId().equals(item.getUuid()) && item.getGamemode() == 3) item.setGamemode(1); - directlySent.put(item.getUuid(), item); + directTabItems.put(item.getUuid(), item); } break; case REMOVE_PLAYER: for(PlayerListItem.Item item : list.getItems()) { - directlySent.remove(item.getUuid()); + directTabItems.remove(item.getUuid()); npcs.remove(item.getUuid()); } break; diff --git a/src/de/steamwar/bungeecore/tablist/TablistManager.java b/src/de/steamwar/bungeecore/tablist/TablistManager.java index e8a0b08..5039ad6 100644 --- a/src/de/steamwar/bungeecore/tablist/TablistManager.java +++ b/src/de/steamwar/bungeecore/tablist/TablistManager.java @@ -21,6 +21,7 @@ package de.steamwar.bungeecore.tablist; import de.steamwar.bungeecore.BungeeCore; import de.steamwar.bungeecore.Servertype; +import de.steamwar.bungeecore.Storage; import de.steamwar.bungeecore.Subserver; import de.steamwar.bungeecore.listeners.BasicListener; import de.steamwar.network.packets.common.FightInfoPacket; @@ -56,6 +57,7 @@ public class TablistManager extends BasicListener { @EventHandler public void onJoin(PostLoginEvent event) { synchronized (tablists) { + Storage.directTabItems.put(event.getPlayer(), new HashMap<>()); tablists.put(event.getPlayer(), new Tablist(event.getPlayer())); } } @@ -71,6 +73,7 @@ public class TablistManager extends BasicListener { public void onLeave(PlayerDisconnectEvent event) { synchronized (tablists) { tablists.remove(event.getPlayer()); + Storage.directTabItems.remove(event.getPlayer()); } }