From a59c3848840dbb40506dcec622838f13ef095614 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Thu, 2 Jul 2020 13:20:49 +0200 Subject: [PATCH] Send dummy respawn on same dimension switch --- .../nl/matsv/viabackwards/BungeePlugin.java | 43 +------------------ .../packets/EntityPackets1_16.java | 15 ++++++- 2 files changed, 15 insertions(+), 43 deletions(-) diff --git a/bungee/src/main/java/nl/matsv/viabackwards/BungeePlugin.java b/bungee/src/main/java/nl/matsv/viabackwards/BungeePlugin.java index d8e65bb1..b08f072c 100644 --- a/bungee/src/main/java/nl/matsv/viabackwards/BungeePlugin.java +++ b/bungee/src/main/java/nl/matsv/viabackwards/BungeePlugin.java @@ -10,59 +10,18 @@ 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, Listener { +public class BungeePlugin extends Plugin implements ViaBackwardsPlatform { @Override public void onLoad() { Via.getManager().addEnableListener(() -> this.init(getDataFolder())); - getProxy().getPluginManager().registerListener(this, this); } @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(); - } - } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java index 1cad7fd4..00c1f7a2 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java @@ -17,6 +17,7 @@ import us.myles.ViaVersion.api.remapper.ValueTransformer; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.Particle; import us.myles.ViaVersion.api.type.types.version.Types1_14; +import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; import us.myles.viaversion.libs.gson.JsonElement; @@ -57,7 +58,19 @@ public class EntityPackets1_16 extends EntityRewriter { map(Type.BYTE, Type.NOTHING); // Previous gamemode handler(wrapper -> { ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); - clientWorld.setEnvironment(wrapper.get(Type.INT, 0)); + int dimension = wrapper.get(Type.INT, 0); + + // Send a dummy respawn with a different dimension if the previous one is equal to the new + if (clientWorld.getEnvironment() != null && dimension == clientWorld.getEnvironment().getId()) { + PacketWrapper packet = wrapper.create(ClientboundPackets1_15.RESPAWN.ordinal()); + packet.write(Type.INT, dimension == 0 ? -1 : 0); + packet.write(Type.LONG, 0L); + packet.write(Type.UNSIGNED_BYTE, (short) 0); + packet.write(Type.STRING, "default"); + packet.send(Protocol1_15_2To1_16.class, true, true); + } + + clientWorld.setEnvironment(dimension); wrapper.write(Type.STRING, "default"); // Level type wrapper.read(Type.BOOLEAN); // Debug