From 0d31054903f7f3b7768c3fda4ed899bff91392b7 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Fri, 28 May 2021 10:02:26 +0200 Subject: [PATCH] Fix view distance update packet in 1.13->1.14 The client world is initialized during the join packet, but the view distance packet was created during/before the join handling. Fixes #2502 --- .../packets/WorldPackets.java | 29 +++++++++++++++---- .../storage/EntityTracker1_14.java | 17 ----------- .../storage/ClientWorld.java | 3 +- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java index 9cae4de55..d832f8a69 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java @@ -20,6 +20,7 @@ package com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.LongArrayTag; import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.BlockFace; import com.viaversion.viaversion.api.minecraft.chunks.Chunk; import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; @@ -246,7 +247,7 @@ public class WorldPackets { if (entityTracker.isForceSendCenterChunk() || diffX >= SERVERSIDE_VIEW_DISTANCE || diffZ >= SERVERSIDE_VIEW_DISTANCE) { - PacketWrapper fakePosLook = wrapper.create(0x40); // Set center chunk + PacketWrapper fakePosLook = wrapper.create(ClientboundPackets1_14.UPDATE_VIEW_POSITION); // Set center chunk fakePosLook.write(Type.VAR_INT, chunk.getX()); fakePosLook.write(Type.VAR_INT, chunk.getZ()); fakePosLook.send(Protocol1_14To1_13_2.class, true, true); @@ -302,7 +303,6 @@ public class WorldPackets { map(Type.INT); // 0 - Entity ID map(Type.UNSIGNED_BYTE); // 1 - Gamemode map(Type.INT); // 2 - Dimension - handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { @@ -320,12 +320,11 @@ public class WorldPackets { tracker.setClientEntityId(entityId); } }); - handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { short difficulty = wrapper.read(Type.UNSIGNED_BYTE); // 19w11a removed difficulty from join game - PacketWrapper difficultyPacket = wrapper.create(0x0D); + PacketWrapper difficultyPacket = wrapper.create(ClientboundPackets1_14.SERVER_DIFFICULTY); difficultyPacket.write(Type.UNSIGNED_BYTE, difficulty); difficultyPacket.write(Type.BOOLEAN, false); // Unknown value added in 19w11a difficultyPacket.send(protocol.getClass()); @@ -336,6 +335,14 @@ public class WorldPackets { wrapper.write(Type.VAR_INT, SERVERSIDE_VIEW_DISTANCE); // Serverside view distance, added in 19w13a } }); + handler(wrapper -> { + // Manually send the packet + wrapper.send(Protocol1_14To1_13_2.class, true, true); + wrapper.cancel(); + + // View distance has to be sent after the join packet + sendViewDistancePacket(wrapper.user()); + }); } }); @@ -373,12 +380,18 @@ public class WorldPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { short difficulty = wrapper.read(Type.UNSIGNED_BYTE); // 19w11a removed difficulty from respawn - PacketWrapper difficultyPacket = wrapper.create(0x0D); + PacketWrapper difficultyPacket = wrapper.create(ClientboundPackets1_14.SERVER_DIFFICULTY); difficultyPacket.write(Type.UNSIGNED_BYTE, difficulty); difficultyPacket.write(Type.BOOLEAN, false); // Unknown value added in 19w11a difficultyPacket.send(protocol.getClass()); } }); + handler(wrapper -> { + // Manually send the packet and update the viewdistance after + wrapper.send(Protocol1_14To1_13_2.class, true, true); + wrapper.cancel(); + sendViewDistancePacket(wrapper.user()); + }); } }); @@ -390,6 +403,12 @@ public class WorldPackets { }); } + private static void sendViewDistancePacket(UserConnection connection) throws Exception { + PacketWrapper setViewDistance = PacketWrapper.create(ClientboundPackets1_14.UPDATE_VIEW_DISTANCE, null, connection); + setViewDistance.write(Type.VAR_INT, WorldPackets.SERVERSIDE_VIEW_DISTANCE); + setViewDistance.send(Protocol1_14To1_13_2.class, true, true); + } + private static long[] encodeHeightMap(int[] heightMap) { return CompactArrayUtil.createCompactArray(9, heightMap.length, i -> heightMap[i]); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java index fb6238283..05805f4af 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java @@ -19,11 +19,7 @@ package com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.data.entity.EntityTrackerBase; -import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; -import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets.WorldPackets; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -90,19 +86,6 @@ public class EntityTracker1_14 extends EntityTrackerBase { } } - @Override - public void setClientEntityId(int playerEntityId) { - super.setClientEntityId(playerEntityId); - - PacketWrapper setViewDistance = PacketWrapper.create(0x41, null, user()); - setViewDistance.write(Type.VAR_INT, WorldPackets.SERVERSIDE_VIEW_DISTANCE); - try { - setViewDistance.send(Protocol1_14To1_13_2.class, true, true); - } catch (Exception e) { - e.printStackTrace(); - } - } - public byte getEntityFlags(int player) { return zeroIfNull(playerEntityFlags.get(player)); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9_3to1_9_1_2/storage/ClientWorld.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9_3to1_9_1_2/storage/ClientWorld.java index 37f8f7cf2..5f40e2348 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9_3to1_9_1_2/storage/ClientWorld.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9_3to1_9_1_2/storage/ClientWorld.java @@ -20,6 +20,7 @@ package com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage; import com.viaversion.viaversion.api.connection.StoredObject; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.Environment; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Stored up until 1.14 to be used in chunk sending. @@ -31,7 +32,7 @@ public class ClientWorld extends StoredObject { super(connection); } - public Environment getEnvironment() { + public @Nullable Environment getEnvironment() { return environment; }