From 87afb84a4d90dab37642a7efdb979c313cc87a1e Mon Sep 17 00:00:00 2001 From: KennyTV Date: Tue, 30 Jun 2020 13:56:38 +0200 Subject: [PATCH] Fix server switching to 1.16 servers on Bungee Fixes #216 --- .../nl/matsv/viabackwards/BungeePlugin.java | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/bungee/src/main/java/nl/matsv/viabackwards/BungeePlugin.java b/bungee/src/main/java/nl/matsv/viabackwards/BungeePlugin.java index dbc7f751..d8e65bb1 100644 --- a/bungee/src/main/java/nl/matsv/viabackwards/BungeePlugin.java +++ b/bungee/src/main/java/nl/matsv/viabackwards/BungeePlugin.java @@ -10,19 +10,59 @@ package nl.matsv.viabackwards; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.event.ServerConnectedEvent; +import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.plugin.Plugin; +import net.md_5.bungee.event.EventHandler; import nl.matsv.viabackwards.api.ViaBackwardsPlatform; +import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16; +import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.protocols.base.ProtocolInfo; +import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; -public class BungeePlugin extends Plugin implements ViaBackwardsPlatform { +public class BungeePlugin extends Plugin implements ViaBackwardsPlatform, Listener { @Override public void onLoad() { Via.getManager().addEnableListener(() -> this.init(getDataFolder())); + getProxy().getPluginManager().registerListener(this, this); } - // Why is this not a thing in Bungee? O_o @Override public void disable() { } + + @EventHandler(priority = -110) // Slightly later than VV + public void serverConnected(ServerConnectedEvent event) { + ProxiedPlayer player = event.getPlayer(); + if (player.getServer() == null) return; + + UserConnection connection = Via.getManager().getConnection(player.getUniqueId()); + if (connection == null) return; + + ProtocolInfo info = connection.getProtocolInfo(); + if (info == null || !info.getPipeline().contains(Protocol1_15_2To1_16.class)) return; + + // Need to send a dummy respawn with a different dimension before the actual respawn + // We also don't know what dimension it's sent to, so just send 2 dummies :> + sendRespawn(connection, -1); + sendRespawn(connection, 0); + } + + private void sendRespawn(UserConnection connection, int dimension) { + PacketWrapper packet = new PacketWrapper(ClientboundPackets1_15.RESPAWN.ordinal(), null, connection); + packet.write(Type.INT, dimension); + packet.write(Type.LONG, 0L); + packet.write(Type.UNSIGNED_BYTE, (short) 0); + packet.write(Type.STRING, "default"); + try { + packet.send(Protocol1_15_2To1_16.class, true, true); + } catch (Exception e) { + e.printStackTrace(); + } + } }