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
This commit is contained in:
KennyTV 2021-05-28 10:02:26 +02:00
parent 8beedffea2
commit 0d31054903
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
3 changed files with 26 additions and 23 deletions

View File

@ -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]);
}

View File

@ -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));
}

View File

@ -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;
}