From dccac81286f7f6c18e03d903901f430766565cde Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Tue, 31 Oct 2023 12:59:38 +1000 Subject: [PATCH] Fix Bungee pipeline clearing, some cleanup --- .../bungee/handlers/BungeeServerHandler.java | 96 ++++++++++--------- .../protocol/ProtocolPipelineImpl.java | 14 ++- .../packets/WorldPackets.java | 2 +- 3 files changed, 67 insertions(+), 45 deletions(-) diff --git a/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java b/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java index b2f732bcc..3f8a3c070 100644 --- a/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java +++ b/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java @@ -54,15 +54,16 @@ import net.md_5.bungee.api.score.Team; import net.md_5.bungee.event.EventHandler; import net.md_5.bungee.protocol.packet.PluginMessage; +// All of this is madness public class BungeeServerHandler implements Listener { - private static Method getHandshake; - private static Method getRegisteredChannels; - private static Method getBrandMessage; - private static Method setProtocol; - private static Method getEntityMap = null; - private static Method setVersion = null; - private static Field entityRewrite = null; - private static Field channelWrapper = null; + private static final Method getHandshake; + private static final Method getRegisteredChannels; + private static final Method getBrandMessage; + private static final Method setProtocol; + private static final Method getEntityMap; + private static final Method setVersion; + private static final Field entityRewrite; + private static final Field channelWrapper; static { try { @@ -76,52 +77,58 @@ public class BungeeServerHandler implements Listener { channelWrapper.setAccessible(true); entityRewrite = Class.forName("net.md_5.bungee.UserConnection").getDeclaredField("entityRewrite"); entityRewrite.setAccessible(true); - } catch (Exception e) { + } catch (ReflectiveOperationException e) { Via.getPlatform().getLogger().severe("Error initializing BungeeServerHandler, try updating BungeeCord or ViaVersion!"); - e.printStackTrace(); + throw new RuntimeException(e); } } // Set the handshake version every time someone connects to any server @EventHandler(priority = 120) - public void onServerConnect(ServerConnectEvent e) { - if (e.isCancelled()) { + public void onServerConnect(ServerConnectEvent event) { + if (event.isCancelled()) { return; } - UserConnection user = Via.getManager().getConnectionManager().getConnectedClient(e.getPlayer().getUniqueId()); - if (user == null) return; - if (!user.has(BungeeStorage.class)) { - user.put(new BungeeStorage(e.getPlayer())); + UserConnection user = Via.getManager().getConnectionManager().getConnectedClient(event.getPlayer().getUniqueId()); + if (user == null) { + return; } - int protocolId = Via.proxyPlatform().protocolDetectorService().serverProtocolVersion(e.getTarget().getName()); - List protocols = Via.getManager().getProtocolManager().getProtocolPath(user.getProtocolInfo().getProtocolVersion(), protocolId); + if (!user.has(BungeeStorage.class)) { + user.put(new BungeeStorage(event.getPlayer())); + } + + int serverProtocolVersion = Via.proxyPlatform().protocolDetectorService().serverProtocolVersion(event.getTarget().getName()); + int clientProtocolVersion = user.getProtocolInfo().getProtocolVersion(); + List protocols = Via.getManager().getProtocolManager().getProtocolPath(clientProtocolVersion, serverProtocolVersion); // Check if ViaVersion can support that version try { - //Object pendingConnection = getPendingConnection.invoke(e.getPlayer()); - Object handshake = getHandshake.invoke(e.getPlayer().getPendingConnection()); - setProtocol.invoke(handshake, protocols == null ? user.getProtocolInfo().getProtocolVersion() : protocolId); - } catch (InvocationTargetException | IllegalAccessException e1) { - e1.printStackTrace(); + Object handshake = getHandshake.invoke(event.getPlayer().getPendingConnection()); + setProtocol.invoke(handshake, protocols == null ? clientProtocolVersion : serverProtocolVersion); + } catch (InvocationTargetException | IllegalAccessException e) { + e.printStackTrace(); } } @EventHandler(priority = -120) - public void onServerConnected(ServerConnectedEvent e) { + public void onServerConnected(ServerConnectedEvent event) { try { - checkServerChange(e, Via.getManager().getConnectionManager().getConnectedClient(e.getPlayer().getUniqueId())); - } catch (Exception e1) { - e1.printStackTrace(); + checkServerChange(event, Via.getManager().getConnectionManager().getConnectedClient(event.getPlayer().getUniqueId())); + } catch (Exception e) { + e.printStackTrace(); } } @EventHandler(priority = -120) - public void onServerSwitch(ServerSwitchEvent e) { + public void onServerSwitch(ServerSwitchEvent event) { // Update entity id - UserConnection userConnection = Via.getManager().getConnectionManager().getConnectedClient(e.getPlayer().getUniqueId()); - if (userConnection == null) return; + UserConnection userConnection = Via.getManager().getConnectionManager().getConnectedClient(event.getPlayer().getUniqueId()); + if (userConnection == null) { + return; + } + int playerId; try { playerId = Via.getManager().getProviders().get(EntityIdProvider.class).getEntityId(userConnection); @@ -142,18 +149,21 @@ public class BungeeServerHandler implements Listener { } public void checkServerChange(ServerConnectedEvent event, UserConnection user) throws Exception { - if (user == null || !user.has(BungeeStorage.class)) { + if (user == null) { return; } - // Auto-team handling - // Handle server/version change BungeeStorage storage = user.get(BungeeStorage.class); + if (storage == null) { + return; + } + Server server = event.getServer(); if (server == null || server.getInfo().getName().equals(storage.getCurrentServer())) { return; } + // Clear auto-team EntityTracker1_9 oldEntityTracker = user.getEntityTracker(Protocol1_9To1_8.class); if (oldEntityTracker != null && oldEntityTracker.isAutoTeam() && oldEntityTracker.isTeamExists()) { @@ -162,8 +172,8 @@ public class BungeeServerHandler implements Listener { String serverName = server.getInfo().getName(); storage.setCurrentServer(serverName); - int protocolId = Via.proxyPlatform().protocolDetectorService().serverProtocolVersion(serverName); - if (protocolId <= ProtocolVersion.v1_8.getVersion() && storage.getBossbar() != null) { // 1.8 doesn't have BossBar packet + int serverProtocolVersion = Via.proxyPlatform().protocolDetectorService().serverProtocolVersion(serverName); + if (serverProtocolVersion <= ProtocolVersion.v1_8.getVersion() && storage.getBossbar() != null) { // 1.8 doesn't have BossBar packet // This ensures we can encode it properly as only the 1.9 protocol is currently implemented. if (user.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) { for (UUID uuid : storage.getBossbar()) { @@ -180,13 +190,13 @@ public class BungeeServerHandler implements Listener { int previousServerProtocol = info.getServerProtocolVersion(); // Refresh the pipes - List protocolPath = Via.getManager().getProtocolManager().getProtocolPath(info.getProtocolVersion(), protocolId); + List protocolPath = Via.getManager().getProtocolManager().getProtocolPath(info.getProtocolVersion(), serverProtocolVersion); ProtocolPipeline pipeline = user.getProtocolInfo().getPipeline(); user.clearStoredObjects(true); pipeline.cleanPipes(); if (protocolPath == null) { // TODO Check Bungee Supported Protocols? *shrugs* - protocolId = info.getProtocolVersion(); + serverProtocolVersion = info.getProtocolVersion(); } else { List protocols = new ArrayList<>(protocolPath.size()); for (ProtocolPathEntry entry : protocolPath) { @@ -195,14 +205,14 @@ public class BungeeServerHandler implements Listener { pipeline.add(protocols); } - info.setServerProtocolVersion(protocolId); + info.setServerProtocolVersion(serverProtocolVersion); // Add version-specific base Protocol - pipeline.add(Via.getManager().getProtocolManager().getBaseProtocol(protocolId)); + pipeline.add(Via.getManager().getProtocolManager().getBaseProtocol(serverProtocolVersion)); // Workaround 1.13 server change int id1_13 = ProtocolVersion.v1_13.getVersion(); - boolean toNewId = previousServerProtocol < id1_13 && protocolId >= id1_13; - boolean toOldId = previousServerProtocol >= id1_13 && protocolId < id1_13; + boolean toNewId = previousServerProtocol < id1_13 && serverProtocolVersion >= id1_13; + boolean toOldId = previousServerProtocol >= id1_13 && serverProtocolVersion < id1_13; if (previousServerProtocol != -1 && (toNewId || toOldId)) { Collection registeredChannels = (Collection) getRegisteredChannels.invoke(event.getPlayer().getPendingConnection()); if (!registeredChannels.isEmpty()) { @@ -274,9 +284,9 @@ public class BungeeServerHandler implements Listener { } Object wrapper = channelWrapper.get(player); - setVersion.invoke(wrapper, protocolId); + setVersion.invoke(wrapper, serverProtocolVersion); - Object entityMap = getEntityMap.invoke(null, protocolId); + Object entityMap = getEntityMap.invoke(null, serverProtocolVersion); entityRewrite.set(player, entityMap); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPipelineImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPipelineImpl.java index 79acfb567..5ea989e43 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPipelineImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPipelineImpl.java @@ -39,7 +39,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements ProtocolPipeline { private final UserConnection userConnection; /** - * Protocol list ordered from client to server transforation with the base protocols at the end. + * Protocol list ordered from client to server transformation with the base protocols at the end. */ private final List protocolList = new CopyOnWriteArrayList<>(); private final Set> protocolSet = new HashSet<>(); @@ -178,6 +178,18 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot @Override public void cleanPipes() { + protocolList.clear(); + reversedProtocolList.clear(); + protocolSet.clear(); + baseProtocols = 0; + registerPackets(); } + + @Override + public String toString() { + return "ProtocolPipelineImpl{" + + "protocolList=" + protocolList + + '}'; + } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java index e86cd58a5..811390671 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java @@ -314,7 +314,7 @@ public class WorldPackets { } static void sendViewDistancePacket(UserConnection connection) throws Exception { - PacketWrapper setViewDistance = PacketWrapper.create(ClientboundPackets1_14.UPDATE_VIEW_DISTANCE, null, connection); + PacketWrapper setViewDistance = PacketWrapper.create(ClientboundPackets1_14.UPDATE_VIEW_DISTANCE, connection); setViewDistance.write(Type.VAR_INT, WorldPackets.SERVERSIDE_VIEW_DISTANCE); setViewDistance.send(Protocol1_14To1_13_2.class); }