From 9aa7f5e879e6defb4456217b7952ddfdee5c546f Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Thu, 19 May 2022 12:30:21 +0200 Subject: [PATCH] Fix data stored on join being lost on Bungee Bungee doesn't resend a join game for old servers, so we will have to keep data stored there across server switches This likely fixes other issues with chunk data writing after server switches on legacy servers as well --- .../api/connection/StorableObject.java | 9 +++++++++ .../api/connection/UserConnection.java | 14 ++++++++++++-- .../bungee/handlers/BungeeServerHandler.java | 2 +- .../connection/UserConnectionImpl.java | 17 +++++++++++++---- .../Protocol1_19To1_18_2.java | 4 +++- .../storage/DimensionRegistryStorage.java | 5 +++++ 6 files changed, 43 insertions(+), 8 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/connection/StorableObject.java b/api/src/main/java/com/viaversion/viaversion/api/connection/StorableObject.java index 1d7a41bb3..69dda615f 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/connection/StorableObject.java +++ b/api/src/main/java/com/viaversion/viaversion/api/connection/StorableObject.java @@ -29,4 +29,13 @@ package com.viaversion.viaversion.api.connection; * @see UserConnection#put(StorableObject) */ public interface StorableObject { + + /** + * Returns whether the object should be uncached on a server switch. + * + * @return whether the object should be uncached on a server switch + */ + default boolean clearOnServerSwitch() { + return true; + } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/connection/UserConnection.java b/api/src/main/java/com/viaversion/viaversion/api/connection/UserConnection.java index 721bd0eac..5fa9d7d16 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/connection/UserConnection.java +++ b/api/src/main/java/com/viaversion/viaversion/api/connection/UserConnection.java @@ -92,6 +92,7 @@ public interface UserConnection { /** * Adds an entity tracker to the user connection. + * Does not override existing entity trackers. * * @param protocolClass protocol class * @param tracker entity tracker @@ -100,9 +101,18 @@ public interface UserConnection { /** * Clear stored objects and entity trackers. - * Used for Bungee when switching servers. */ - void clearStoredObjects(); + default void clearStoredObjects() { + clearStoredObjects(false); + } + + /** + * Clear stored objects and entity trackers. + * If cleared for a proxy server switch, some stored objects and tracker data will be retained. + * + * @param isServerSwitch whether the clear is due to a server switch + */ + void clearStoredObjects(boolean isServerSwitch); /** * Sends a raw packet to the connection on the current thread. 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 95ea74873..5250b8082 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 @@ -184,7 +184,7 @@ public class BungeeServerHandler implements Listener { // Refresh the pipes List protocolPath = Via.getManager().getProtocolManager().getProtocolPath(info.getProtocolVersion(), protocolId); ProtocolPipeline pipeline = user.getProtocolInfo().getPipeline(); - user.clearStoredObjects(); + user.clearStoredObjects(true); pipeline.cleanPipes(); if (protocolPath == null) { // TODO Check Bungee Supported Protocols? *shrugs* diff --git a/common/src/main/java/com/viaversion/viaversion/connection/UserConnectionImpl.java b/common/src/main/java/com/viaversion/viaversion/connection/UserConnectionImpl.java index cab851d3f..9d2eb49f8 100644 --- a/common/src/main/java/com/viaversion/viaversion/connection/UserConnectionImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/connection/UserConnectionImpl.java @@ -115,13 +115,22 @@ public class UserConnectionImpl implements UserConnection { @Override public void addEntityTracker(Class protocolClass, EntityTracker tracker) { - entityTrackers.put(protocolClass, tracker); + if (!entityTrackers.containsKey(protocolClass)) { + entityTrackers.put(protocolClass, tracker); + } } @Override - public void clearStoredObjects() { - storedObjects.clear(); - entityTrackers.clear(); + public void clearStoredObjects(boolean isServerSwitch) { + if (isServerSwitch) { + storedObjects.values().removeIf(StorableObject::clearOnServerSwitch); + for (EntityTracker tracker : entityTrackers.values()) { + tracker.clearEntities(); + } + } else { + storedObjects.clear(); + entityTrackers.clear(); + } } @Override diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/Protocol1_19To1_18_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/Protocol1_19To1_18_2.java index 09735a366..4911ce38d 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/Protocol1_19To1_18_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_19to1_18_2/Protocol1_19To1_18_2.java @@ -287,7 +287,9 @@ public final class Protocol1_19To1_18_2 extends AbstractProtocol dimensions) { this.dimensions = dimensions; } + + @Override + public boolean clearOnServerSwitch() { + return false; + } }