From 9ed074d0d50ad5b837bb0bdedc6d77ba2e8b97ab Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Fri, 11 Aug 2023 15:47:36 +1000 Subject: [PATCH] Handle changes block state ids --- .../Protocol1_20_2To1_20.java | 13 ++++++ .../BlockItemPacketRewriter1_20_2.java | 42 +++++++++++++----- .../rewriter/EntityPacketRewriter1_20_2.java | 1 + .../viaversion/rewriter/SoundRewriter.java | 5 ++- .../viaversion/data/mappings-1.20to1.20.2.nbt | Bin 0 -> 163 bytes 5 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 common/src/main/resources/assets/viaversion/data/mappings-1.20to1.20.2.nbt diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_2to1_20/Protocol1_20_2To1_20.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_2to1_20/Protocol1_20_2To1_20.java index 5c9397a3d..a625465c9 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_2to1_20/Protocol1_20_2To1_20.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_2to1_20/Protocol1_20_2To1_20.java @@ -18,6 +18,8 @@ package com.viaversion.viaversion.protocols.protocol1_20_2to1_20; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.data.MappingData; +import com.viaversion.viaversion.api.data.MappingDataBase; import com.viaversion.viaversion.api.minecraft.entities.Entity1_19_4Types; import com.viaversion.viaversion.api.protocol.AbstractProtocol; import com.viaversion.viaversion.api.protocol.packet.Direction; @@ -41,11 +43,13 @@ import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.rewriter.BlockIt import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.rewriter.EntityPacketRewriter1_20_2; import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.storage.ConfigurationState; import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.storage.ConfigurationState.BridgePhase; +import com.viaversion.viaversion.rewriter.SoundRewriter; import java.util.UUID; import org.checkerframework.checker.nullness.qual.Nullable; public final class Protocol1_20_2To1_20 extends AbstractProtocol { + public static final MappingData MAPPINGS = new MappingDataBase("1.20", "1.20.2"); private final EntityPacketRewriter1_20_2 entityPacketRewriter = new EntityPacketRewriter1_20_2(this); private final BlockItemPacketRewriter1_20_2 itemPacketRewriter = new BlockItemPacketRewriter1_20_2(this); @@ -58,6 +62,10 @@ public final class Protocol1_20_2To1_20 extends AbstractProtocol soundRewriter = new SoundRewriter<>(this); + soundRewriter.register1_19_3Sound(ClientboundPackets1_19_4.SOUND); + soundRewriter.registerEntitySound(ClientboundPackets1_19_4.ENTITY_SOUND); + registerClientbound(ClientboundPackets1_19_4.SCOREBOARD_OBJECTIVE, wrapper -> { final byte slot = wrapper.read(Type.BYTE); wrapper.write(Type.VAR_INT, (int) slot); @@ -190,6 +198,11 @@ public final class Protocol1_20_2To1_20 extends AbstractProtocol blockRewriter = new BlockRewriter<>(protocol, Type.POSITION1_14); + blockRewriter.registerBlockAction(ClientboundPackets1_19_4.BLOCK_ACTION); + blockRewriter.registerBlockChange(ClientboundPackets1_19_4.BLOCK_CHANGE); + blockRewriter.registerVarLongMultiBlockChange1_20(ClientboundPackets1_19_4.MULTI_BLOCK_CHANGE); + blockRewriter.registerEffect(ClientboundPackets1_19_4.EFFECT, 1010, 2001); + protocol.registerServerbound(ServerboundPackets1_20_2.SET_BEACON_EFFECT, wrapper -> { if (wrapper.passthrough(Type.BOOLEAN)) { // Effects start at 1 before 1.20.2 @@ -113,15 +123,23 @@ public final class BlockItemPacketRewriter1_20_2 extends ItemRewriter { final EntityTracker tracker = protocol.getEntityRewriter().tracker(wrapper.user()); final Type chunkType = new Chunk1_18Type(tracker.currentWorldSectionHeight(), - MathUtil.ceilLog2(Protocol1_20To1_19_4.MAPPINGS.getBlockStateMappings().mappedSize()), + MathUtil.ceilLog2(protocol.getMappingData().getBlockStateMappings().size()), MathUtil.ceilLog2(tracker.biomesSent())); final Chunk chunk = wrapper.read(chunkType); final Type newChunkType = new ChunkType1_20_2(tracker.currentWorldSectionHeight(), - MathUtil.ceilLog2(Protocol1_20To1_19_4.MAPPINGS.getBlockStateMappings().mappedSize()), + MathUtil.ceilLog2(protocol.getMappingData().getBlockStateMappings().mappedSize()), MathUtil.ceilLog2(tracker.biomesSent())); wrapper.write(newChunkType, chunk); + 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)); + } + } + for (final BlockEntity blockEntity : chunk.blockEntities()) { handleBlockEntity(blockEntity.tag()); } @@ -150,7 +168,7 @@ public final class BlockItemPacketRewriter1_20_2 extends ItemRewriter { wrapper.passthrough(Type.BOOLEAN); // Reset/clear - int size = wrapper.passthrough(Type.VAR_INT); // Mapping size + final int size = wrapper.passthrough(Type.VAR_INT); // Mapping size for (int i = 0; i < size; i++) { wrapper.passthrough(Type.STRING); // Identifier @@ -164,7 +182,7 @@ public final class BlockItemPacketRewriter1_20_2 extends ItemRewriter { // Affected items - int length = wrapper.passthrough(Type.VAR_INT); + final int length = wrapper.passthrough(Type.VAR_INT); for (int i = 0; i < length; i++) { wrapper.passthrough(Type.SHORT); // Slot wrapper.write(Type.FLAT_VAR_INT_ITEM, wrapper.read(Type.ITEM1_20_2)); @@ -219,7 +237,7 @@ public final class BlockItemPacketRewriter1_20_2 extends ItemRewriter { wrapper.passthrough(Type.VAR_INT); // Container id - int size = wrapper.passthrough(Type.VAR_INT); + final int size = wrapper.passthrough(Type.VAR_INT); for (int i = 0; i < size; i++) { wrapper.write(Type.ITEM1_20_2, wrapper.read(Type.FLAT_VAR_INT_ITEM)); // Input wrapper.write(Type.ITEM1_20_2, wrapper.read(Type.FLAT_VAR_INT_ITEM)); // Output @@ -256,9 +274,13 @@ public final class BlockItemPacketRewriter1_20_2 extends ItemRewriter { - int id = wrapper.get(Type.VAR_INT, 0); - ParticleMappings mappings = Protocol1_20To1_19_4.MAPPINGS.getParticleMappings(); - if (mappings.isItemParticle(id)) { + final int id = wrapper.get(Type.VAR_INT, 0); + // Use 1.19.4->1.20 mappings + final ParticleMappings mappings = Protocol1_20To1_19_4.MAPPINGS.getParticleMappings(); + if (mappings.isBlockParticle(id)) { + final int data = wrapper.read(Type.VAR_INT); + wrapper.write(Type.VAR_INT, protocol.getMappingData().getNewBlockStateId(data)); + } else if (mappings.isItemParticle(id)) { wrapper.write(Type.ITEM1_20_2, wrapper.read(Type.FLAT_VAR_INT_ITEM)); } }); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_2to1_20/rewriter/EntityPacketRewriter1_20_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_2to1_20/rewriter/EntityPacketRewriter1_20_2.java index 64e072f51..460afff2b 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_2to1_20/rewriter/EntityPacketRewriter1_20_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_2to1_20/rewriter/EntityPacketRewriter1_20_2.java @@ -145,6 +145,7 @@ public final class EntityPacketRewriter1_20_2 extends EntityRewriter meta.setMetaType(Types1_20_2.META_TYPES.byId(meta.metaType().typeId()))); + registerMetaTypeHandler(null, Types1_20_2.META_TYPES.blockStateType, Types1_20_2.META_TYPES.optionalBlockStateType, Types1_20_2.META_TYPES.particleType); filter().filterFamily(Entity1_19_4Types.DISPLAY).addIndex(10); } diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/SoundRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/SoundRewriter.java index cf6a56ba5..0deb21d87 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/SoundRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/SoundRewriter.java @@ -37,7 +37,6 @@ public class SoundRewriter { this.idRewriter = idRewriter; } - // The same for entity sound public void registerSound(C packetType) { protocol.registerClientbound(packetType, new PacketHandlers() { @Override @@ -48,6 +47,10 @@ public class SoundRewriter { }); } + public void registerEntitySound(C packetType) { + this.registerSound(packetType); + } + // Not for entity sounds public void register1_19_3Sound(C packetType) { protocol.registerClientbound(packetType, wrapper -> { diff --git a/common/src/main/resources/assets/viaversion/data/mappings-1.20to1.20.2.nbt b/common/src/main/resources/assets/viaversion/data/mappings-1.20to1.20.2.nbt new file mode 100644 index 0000000000000000000000000000000000000000..a96a784e0b413b74bc2eecdb6d58e21eb3d4ebc3 GIT binary patch literal 163 zcmd;LU|?ooFH0>d&dkqaU|?Y6V&G27$xqHME=ep&EoNrm%1taNNKFaOtV(5Ii1T1% zV9HEkWM*I~h6uQGGcYBVfHX2NsDA`