From 182148dbe89e7aee5c22567bb52b8be49d08401a Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sat, 4 May 2024 13:04:39 +0200 Subject: [PATCH] Some cleanup --- .../rewriter/BlockItemPacketRewriter1_21.java | 3 + .../rewriter/EntityPacketRewriter1_21.java | 41 +++++------- .../viaversion/rewriter/EntityRewriter.java | 62 +++++++++---------- .../rewriter/EntityPacketRewriter1_99.java | 24 +++---- 4 files changed, 60 insertions(+), 70 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_21to1_20_5/rewriter/BlockItemPacketRewriter1_21.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_21to1_20_5/rewriter/BlockItemPacketRewriter1_21.java index bdbeb477c..ccf41e162 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_21to1_20_5/rewriter/BlockItemPacketRewriter1_21.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_21to1_20_5/rewriter/BlockItemPacketRewriter1_21.java @@ -17,6 +17,8 @@ */ package com.viaversion.viaversion.protocols.protocol1_21to1_20_5.rewriter; +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_20_2; import com.viaversion.viaversion.api.type.types.version.Types1_20_5; import com.viaversion.viaversion.api.type.types.version.Types1_21; @@ -28,6 +30,7 @@ import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.Serverb import com.viaversion.viaversion.protocols.protocol1_21to1_20_5.Protocol1_21To1_20_5; import com.viaversion.viaversion.rewriter.BlockRewriter; import com.viaversion.viaversion.rewriter.StructuredItemRewriter; +import org.checkerframework.checker.nullness.qual.Nullable; public final class BlockItemPacketRewriter1_21 extends StructuredItemRewriter { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_21to1_20_5/rewriter/EntityPacketRewriter1_21.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_21to1_20_5/rewriter/EntityPacketRewriter1_21.java index 8dd39ed38..6a2522ad0 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_21to1_20_5/rewriter/EntityPacketRewriter1_21.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_21to1_20_5/rewriter/EntityPacketRewriter1_21.java @@ -47,24 +47,18 @@ public final class EntityPacketRewriter1_21 extends EntityRewriter { - // Add required damage type - final String type = wrapper.get(Type.STRING, 0); - final RegistryEntry[] entries = wrapper.get(Type.REGISTRY_ENTRY_ARRAY, 0); - if (Key.stripMinecraftNamespace(type).equals("damage_type")) { - final CompoundTag campfireDamageType = new CompoundTag(); - campfireDamageType.putString("scaling", "when_caused_by_living_non_player"); - campfireDamageType.putString("message_id", "inFire"); - campfireDamageType.putFloat("exhaustion", 0.1F); - wrapper.set(Type.REGISTRY_ENTRY_ARRAY, 0, addRegistryEnties(entries, new RegistryEntry("minecraft:campfire", campfireDamageType))); - } - }); + protocol.registerClientbound(ClientboundConfigurationPackets1_20_5.REGISTRY_DATA, wrapper -> { + final String type = Key.stripMinecraftNamespace(wrapper.passthrough(Type.STRING)); + final RegistryEntry[] entries = wrapper.passthrough(Type.REGISTRY_ENTRY_ARRAY); + if (Key.stripMinecraftNamespace(type).equals("damage_type")) { + // Add required damage type + final CompoundTag campfireDamageType = new CompoundTag(); + campfireDamageType.putString("scaling", "when_caused_by_living_non_player"); + campfireDamageType.putString("message_id", "inFire"); + campfireDamageType.putFloat("exhaustion", 0.1F); + wrapper.set(Type.REGISTRY_ENTRY_ARRAY, 0, addRegistryEnties(entries, new RegistryEntry("minecraft:campfire", campfireDamageType))); + } else { + handleRegistryData1_20_5(wrapper.user(), type, entries); } }); @@ -115,13 +109,10 @@ public final class EntityPacketRewriter1_21 extends EntityRewriter { + final int dimensionId = wrapper.passthrough(Type.VAR_INT); + final String world = wrapper.passthrough(Type.STRING); + trackWorldDataByKey1_20_5(wrapper.user(), dimensionId, world); // Tracks world height and name for chunk data and entity (un)tracking }); } 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 6257f593f..163ea89a7 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java @@ -446,27 +446,32 @@ public abstract class EntityRewriter { - EntityTracker tracker = tracker(wrapper.user()); int dimensionId = wrapper.get(Types.VAR_INT, dimensionIdIndex); - DimensionData dimensionData = tracker.dimensionData(dimensionId); - if (dimensionData == null) { - protocol.getLogger().severe("Dimension data missing for dimension: " + dimensionId + ", falling back to overworld"); - dimensionData = tracker.dimensionData("minecraft:overworld"); - Preconditions.checkNotNull(dimensionData, "Overworld data missing"); - } - - tracker.setCurrentWorldSectionHeight(dimensionData.height() >> 4); - tracker.setCurrentMinY(dimensionData.minY()); - String world = wrapper.get(Types.STRING, 0); - if (tracker.currentWorld() != null && !tracker.currentWorld().equals(world)) { - tracker.clearEntities(); - tracker.trackClientEntity(); - } - tracker.setCurrentWorld(world); + trackWorldDataByKey1_20_5(wrapper.user(), dimensionId, world); }; } + public void trackWorldDataByKey1_20_5(final UserConnection connection, final int dimensionId, final String world) { + // Track world height for use in chunk data + EntityTracker tracker = tracker(connection); + DimensionData dimensionData = tracker.dimensionData(dimensionId); + if (dimensionData == null) { + protocol.getLogger().severe("Dimension data missing for dimension: " + dimensionId + ", falling back to overworld"); + dimensionData = tracker.dimensionData("overworld"); + Preconditions.checkNotNull(dimensionData, "Overworld data missing"); + } + tracker.setCurrentWorldSectionHeight(dimensionData.height() >> 4); + tracker.setCurrentMinY(dimensionData.minY()); + + // Clear entities if the world changes + if (tracker.currentWorld() != null && !tracker.currentWorld().equals(world)) { + tracker.clearEntities(); + tracker.trackClientEntity(); + } + tracker.setCurrentWorld(world); + } + public PacketHandler biomeSizeTracker() { return wrapper -> trackBiomeSize(wrapper.user(), wrapper.get(Types.NAMED_COMPOUND_TAG, 0)); } @@ -503,22 +508,17 @@ public abstract class EntityRewriter { - final String registryKey = Key.stripMinecraftNamespace(wrapper.get(Types.STRING, 0)); - if (registryKey.equals("worldgen/biome")) { - final RegistryEntry[] entries = wrapper.get(Types.REGISTRY_ENTRY_ARRAY, 0); - tracker(wrapper.user()).setBiomesSent(entries.length); - } else if (registryKey.equals("dimension_type")) { - final RegistryEntry[] entries = wrapper.get(Types.REGISTRY_ENTRY_ARRAY, 0); - final Map dimensionDataMap = new HashMap<>(entries.length); - for (int i = 0; i < entries.length; i++) { - final RegistryEntry entry = entries[i]; - dimensionDataMap.put(Key.stripMinecraftNamespace(entry.key()), new DimensionDataImpl(i, (CompoundTag) entry.tag())); - } - tracker(wrapper.user()).setDimensions(dimensionDataMap); + public void handleRegistryData1_20_5(final UserConnection connection, final String registryKey, final RegistryEntry[] entries) { + if (registryKey.equals("worldgen/biome")) { + tracker(connection).setBiomesSent(entries.length); + } else if (registryKey.equals("dimension_type")) { + final Map dimensionDataMap = new HashMap<>(entries.length); + for (int i = 0; i < entries.length; i++) { + final RegistryEntry entry = entries[i]; + dimensionDataMap.put(Key.stripMinecraftNamespace(entry.key()), new DimensionDataImpl(i, (CompoundTag) entry.tag())); } - }; + tracker(connection).setDimensions(dimensionDataMap); + } } // --------------------------------------------------------------------------- diff --git a/template/src/main/java/com/viaversion/viaversion/template/protocols/rewriter/EntityPacketRewriter1_99.java b/template/src/main/java/com/viaversion/viaversion/template/protocols/rewriter/EntityPacketRewriter1_99.java index 0b424f50e..ffc4714a1 100644 --- a/template/src/main/java/com/viaversion/viaversion/template/protocols/rewriter/EntityPacketRewriter1_99.java +++ b/template/src/main/java/com/viaversion/viaversion/template/protocols/rewriter/EntityPacketRewriter1_99.java @@ -17,6 +17,7 @@ */ package com.viaversion.viaversion.template.protocols.rewriter; +import com.viaversion.viaversion.api.minecraft.RegistryEntry; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_20_5; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; @@ -27,6 +28,7 @@ import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ClientboundPac import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ClientboundPackets1_20_5; import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.template.protocols.Protocol1_99To_98; +import com.viaversion.viaversion.util.Key; // Replace if needed // Types1_OLD @@ -44,13 +46,10 @@ public final class EntityPacketRewriter1_99 extends EntityRewriter { + final String registryKey = Key.stripMinecraftNamespace(wrapper.passthrough(Types.STRING)); + final RegistryEntry[] entries = wrapper.passthrough(Types.REGISTRY_ENTRY_ARRAY); + handleRegistryData1_20_5(wrapper.user(), registryKey, entries); // Caches dimensions to access data like height later and tracks the amount of biomes sent for chunk data }); protocol.registerClientbound(ClientboundPackets1_20_5.LOGIN, new PacketHandlers() { @@ -72,13 +71,10 @@ public final class EntityPacketRewriter1_99 extends EntityRewriter { + final int dimensionId = wrapper.passthrough(Types.VAR_INT); + final String world = wrapper.passthrough(Types.STRING); + trackWorldDataByKey1_20_5(wrapper.user(), dimensionId, world); // Tracks world height and name for chunk data and entity (un)tracking }); }