From 3eba8ba922297b6e9099d82a62f99d692ead17c2 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Fri, 20 May 2022 09:53:39 +0200 Subject: [PATCH] More consistent client entity tracking --- .../Protocol1_13To1_12_2.java | 26 +------- .../packets/EntityPackets.java | 18 ++++++ .../packets/EntityPackets.java | 37 +++++++++++ .../packets/WorldPackets.java | 52 +-------------- .../Protocol1_15To1_14_4.java | 4 +- .../packets/EntityPackets.java | 27 ++++++++ .../packets/PlayerPackets.java | 64 ------------------- .../packets/EntityPackets.java | 4 +- .../packets/EntityPackets.java | 45 +++++++++++++ .../packets/WorldPackets.java | 49 -------------- .../viaversion/rewriter/EntityRewriter.java | 4 +- 11 files changed, 132 insertions(+), 198 deletions(-) delete mode 100644 common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/packets/PlayerPackets.java diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java index c67f4ddb2..00ec101d3 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java @@ -118,7 +118,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol { // Send fake declare commands w.create(ClientboundPackets1_13.DECLARE_COMMANDS, wrapper -> { @@ -388,30 +388,6 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol { + ClientWorld clientChunks = wrapper.user().get(ClientWorld.class); + int dimensionId = wrapper.get(Type.INT, 1); + clientChunks.setEnvironment(dimensionId); + }); + handler(metadataRewriter.playerTrackerHandler()); + handler(Protocol1_13To1_12_2.SEND_DECLARE_COMMANDS_AND_TAGS); + } + }); + metadataRewriter.registerRemoveEntities(ClientboundPackets1_12_1.DESTROY_ENTITIES); metadataRewriter.registerMetadataRewriter(ClientboundPackets1_12_1.ENTITY_METADATA, Types1_12.METADATA_LIST, Types1_13.METADATA_LIST); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java index ba6393193..9a25c4bbc 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java @@ -33,6 +33,7 @@ import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPacke import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.metadata.MetadataRewriter1_14To1_13_2; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14; +import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; import java.util.LinkedList; import java.util.List; @@ -195,6 +196,42 @@ public class EntityPackets { } }); + protocol.registerClientbound(ClientboundPackets1_13.JOIN_GAME, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.INT); // 0 - Entity ID + map(Type.UNSIGNED_BYTE); // 1 - Gamemode + map(Type.INT); // 2 - Dimension + handler(wrapper -> { + // Store the player + ClientWorld clientChunks = wrapper.user().get(ClientWorld.class); + int dimensionId = wrapper.get(Type.INT, 1); + clientChunks.setEnvironment(dimensionId); + }); + handler(metadataRewriter.playerTrackerHandler()); + handler(wrapper -> { + short difficulty = wrapper.read(Type.UNSIGNED_BYTE); // 19w11a removed difficulty from join game + 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.scheduleSend(protocol.getClass()); + + wrapper.passthrough(Type.UNSIGNED_BYTE); // Max Players + wrapper.passthrough(Type.STRING); // Level Type + + wrapper.write(Type.VAR_INT, WorldPackets.SERVERSIDE_VIEW_DISTANCE); // Serverside view distance, added in 19w13a + }); + handler(wrapper -> { + // Manually send the packet + wrapper.send(Protocol1_14To1_13_2.class); + wrapper.cancel(); + + // View distance has to be sent after the join packet + WorldPackets.sendViewDistancePacket(wrapper.user()); + }); + } + }); + protocol.registerClientbound(ClientboundPackets1_13.USE_BED, ClientboundPackets1_14.ENTITY_METADATA, new PacketRemapper() { @Override public void registerMap() { 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 58c6d7504..87942e976 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 @@ -25,7 +25,6 @@ import com.viaversion.viaversion.api.minecraft.BlockFace; import com.viaversion.viaversion.api.minecraft.chunks.Chunk; import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; import com.viaversion.viaversion.api.minecraft.chunks.NibbleArray; -import com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; @@ -288,55 +287,6 @@ public class WorldPackets { } }); - protocol.registerClientbound(ClientboundPackets1_13.JOIN_GAME, new PacketRemapper() { - @Override - public void registerMap() { - 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 { - // Store the player - ClientWorld clientChunks = wrapper.user().get(ClientWorld.class); - int dimensionId = wrapper.get(Type.INT, 1); - clientChunks.setEnvironment(dimensionId); - - int entityId = wrapper.get(Type.INT, 0); - - Entity1_14Types entType = Entity1_14Types.PLAYER; - // Register Type ID - EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class); - tracker.addEntity(entityId, entType); - 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(ClientboundPackets1_14.SERVER_DIFFICULTY); - difficultyPacket.write(Type.UNSIGNED_BYTE, difficulty); - difficultyPacket.write(Type.BOOLEAN, false); // Unknown value added in 19w11a - difficultyPacket.scheduleSend(protocol.getClass()); - - wrapper.passthrough(Type.UNSIGNED_BYTE); // Max Players - wrapper.passthrough(Type.STRING); // Level Type - - 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); - wrapper.cancel(); - - // View distance has to be sent after the join packet - sendViewDistancePacket(wrapper.user()); - }); - } - }); - protocol.registerClientbound(ClientboundPackets1_13.MAP_DATA, new PacketRemapper() { @Override public void registerMap() { @@ -394,7 +344,7 @@ public class WorldPackets { }); } - private static void sendViewDistancePacket(UserConnection connection) throws Exception { + 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); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java index ec3231640..fc6b293d2 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java @@ -18,6 +18,7 @@ package com.viaversion.viaversion.protocols.protocol1_15to1_14_4; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types; import com.viaversion.viaversion.api.protocol.AbstractProtocol; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; @@ -31,9 +32,7 @@ import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.data.MappingData import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.metadata.MetadataRewriter1_15To1_14_4; import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.EntityPackets; import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.InventoryPackets; -import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.PlayerPackets; import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.WorldPackets; -import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; @@ -55,7 +54,6 @@ public class Protocol1_15To1_14_4 extends AbstractProtocol wrapper.write(Type.LONG, 0L)); // Level Seed + } + }); + + protocol.registerClientbound(ClientboundPackets1_14.JOIN_GAME, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.INT); // 0 - Entity ID + map(Type.UNSIGNED_BYTE); // 1 - Gamemode + map(Type.INT); // 2 - Dimension + handler(metadataRewriter.playerTrackerHandler()); + handler(wrapper -> wrapper.write(Type.LONG, 0L)); // Level Seed + + map(Type.UNSIGNED_BYTE); // 3 - Max Players + map(Type.STRING); // 4 - Level Type + map(Type.VAR_INT); // 5 - View Distance + map(Type.BOOLEAN); // 6 - Reduce Debug Info + + handler(wrapper -> wrapper.write(Type.BOOLEAN, !Via.getConfig().is1_15InstantRespawn())); // Show Death Screen + } + }); + metadataRewriter.registerMetadataRewriter(ClientboundPackets1_14.ENTITY_METADATA, Types1_14.METADATA_LIST); metadataRewriter.registerRemoveEntities(ClientboundPackets1_14.DESTROY_ENTITIES); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/packets/PlayerPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/packets/PlayerPackets.java deleted file mode 100644 index 8b78ec869..000000000 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/packets/PlayerPackets.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2022 ViaVersion and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets; - -import com.viaversion.viaversion.api.Via; -import com.viaversion.viaversion.api.data.entity.EntityTracker; -import com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types; -import com.viaversion.viaversion.api.protocol.Protocol; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; -import com.viaversion.viaversion.api.type.Type; -import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; -import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4; - -public class PlayerPackets { - - public static void register(Protocol protocol) { - protocol.registerClientbound(ClientboundPackets1_14.RESPAWN, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); - handler(wrapper -> wrapper.write(Type.LONG, 0L)); // Level Seed - } - }); - - protocol.registerClientbound(ClientboundPackets1_14.JOIN_GAME, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); // 0 - Entity ID - map(Type.UNSIGNED_BYTE); // 1 - Gamemode - map(Type.INT); // 2 - Dimension - - handler(wrapper -> { - // Register Type ID - EntityTracker tracker = wrapper.user().getEntityTracker(Protocol1_15To1_14_4.class); - int entityId = wrapper.get(Type.INT, 0); - tracker.addEntity(entityId, Entity1_15Types.PLAYER); - }); - handler(wrapper -> wrapper.write(Type.LONG, 0L)); // Level Seed - - map(Type.UNSIGNED_BYTE); // 3 - Max Players - map(Type.STRING); // 4 - Level Type - map(Type.VAR_INT); // 5 - View Distance - map(Type.BOOLEAN); // 6 - Reduce Debug Info - - handler(wrapper -> wrapper.write(Type.BOOLEAN, !Via.getConfig().is1_15InstantRespawn())); // Show Death Screen - } - }); - } -} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/packets/EntityPackets.java index a0ae8d70d..2d6789590 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/packets/EntityPackets.java @@ -63,9 +63,7 @@ public class EntityPackets { map(Type.LONG); // Seed map(Type.UNSIGNED_BYTE, Type.VAR_INT); // Max players // ... - handler(wrapper -> { - wrapper.user().getEntityTracker(Protocol1_16_2To1_16_1.class).addEntity(wrapper.get(Type.INT, 0), Entity1_16_2Types.PLAYER); - }); + handler(metadataRewriter.playerTrackerHandler()); } }); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/EntityPackets.java index 2e5b77a14..3be81a65e 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/EntityPackets.java @@ -17,6 +17,10 @@ */ package com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets; +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.github.steveice10.opennbt.tag.builtin.IntTag; +import com.github.steveice10.opennbt.tag.builtin.ListTag; +import com.github.steveice10.opennbt.tag.builtin.Tag; import com.viaversion.viaversion.api.data.entity.EntityTracker; import com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types; import com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types; @@ -66,6 +70,42 @@ public final class EntityPackets extends EntityRewriter { } }); + protocol.registerClientbound(ClientboundPackets1_16_2.JOIN_GAME, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.INT); // Entity ID + map(Type.BOOLEAN); // Hardcore + map(Type.UNSIGNED_BYTE); // Gamemode + map(Type.BYTE); // Previous Gamemode + map(Type.STRING_ARRAY); // World List + map(Type.NBT); // Registry + map(Type.NBT); // Current dimension + handler(wrapper -> { + // Add new dimension fields + CompoundTag dimensionRegistry = wrapper.get(Type.NBT, 0).get("minecraft:dimension_type"); + ListTag dimensions = dimensionRegistry.get("value"); + for (Tag dimension : dimensions) { + CompoundTag dimensionCompound = ((CompoundTag) dimension).get("element"); + addNewDimensionData(dimensionCompound); + } + + CompoundTag currentDimensionTag = wrapper.get(Type.NBT, 1); + addNewDimensionData(currentDimensionTag); + }); + handler(playerTrackerHandler()); + } + }); + + protocol.registerClientbound(ClientboundPackets1_16_2.RESPAWN, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> { + CompoundTag dimensionData = wrapper.passthrough(Type.NBT); + addNewDimensionData(dimensionData); + }); + } + }); + protocol.registerClientbound(ClientboundPackets1_16_2.ENTITY_PROPERTIES, new PacketRemapper() { @Override public void registerMap() { @@ -156,4 +196,9 @@ public final class EntityPackets extends EntityRewriter { public EntityType typeFromId(int type) { return Entity1_17Types.getTypeFromId(type); } + + private static void addNewDimensionData(CompoundTag tag) { + tag.put("min_y", new IntTag(0)); + tag.put("height", new IntTag(256)); + } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/WorldPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/WorldPackets.java index 497c1b072..d0f5e3b83 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/WorldPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/WorldPackets.java @@ -17,16 +17,10 @@ */ package com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.opennbt.tag.builtin.IntTag; -import com.github.steveice10.opennbt.tag.builtin.ListTag; -import com.github.steveice10.opennbt.tag.builtin.Tag; -import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.BlockChangeRecord; import com.viaversion.viaversion.api.minecraft.BlockChangeRecord1_16_2; import com.viaversion.viaversion.api.minecraft.chunks.Chunk; import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; -import com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; @@ -164,44 +158,6 @@ public final class WorldPackets { } }); - protocol.registerClientbound(ClientboundPackets1_16_2.JOIN_GAME, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); // Entity ID - map(Type.BOOLEAN); // Hardcore - map(Type.UNSIGNED_BYTE); // Gamemode - map(Type.BYTE); // Previous Gamemode - map(Type.STRING_ARRAY); // World List - map(Type.NBT); // Registry - map(Type.NBT); // Current dimension - handler(wrapper -> { - // Add new dimension fields - CompoundTag dimensionRegistry = wrapper.get(Type.NBT, 0).get("minecraft:dimension_type"); - ListTag dimensions = dimensionRegistry.get("value"); - for (Tag dimension : dimensions) { - CompoundTag dimensionCompound = ((CompoundTag) dimension).get("element"); - addNewDimensionData(dimensionCompound); - } - - CompoundTag currentDimensionTag = wrapper.get(Type.NBT, 1); - addNewDimensionData(currentDimensionTag); - - UserConnection user = wrapper.user(); - user.getEntityTracker(Protocol1_17To1_16_4.class).addEntity(wrapper.get(Type.INT, 0), Entity1_17Types.PLAYER); - }); - } - }); - - protocol.registerClientbound(ClientboundPackets1_16_2.RESPAWN, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> { - CompoundTag dimensionData = wrapper.passthrough(Type.NBT); - addNewDimensionData(dimensionData); - }); - } - }); - blockRewriter.registerEffect(ClientboundPackets1_16_2.EFFECT, 1010, 2001); } @@ -234,9 +190,4 @@ public final class WorldPackets { blockChangePacket.send(Protocol1_17To1_16_4.class); } } - - private static void addNewDimensionData(CompoundTag tag) { - tag.put("min_y", new IntTag(0)); - tag.put("height", new IntTag(256)); - } } diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java index 9002b7c03..992768311 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java @@ -402,9 +402,7 @@ public abstract class EntityRewriter extends RewriterBase return wrapper -> { final EntityTracker tracker = tracker(wrapper.user()); final int entityId = wrapper.get(Type.INT, 0); - if (tracker.clientEntityId() == -1) { - tracker.setClientEntityId(entityId); - } + tracker.setClientEntityId(entityId); tracker.addEntity(entityId, tracker.playerType()); }; }