From 1399c901d267aaf2a55026e081a1c3a0a362bea1 Mon Sep 17 00:00:00 2001 From: EnZaXD <60033407+FlorianMichael@users.noreply.github.com> Date: Wed, 15 May 2024 16:58:57 +0200 Subject: [PATCH] Add rewriter function for legacy chunk types to BlockRewriter (#3854) --- .../rewriter/WorldPacketRewriter1_13_1.java | 12 +----- .../rewriter/WorldPacketRewriter1_15.java | 23 +--------- .../rewriter/WorldPacketRewriter1_16.java | 43 +++++-------------- .../rewriter/WorldPacketRewriter1_16_2.java | 24 +---------- .../rewriter/WorldPacketRewriter1_17.java | 14 +----- .../rewriter/WorldPacketRewriter1_19.java | 25 +---------- .../BlockItemPacketRewriter1_20_3.java | 2 - .../viaversion/rewriter/BlockRewriter.java | 31 +++++++++++++ 8 files changed, 48 insertions(+), 126 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_13to1_13_1/rewriter/WorldPacketRewriter1_13_1.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_13to1_13_1/rewriter/WorldPacketRewriter1_13_1.java index 651a60de7..8fa820fdc 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_13to1_13_1/rewriter/WorldPacketRewriter1_13_1.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_13to1_13_1/rewriter/WorldPacketRewriter1_13_1.java @@ -38,17 +38,7 @@ public class WorldPacketRewriter1_13_1 { ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); Chunk chunk = wrapper.passthrough(ChunkType1_13.forEnvironment(clientWorld.getEnvironment())); - for (ChunkSection section : chunk.getSections()) { - if (section == null) { - continue; - } - - DataPalette palette = section.palette(PaletteType.BLOCKS); - for (int i = 0; i < palette.size(); i++) { - int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(i)); - palette.setIdByIndex(i, mappedBlockStateId); - } - } + blockRewriter.handleChunk(chunk); }); blockRewriter.registerBlockEvent(ClientboundPackets1_13.BLOCK_EVENT); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_14_4to1_15/rewriter/WorldPacketRewriter1_15.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_14_4to1_15/rewriter/WorldPacketRewriter1_15.java index cf6ab0b7b..7e46bcaff 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_14_4to1_15/rewriter/WorldPacketRewriter1_15.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_14_4to1_15/rewriter/WorldPacketRewriter1_15.java @@ -17,10 +17,6 @@ */ package com.viaversion.viaversion.protocols.v1_14_4to1_15.rewriter; -import com.viaversion.viaversion.api.minecraft.chunks.Chunk; -import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; -import com.viaversion.viaversion.api.minecraft.chunks.DataPalette; -import com.viaversion.viaversion.api.minecraft.chunks.PaletteType; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_14; @@ -38,11 +34,7 @@ public final class WorldPacketRewriter1_15 { blockRewriter.registerBlockUpdate(ClientboundPackets1_14_4.BLOCK_UPDATE); blockRewriter.registerChunkBlocksUpdate(ClientboundPackets1_14_4.CHUNK_BLOCKS_UPDATE); blockRewriter.registerBlockBreakAck(ClientboundPackets1_14_4.BLOCK_BREAK_ACK); - - protocol.registerClientbound(ClientboundPackets1_14_4.LEVEL_CHUNK, wrapper -> { - Chunk chunk = wrapper.read(ChunkType1_14.TYPE); - wrapper.write(ChunkType1_15.TYPE, chunk); - + blockRewriter.registerLevelChunk(ClientboundPackets1_14_4.LEVEL_CHUNK, ChunkType1_14.TYPE, ChunkType1_15.TYPE, (connection, chunk) -> { if (chunk.isFullChunk()) { int[] biomeData = chunk.getBiomeData(); int[] newBiomeData = new int[1024]; @@ -64,19 +56,6 @@ public final class WorldPacketRewriter1_15 { chunk.setBiomeData(newBiomeData); } - - for (int s = 0; s < chunk.getSections().length; s++) { - ChunkSection section = chunk.getSections()[s]; - if (section == null) { - continue; - } - - DataPalette palette = section.palette(PaletteType.BLOCKS); - for (int i = 0; i < palette.size(); i++) { - int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(i)); - palette.setIdByIndex(i, mappedBlockStateId); - } - } }); blockRewriter.registerLevelEvent(ClientboundPackets1_14_4.LEVEL_EVENT, 1010, 2001); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_15_2to1_16/rewriter/WorldPacketRewriter1_16.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_15_2to1_16/rewriter/WorldPacketRewriter1_16.java index ff1fee066..9acfb6d12 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_15_2to1_16/rewriter/WorldPacketRewriter1_16.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_15_2to1_16/rewriter/WorldPacketRewriter1_16.java @@ -24,10 +24,6 @@ import com.viaversion.nbt.tag.LongArrayTag; import com.viaversion.nbt.tag.StringTag; import com.viaversion.nbt.tag.Tag; import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.minecraft.chunks.Chunk; -import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; -import com.viaversion.viaversion.api.minecraft.chunks.DataPalette; -import com.viaversion.viaversion.api.minecraft.chunks.PaletteType; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_15; @@ -50,35 +46,9 @@ public class WorldPacketRewriter1_16 { blockRewriter.registerBlockUpdate(ClientboundPackets1_15.BLOCK_UPDATE); blockRewriter.registerChunkBlocksUpdate(ClientboundPackets1_15.CHUNK_BLOCKS_UPDATE); blockRewriter.registerBlockBreakAck(ClientboundPackets1_15.BLOCK_BREAK_ACK); - - protocol.registerClientbound(ClientboundPackets1_15.LIGHT_UPDATE, new PacketHandlers() { - @Override - public void register() { - map(Types.VAR_INT); // x - map(Types.VAR_INT); // y - handler(wrapper -> wrapper.write(Types.BOOLEAN, true)); // Take neighbour's light into account as well - } - }); - - protocol.registerClientbound(ClientboundPackets1_15.LEVEL_CHUNK, wrapper -> { - Chunk chunk = wrapper.read(ChunkType1_15.TYPE); - wrapper.write(ChunkType1_16.TYPE, chunk); - + blockRewriter.registerLevelChunk(ClientboundPackets1_15.LEVEL_CHUNK, ChunkType1_15.TYPE, ChunkType1_16.TYPE, (connection, chunk) -> { chunk.setIgnoreOldLightData(chunk.isFullChunk()); - for (int s = 0; s < chunk.getSections().length; s++) { - ChunkSection section = chunk.getSections()[s]; - if (section == null) { - continue; - } - - DataPalette palette = section.palette(PaletteType.BLOCKS); - for (int i = 0; i < palette.size(); i++) { - int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(i)); - palette.setIdByIndex(i, mappedBlockStateId); - } - } - CompoundTag heightMaps = chunk.getHeightMap(); for (Tag heightMapTag : heightMaps.values()) { LongArrayTag heightMap = (LongArrayTag) heightMapTag; @@ -89,7 +59,16 @@ public class WorldPacketRewriter1_16 { if (chunk.getBlockEntities() == null) return; for (CompoundTag blockEntity : chunk.getBlockEntities()) { - handleBlockEntity(protocol, wrapper.user(), blockEntity); + handleBlockEntity(protocol, connection, blockEntity); + } + }); + + protocol.registerClientbound(ClientboundPackets1_15.LIGHT_UPDATE, new PacketHandlers() { + @Override + public void register() { + map(Types.VAR_INT); // x + map(Types.VAR_INT); // y + handler(wrapper -> wrapper.write(Types.BOOLEAN, true)); // Take neighbour's light into account as well } }); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_16_1to1_16_2/rewriter/WorldPacketRewriter1_16_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_16_1to1_16_2/rewriter/WorldPacketRewriter1_16_2.java index a278372b8..b38ccaee7 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_16_1to1_16_2/rewriter/WorldPacketRewriter1_16_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_16_1to1_16_2/rewriter/WorldPacketRewriter1_16_2.java @@ -19,12 +19,7 @@ package com.viaversion.viaversion.protocols.v1_16_1to1_16_2.rewriter; 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.chunks.DataPalette; -import com.viaversion.viaversion.api.minecraft.chunks.PaletteType; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_16; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_16_2; @@ -45,24 +40,7 @@ public class WorldPacketRewriter1_16_2 { blockRewriter.registerBlockEvent(ClientboundPackets1_16.BLOCK_EVENT); blockRewriter.registerBlockUpdate(ClientboundPackets1_16.BLOCK_UPDATE); blockRewriter.registerBlockBreakAck(ClientboundPackets1_16.BLOCK_BREAK_ACK); - - protocol.registerClientbound(ClientboundPackets1_16.LEVEL_CHUNK, wrapper -> { - Chunk chunk = wrapper.read(ChunkType1_16.TYPE); - wrapper.write(ChunkType1_16_2.TYPE, chunk); - - for (int s = 0; s < chunk.getSections().length; s++) { - ChunkSection section = chunk.getSections()[s]; - if (section == null) { - continue; - } - - DataPalette palette = section.palette(PaletteType.BLOCKS); - for (int i = 0; i < palette.size(); i++) { - int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(i)); - palette.setIdByIndex(i, mappedBlockStateId); - } - } - }); + blockRewriter.registerLevelChunk(ClientboundPackets1_16.LEVEL_CHUNK, ChunkType1_16.TYPE, ChunkType1_16_2.TYPE); protocol.registerClientbound(ClientboundPackets1_16.CHUNK_BLOCKS_UPDATE, ClientboundPackets1_16_2.SECTION_BLOCKS_UPDATE, wrapper -> { wrapper.cancel(); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_16_4to1_17/rewriter/WorldPacketRewriter1_17.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_16_4to1_17/rewriter/WorldPacketRewriter1_17.java index e9b972f6f..d5abf3473 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_16_4to1_17/rewriter/WorldPacketRewriter1_17.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_16_4to1_17/rewriter/WorldPacketRewriter1_17.java @@ -125,18 +125,8 @@ public final class WorldPacketRewriter1_17 { // 1.17 uses a bitset for the mask chunk.setChunkMask(BitSet.valueOf(new long[]{chunk.getBitmask()})); - for (int s = 0; s < chunk.getSections().length; s++) { - ChunkSection section = chunk.getSections()[s]; - if (section == null) { - continue; - } - - DataPalette palette = section.palette(PaletteType.BLOCKS); - for (int i = 0; i < palette.size(); i++) { - int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(i)); - palette.setIdByIndex(i, mappedBlockStateId); - } - } + // Rewrite block state ids + blockRewriter.handleChunk(chunk); }); blockRewriter.registerLevelEvent(ClientboundPackets1_16_2.LEVEL_EVENT, 1010, 2001); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_18_2to1_19/rewriter/WorldPacketRewriter1_19.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_18_2to1_19/rewriter/WorldPacketRewriter1_19.java index 6c539aed6..98e52c008 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_18_2to1_19/rewriter/WorldPacketRewriter1_19.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_18_2to1_19/rewriter/WorldPacketRewriter1_19.java @@ -17,19 +17,12 @@ */ package com.viaversion.viaversion.protocols.v1_18_2to1_19.rewriter; -import com.google.common.base.Preconditions; -import com.viaversion.viaversion.api.data.entity.EntityTracker; -import com.viaversion.viaversion.api.minecraft.chunks.Chunk; -import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; -import com.viaversion.viaversion.api.minecraft.chunks.DataPalette; -import com.viaversion.viaversion.api.minecraft.chunks.PaletteType; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_18; import com.viaversion.viaversion.protocols.v1_17_1to1_18.packet.ClientboundPackets1_18; import com.viaversion.viaversion.protocols.v1_18_2to1_19.Protocol1_18_2To1_19; import com.viaversion.viaversion.protocols.v1_18_2to1_19.packet.ServerboundPackets1_19; import com.viaversion.viaversion.rewriter.BlockRewriter; -import com.viaversion.viaversion.util.MathUtil; public final class WorldPacketRewriter1_19 { @@ -39,26 +32,10 @@ public final class WorldPacketRewriter1_19 { blockRewriter.registerBlockUpdate(ClientboundPackets1_18.BLOCK_UPDATE); blockRewriter.registerSectionBlocksUpdate(ClientboundPackets1_18.SECTION_BLOCKS_UPDATE); blockRewriter.registerLevelEvent(ClientboundPackets1_18.LEVEL_EVENT, 1010, 2001); + blockRewriter.registerLevelChunk1_19(ClientboundPackets1_18.LEVEL_CHUNK_WITH_LIGHT, ChunkType1_18::new); protocol.cancelClientbound(ClientboundPackets1_18.BLOCK_BREAK_ACK); - protocol.registerClientbound(ClientboundPackets1_18.LEVEL_CHUNK_WITH_LIGHT, wrapper -> { - final EntityTracker tracker = protocol.getEntityRewriter().tracker(wrapper.user()); - Preconditions.checkArgument(tracker.biomesSent() != -1, "Biome count not set"); - Preconditions.checkArgument(tracker.currentWorldSectionHeight() != -1, "Section height not set"); - final ChunkType1_18 chunkType = new ChunkType1_18(tracker.currentWorldSectionHeight(), - MathUtil.ceilLog2(protocol.getMappingData().getBlockStateMappings().mappedSize()), - MathUtil.ceilLog2(tracker.biomesSent())); - final Chunk chunk = wrapper.passthrough(chunkType); - for (final ChunkSection section : chunk.getSections()) { - final DataPalette blockPalette = section.palette(PaletteType.BLOCKS); - for (int i = 0; i < blockPalette.size(); i++) { - final int id = blockPalette.idByIndex(i); - blockPalette.setIdByIndex(i, protocol.getMappingData().getNewBlockStateId(id)); - } - } - }); - protocol.registerServerbound(ServerboundPackets1_19.SET_BEACON, wrapper -> { // Primary effect if (wrapper.read(Types.BOOLEAN)) { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_2to1_20_3/rewriter/BlockItemPacketRewriter1_20_3.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_2to1_20_3/rewriter/BlockItemPacketRewriter1_20_3.java index 97566ad29..f2d0e78f9 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_2to1_20_3/rewriter/BlockItemPacketRewriter1_20_3.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_2to1_20_3/rewriter/BlockItemPacketRewriter1_20_3.java @@ -20,7 +20,6 @@ package com.viaversion.viaversion.protocols.v1_20_2to1_20_3.rewriter; import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.ListTag; import com.viaversion.nbt.tag.StringTag; -import com.viaversion.nbt.tag.Tag; import com.google.gson.JsonElement; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; @@ -29,7 +28,6 @@ import com.viaversion.viaversion.api.minecraft.Particle; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; -import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_20_2; import com.viaversion.viaversion.api.type.types.version.Types1_20_3; diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/BlockRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/BlockRewriter.java index c90478296..c66c12f23 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/BlockRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/BlockRewriter.java @@ -173,6 +173,37 @@ public class BlockRewriter { }); } + public void registerLevelChunk(C packetType, Type chunkType, Type newChunkType) { + registerLevelChunk(packetType, chunkType, newChunkType, null); + } + + public void registerLevelChunk(C packetType, Type chunkType, Type newChunkType, @Nullable BiConsumer chunkRewriter) { + protocol.registerClientbound(packetType, wrapper -> { + Chunk chunk = wrapper.read(chunkType); + wrapper.write(newChunkType, chunk); + + handleChunk(chunk); + if (chunkRewriter != null) { + chunkRewriter.accept(wrapper.user(), chunk); + } + }); + } + + public void handleChunk(Chunk chunk) { + for (int s = 0; s < chunk.getSections().length; s++) { + ChunkSection section = chunk.getSections()[s]; + if (section == null) { + continue; + } + + DataPalette palette = section.palette(PaletteType.BLOCKS); + for (int i = 0; i < palette.size(); i++) { + int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(palette.idByIndex(i)); + palette.setIdByIndex(i, mappedBlockStateId); + } + } + } + public void registerLevelChunk1_19(C packetType, ChunkTypeSupplier chunkTypeSupplier) { registerLevelChunk1_19(packetType, chunkTypeSupplier, null); }