From 50b80a64d3e8de39c82fa454453bcfd31bc544a7 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Sun, 3 Jan 2021 19:06:20 -0500 Subject: [PATCH] Dimension switching cleanup (#1694) * Dimension switching cleanup Cleans up dimension switching logic that should no longer be needed. Also fixes above Nether Bedrock building dimension switching. * Clear thunder on dimension switch too * Clarify fake dimension switch function name * Javadoc that --- .../network/session/GeyserSession.java | 21 ++++----------- .../player/BedrockActionTranslator.java | 15 +++++------ .../player/BedrockMovePlayerTranslator.java | 2 +- .../java/JavaJoinGameTranslator.java | 4 +-- .../java/JavaRespawnTranslator.java | 26 +++++++++++-------- .../JavaPlayerPositionRotationTranslator.java | 6 ++--- .../connector/utils/DimensionUtils.java | 21 +++++++++++---- 7 files changed, 47 insertions(+), 48 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java index cb8425868..375c747d4 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java @@ -37,7 +37,6 @@ import com.github.steveice10.mc.protocol.data.game.window.VillagerTrade; import com.github.steveice10.mc.protocol.packet.handshake.client.HandshakePacket; import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerPositionRotationPacket; import com.github.steveice10.mc.protocol.packet.ingame.client.world.ClientTeleportConfirmPacket; -import com.github.steveice10.mc.protocol.packet.ingame.server.ServerRespawnPacket; import com.github.steveice10.mc.protocol.packet.login.server.LoginSuccessPacket; import com.github.steveice10.packetlib.BuiltinFlags; import com.github.steveice10.packetlib.Client; @@ -95,7 +94,6 @@ import java.security.spec.InvalidKeySpecException; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.atomic.AtomicInteger; @Getter public class GeyserSession implements CommandSender { @@ -148,7 +146,11 @@ public class GeyserSession implements CommandSender { @Setter private GameMode gameMode = GameMode.SURVIVAL; - private final AtomicInteger pendingDimSwitches = new AtomicInteger(0); + /** + * Keeps track of the world name for respawning. + */ + @Setter + private String worldName = null; private boolean sneaking; @@ -185,9 +187,6 @@ public class GeyserSession implements CommandSender { @Setter private Vector3i lastInteractionPosition = Vector3i.ZERO; - private boolean manyDimPackets = false; - private ServerRespawnPacket lastDimPacket = null; - @Setter private Entity ridingVehicleEntity; @@ -537,16 +536,6 @@ public class GeyserSession implements CommandSender { @Override public void packetReceived(PacketReceivedEvent event) { if (!closed) { - //handle consecutive respawn packets - if (event.getPacket().getClass().equals(ServerRespawnPacket.class)) { - manyDimPackets = lastDimPacket != null; - lastDimPacket = event.getPacket(); - return; - } else if (lastDimPacket != null) { - PacketTranslatorRegistry.JAVA_TRANSLATOR.translate(lastDimPacket.getClass(), lastDimPacket, GeyserSession.this); - lastDimPacket = null; - } - // Required, or else Floodgate players break with Bukkit chunk caching if (event.getPacket() instanceof LoginSuccessPacket) { GameProfile profile = ((LoginSuccessPacket) event.getPacket()).getProfile(); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/player/BedrockActionTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/player/BedrockActionTranslator.java index 106a0d8da..66ec0a07b 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/player/BedrockActionTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/player/BedrockActionTranslator.java @@ -43,7 +43,6 @@ import org.geysermc.connector.entity.Entity; import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.PacketTranslator; import org.geysermc.connector.network.translators.Translator; -import org.geysermc.connector.network.translators.collision.CollisionManager; import org.geysermc.connector.network.translators.world.block.BlockTranslator; import org.geysermc.connector.utils.BlockUtils; @@ -157,14 +156,12 @@ public class BedrockActionTranslator extends PacketTranslator 0) return; + if (!session.isSpawned()) return; if (!session.getUpstream().isInitialized()) { MoveEntityAbsolutePacket moveEntityBack = new MoveEntityAbsolutePacket(); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaJoinGameTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaJoinGameTranslator.java index 292cfc74d..e9a1901dc 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaJoinGameTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaJoinGameTranslator.java @@ -55,12 +55,12 @@ public class JavaJoinGameTranslator extends PacketTranslator @Override public void translate(ServerRespawnPacket packet, GeyserSession session) { Entity entity = session.getPlayerEntity(); - if (entity == null) - return; float maxHealth = entity.getAttributes().containsKey(AttributeType.MAX_HEALTH) ? entity.getAttributes().get(AttributeType.MAX_HEALTH).getValue() : 20f; // Max health must be divisible by two in bedrock @@ -66,18 +64,24 @@ public class JavaRespawnTranslator extends PacketTranslator session.setRaining(false); } + if (session.isThunder()) { + LevelEventPacket stopThunderPacket = new LevelEventPacket(); + stopThunderPacket.setType(LevelEventType.STOP_THUNDERSTORM); + stopThunderPacket.setData(0); + stopThunderPacket.setPosition(Vector3f.ZERO); + session.sendUpstreamPacket(stopThunderPacket); + session.setThunder(false); + } + String newDimension = DimensionUtils.getNewDimension(packet.getDimension()); - if (!session.getDimension().equals(newDimension)) { - DimensionUtils.switchDimension(session, newDimension); - } else { - if (session.isManyDimPackets()) { //reloading world - String fakeDim = session.getDimension().equals(DimensionUtils.OVERWORLD) ? DimensionUtils.NETHER : DimensionUtils.OVERWORLD; + if (!session.getDimension().equals(newDimension) || !packet.getWorldName().equals(session.getWorldName())) { + if (!packet.getWorldName().equals(session.getWorldName()) && session.getDimension().equals(newDimension)) { + // Switching to a new world (based off the world name change); send a fake dimension change + String fakeDim = DimensionUtils.getTemporaryDimension(session.getDimension(), newDimension); DimensionUtils.switchDimension(session, fakeDim); - DimensionUtils.switchDimension(session, newDimension); - } else { - // Handled in JavaPlayerPositionRotationTranslator - session.setSpawned(false); } + session.setWorldName(packet.getWorldName()); + DimensionUtils.switchDimension(session, newDimension); } } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/player/JavaPlayerPositionRotationTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/player/JavaPlayerPositionRotationTranslator.java index 10d451df5..d5d8e7d3c 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/player/JavaPlayerPositionRotationTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/player/JavaPlayerPositionRotationTranslator.java @@ -46,13 +46,11 @@ public class JavaPlayerPositionRotationTranslator extends PacketTranslator 0) { - ChunkUtils.sendEmptyChunks(session, player.getPosition().toInt(), 3, true); - } Vector3i pos = Vector3i.from(0, Short.MAX_VALUE, 0); @@ -150,4 +145,20 @@ public class DimensionUtils { // Change dimension ID to the End to allow for building above Bedrock BEDROCK_NETHER_ID = isAboveNetherBedrockBuilding ? 2 : 1; } + + /** + * Gets the fake, temporary dimension we send clients to so we aren't switching to the same dimension without an additional + * dimension switch. + * + * @param currentDimension the current dimension of the player + * @param newDimension the new dimension that the player will be transferred to + * @return the fake dimension to transfer to + */ + public static String getTemporaryDimension(String currentDimension, String newDimension) { + if (BEDROCK_NETHER_ID == 2) { + // Prevents rare instances of Bedrock locking up + return javaToBedrock(newDimension) == 2 ? OVERWORLD : NETHER; + } + return currentDimension.equals(OVERWORLD) ? NETHER : OVERWORLD; + } }