Fix Bungee pipeline clearing, some cleanup

This commit is contained in:
Nassim Jahnke 2023-10-31 12:59:38 +10:00
parent f9ab1bd811
commit dccac81286
3 changed files with 67 additions and 45 deletions

View File

@ -54,15 +54,16 @@ import net.md_5.bungee.api.score.Team;
import net.md_5.bungee.event.EventHandler; import net.md_5.bungee.event.EventHandler;
import net.md_5.bungee.protocol.packet.PluginMessage; import net.md_5.bungee.protocol.packet.PluginMessage;
// All of this is madness
public class BungeeServerHandler implements Listener { public class BungeeServerHandler implements Listener {
private static Method getHandshake; private static final Method getHandshake;
private static Method getRegisteredChannels; private static final Method getRegisteredChannels;
private static Method getBrandMessage; private static final Method getBrandMessage;
private static Method setProtocol; private static final Method setProtocol;
private static Method getEntityMap = null; private static final Method getEntityMap;
private static Method setVersion = null; private static final Method setVersion;
private static Field entityRewrite = null; private static final Field entityRewrite;
private static Field channelWrapper = null; private static final Field channelWrapper;
static { static {
try { try {
@ -76,52 +77,58 @@ public class BungeeServerHandler implements Listener {
channelWrapper.setAccessible(true); channelWrapper.setAccessible(true);
entityRewrite = Class.forName("net.md_5.bungee.UserConnection").getDeclaredField("entityRewrite"); entityRewrite = Class.forName("net.md_5.bungee.UserConnection").getDeclaredField("entityRewrite");
entityRewrite.setAccessible(true); entityRewrite.setAccessible(true);
} catch (Exception e) { } catch (ReflectiveOperationException e) {
Via.getPlatform().getLogger().severe("Error initializing BungeeServerHandler, try updating BungeeCord or ViaVersion!"); 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 // Set the handshake version every time someone connects to any server
@EventHandler(priority = 120) @EventHandler(priority = 120)
public void onServerConnect(ServerConnectEvent e) { public void onServerConnect(ServerConnectEvent event) {
if (e.isCancelled()) { if (event.isCancelled()) {
return; return;
} }
UserConnection user = Via.getManager().getConnectionManager().getConnectedClient(e.getPlayer().getUniqueId()); UserConnection user = Via.getManager().getConnectionManager().getConnectedClient(event.getPlayer().getUniqueId());
if (user == null) return; if (user == null) {
if (!user.has(BungeeStorage.class)) { return;
user.put(new BungeeStorage(e.getPlayer()));
} }
int protocolId = Via.proxyPlatform().protocolDetectorService().serverProtocolVersion(e.getTarget().getName()); if (!user.has(BungeeStorage.class)) {
List<ProtocolPathEntry> protocols = Via.getManager().getProtocolManager().getProtocolPath(user.getProtocolInfo().getProtocolVersion(), protocolId); user.put(new BungeeStorage(event.getPlayer()));
}
int serverProtocolVersion = Via.proxyPlatform().protocolDetectorService().serverProtocolVersion(event.getTarget().getName());
int clientProtocolVersion = user.getProtocolInfo().getProtocolVersion();
List<ProtocolPathEntry> protocols = Via.getManager().getProtocolManager().getProtocolPath(clientProtocolVersion, serverProtocolVersion);
// Check if ViaVersion can support that version // Check if ViaVersion can support that version
try { try {
//Object pendingConnection = getPendingConnection.invoke(e.getPlayer()); Object handshake = getHandshake.invoke(event.getPlayer().getPendingConnection());
Object handshake = getHandshake.invoke(e.getPlayer().getPendingConnection()); setProtocol.invoke(handshake, protocols == null ? clientProtocolVersion : serverProtocolVersion);
setProtocol.invoke(handshake, protocols == null ? user.getProtocolInfo().getProtocolVersion() : protocolId); } catch (InvocationTargetException | IllegalAccessException e) {
} catch (InvocationTargetException | IllegalAccessException e1) { e.printStackTrace();
e1.printStackTrace();
} }
} }
@EventHandler(priority = -120) @EventHandler(priority = -120)
public void onServerConnected(ServerConnectedEvent e) { public void onServerConnected(ServerConnectedEvent event) {
try { try {
checkServerChange(e, Via.getManager().getConnectionManager().getConnectedClient(e.getPlayer().getUniqueId())); checkServerChange(event, Via.getManager().getConnectionManager().getConnectedClient(event.getPlayer().getUniqueId()));
} catch (Exception e1) { } catch (Exception e) {
e1.printStackTrace(); e.printStackTrace();
} }
} }
@EventHandler(priority = -120) @EventHandler(priority = -120)
public void onServerSwitch(ServerSwitchEvent e) { public void onServerSwitch(ServerSwitchEvent event) {
// Update entity id // Update entity id
UserConnection userConnection = Via.getManager().getConnectionManager().getConnectedClient(e.getPlayer().getUniqueId()); UserConnection userConnection = Via.getManager().getConnectionManager().getConnectedClient(event.getPlayer().getUniqueId());
if (userConnection == null) return; if (userConnection == null) {
return;
}
int playerId; int playerId;
try { try {
playerId = Via.getManager().getProviders().get(EntityIdProvider.class).getEntityId(userConnection); 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 { public void checkServerChange(ServerConnectedEvent event, UserConnection user) throws Exception {
if (user == null || !user.has(BungeeStorage.class)) { if (user == null) {
return; return;
} }
// Auto-team handling
// Handle server/version change
BungeeStorage storage = user.get(BungeeStorage.class); BungeeStorage storage = user.get(BungeeStorage.class);
if (storage == null) {
return;
}
Server server = event.getServer(); Server server = event.getServer();
if (server == null || server.getInfo().getName().equals(storage.getCurrentServer())) { if (server == null || server.getInfo().getName().equals(storage.getCurrentServer())) {
return; return;
} }
// Clear auto-team // Clear auto-team
EntityTracker1_9 oldEntityTracker = user.getEntityTracker(Protocol1_9To1_8.class); EntityTracker1_9 oldEntityTracker = user.getEntityTracker(Protocol1_9To1_8.class);
if (oldEntityTracker != null && oldEntityTracker.isAutoTeam() && oldEntityTracker.isTeamExists()) { if (oldEntityTracker != null && oldEntityTracker.isAutoTeam() && oldEntityTracker.isTeamExists()) {
@ -162,8 +172,8 @@ public class BungeeServerHandler implements Listener {
String serverName = server.getInfo().getName(); String serverName = server.getInfo().getName();
storage.setCurrentServer(serverName); storage.setCurrentServer(serverName);
int protocolId = Via.proxyPlatform().protocolDetectorService().serverProtocolVersion(serverName); int serverProtocolVersion = Via.proxyPlatform().protocolDetectorService().serverProtocolVersion(serverName);
if (protocolId <= ProtocolVersion.v1_8.getVersion() && storage.getBossbar() != null) { // 1.8 doesn't have BossBar packet 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. // 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)) { if (user.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) {
for (UUID uuid : storage.getBossbar()) { for (UUID uuid : storage.getBossbar()) {
@ -180,13 +190,13 @@ public class BungeeServerHandler implements Listener {
int previousServerProtocol = info.getServerProtocolVersion(); int previousServerProtocol = info.getServerProtocolVersion();
// Refresh the pipes // Refresh the pipes
List<ProtocolPathEntry> protocolPath = Via.getManager().getProtocolManager().getProtocolPath(info.getProtocolVersion(), protocolId); List<ProtocolPathEntry> protocolPath = Via.getManager().getProtocolManager().getProtocolPath(info.getProtocolVersion(), serverProtocolVersion);
ProtocolPipeline pipeline = user.getProtocolInfo().getPipeline(); ProtocolPipeline pipeline = user.getProtocolInfo().getPipeline();
user.clearStoredObjects(true); user.clearStoredObjects(true);
pipeline.cleanPipes(); pipeline.cleanPipes();
if (protocolPath == null) { if (protocolPath == null) {
// TODO Check Bungee Supported Protocols? *shrugs* // TODO Check Bungee Supported Protocols? *shrugs*
protocolId = info.getProtocolVersion(); serverProtocolVersion = info.getProtocolVersion();
} else { } else {
List<Protocol> protocols = new ArrayList<>(protocolPath.size()); List<Protocol> protocols = new ArrayList<>(protocolPath.size());
for (ProtocolPathEntry entry : protocolPath) { for (ProtocolPathEntry entry : protocolPath) {
@ -195,14 +205,14 @@ public class BungeeServerHandler implements Listener {
pipeline.add(protocols); pipeline.add(protocols);
} }
info.setServerProtocolVersion(protocolId); info.setServerProtocolVersion(serverProtocolVersion);
// Add version-specific base Protocol // Add version-specific base Protocol
pipeline.add(Via.getManager().getProtocolManager().getBaseProtocol(protocolId)); pipeline.add(Via.getManager().getProtocolManager().getBaseProtocol(serverProtocolVersion));
// Workaround 1.13 server change // Workaround 1.13 server change
int id1_13 = ProtocolVersion.v1_13.getVersion(); int id1_13 = ProtocolVersion.v1_13.getVersion();
boolean toNewId = previousServerProtocol < id1_13 && protocolId >= id1_13; boolean toNewId = previousServerProtocol < id1_13 && serverProtocolVersion >= id1_13;
boolean toOldId = previousServerProtocol >= id1_13 && protocolId < id1_13; boolean toOldId = previousServerProtocol >= id1_13 && serverProtocolVersion < id1_13;
if (previousServerProtocol != -1 && (toNewId || toOldId)) { if (previousServerProtocol != -1 && (toNewId || toOldId)) {
Collection<String> registeredChannels = (Collection<String>) getRegisteredChannels.invoke(event.getPlayer().getPendingConnection()); Collection<String> registeredChannels = (Collection<String>) getRegisteredChannels.invoke(event.getPlayer().getPendingConnection());
if (!registeredChannels.isEmpty()) { if (!registeredChannels.isEmpty()) {
@ -274,9 +284,9 @@ public class BungeeServerHandler implements Listener {
} }
Object wrapper = channelWrapper.get(player); 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); entityRewrite.set(player, entityMap);
} }
} }

View File

@ -39,7 +39,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements ProtocolPipeline { public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements ProtocolPipeline {
private final UserConnection userConnection; 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<Protocol> protocolList = new CopyOnWriteArrayList<>(); private final List<Protocol> protocolList = new CopyOnWriteArrayList<>();
private final Set<Class<? extends Protocol>> protocolSet = new HashSet<>(); private final Set<Class<? extends Protocol>> protocolSet = new HashSet<>();
@ -178,6 +178,18 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot
@Override @Override
public void cleanPipes() { public void cleanPipes() {
protocolList.clear();
reversedProtocolList.clear();
protocolSet.clear();
baseProtocols = 0;
registerPackets(); registerPackets();
} }
@Override
public String toString() {
return "ProtocolPipelineImpl{" +
"protocolList=" + protocolList +
'}';
}
} }

View File

@ -314,7 +314,7 @@ public class WorldPackets {
} }
static void sendViewDistancePacket(UserConnection connection) throws Exception { 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.write(Type.VAR_INT, WorldPackets.SERVERSIDE_VIEW_DISTANCE);
setViewDistance.send(Protocol1_14To1_13_2.class); setViewDistance.send(Protocol1_14To1_13_2.class);
} }