From c2b237f19665ff482a2948d0afe9db54a0197f11 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Tue, 7 Jun 2022 23:35:43 -0400 Subject: [PATCH] DRY - one function to create respawn packets from JoinGame packets --- .../client/ClientPlaySessionHandler.java | 26 ++++++------------- .../proxy/protocol/packet/Respawn.java | 7 +++++ 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index b65d413de..04bfd100e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -525,7 +525,8 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { // Most notably, by having the client accept the join game packet, we can work around the need // to perform entity ID rewrites, eliminating potential issues from rewriting packets and // improving compatibility with mods. - int sentOldDim = joinGame.getDimension(); + final Respawn respawn = Respawn.fromJoinGame(joinGame); + if (player.getProtocolVersion().compareTo(MINECRAFT_1_16) < 0) { // Before Minecraft 1.16, we could not switch to the same dimension without sending an // additional respawn. On older versions of Minecraft this forces the client to perform @@ -533,12 +534,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { joinGame.setDimension(joinGame.getDimension() == 0 ? -1 : 0); } player.getConnection().delayedWrite(joinGame); - - player.getConnection().delayedWrite( - new Respawn(sentOldDim, joinGame.getPartialHashedSeed(), - joinGame.getDifficulty(), joinGame.getGamemode(), joinGame.getLevelType(), - false, joinGame.getDimensionInfo(), joinGame.getPreviousGamemode(), - joinGame.getCurrentDimensionData(), joinGame.getLastDeathPosition())); + player.getConnection().delayedWrite(respawn); } private void doSafeClientServerSwitch(JoinGame joinGame) { @@ -550,19 +546,13 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { player.getConnection().delayedWrite(joinGame); // Send a respawn packet in a different dimension. - int tempDim = joinGame.getDimension() == 0 ? -1 : 0; - player.getConnection().delayedWrite( - new Respawn(tempDim, joinGame.getPartialHashedSeed(), joinGame.getDifficulty(), - joinGame.getGamemode(), joinGame.getLevelType(), - false, joinGame.getDimensionInfo(), joinGame.getPreviousGamemode(), - joinGame.getCurrentDimensionData(), joinGame.getLastDeathPosition())); + final Respawn fakeSwitchPacket = Respawn.fromJoinGame(joinGame); + fakeSwitchPacket.setDimension(joinGame.getDimension() == 0 ? -1 : 0); + player.getConnection().delayedWrite(fakeSwitchPacket); // Now send a respawn packet in the correct dimension. - player.getConnection().delayedWrite( - new Respawn(joinGame.getDimension(), joinGame.getPartialHashedSeed(), - joinGame.getDifficulty(), joinGame.getGamemode(), joinGame.getLevelType(), - false, joinGame.getDimensionInfo(), joinGame.getPreviousGamemode(), - joinGame.getCurrentDimensionData(), joinGame.getLastDeathPosition())); + final Respawn correctSwitchPacket = Respawn.fromJoinGame(joinGame); + player.getConnection().delayedWrite(correctSwitchPacket); } public List getServerBossBars() { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Respawn.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Respawn.java index e5db58bba..fc2d42e0c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Respawn.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Respawn.java @@ -60,6 +60,13 @@ public class Respawn implements MinecraftPacket { this.lastDeathPosition = lastDeathPosition; } + public static Respawn fromJoinGame(JoinGame joinGame) { + return new Respawn(joinGame.getDimension(), joinGame.getPartialHashedSeed(), + joinGame.getDifficulty(), joinGame.getGamemode(), joinGame.getLevelType(), + false, joinGame.getDimensionInfo(), joinGame.getPreviousGamemode(), + joinGame.getCurrentDimensionData(), joinGame.getLastDeathPosition()); + } + public int getDimension() { return dimension; }