From ed52ef21ee2f608ed064e6a8ce6f8a1d40dc2ce4 Mon Sep 17 00:00:00 2001 From: EnZaXD <60033407+FlorianMichael@users.noreply.github.com> Date: Wed, 27 Mar 2024 16:49:14 +0100 Subject: [PATCH] Add calculation methods for block id and data (#704) --- .../rewriters/LegacyBlockItemRewriter.java | 32 +++++++++---------- .../packets/BlockItemPackets1_11.java | 4 +-- .../packets/BlockItemPackets1_13.java | 9 +++--- .../viaversion/viabackwards/utils/Block.java | 20 ++++++++++++ 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyBlockItemRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyBlockItemRewriter.java index 2578253f..1ebaf55f 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyBlockItemRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyBlockItemRewriter.java @@ -82,9 +82,9 @@ public abstract class LegacyBlockItemRewriter { int idx = wrapper.get(Type.VAR_INT, 0); - wrapper.set(Type.VAR_INT, 0, handleBlockID(idx)); + wrapper.set(Type.VAR_INT, 0, handleBlockId(idx)); }); } }); @@ -134,7 +134,7 @@ public abstract class LegacyBlockItemRewriter { for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) { - record.setBlockId(handleBlockID(record.getBlockId())); + record.setBlockId(handleBlockId(record.getBlockId())); } }); } @@ -185,14 +185,14 @@ public abstract class LegacyBlockItemRewriter> 4; - int meta = idx & 15; + public int handleBlockId(final int rawId) { + final int id = Block.getId(rawId); + final int data = Block.getData(rawId); - Block b = handleBlock(type, meta); - if (b == null) return idx; + final Block mappedBlock = handleBlock(id, data); + if (mappedBlock == null) return rawId; - return (b.getId() << 4 | (b.getData() & 15)); + return Block.toRawId(mappedBlock.getId(), mappedBlock.getData()); } public PacketHandler getFallingBlockHandler() { @@ -222,13 +222,13 @@ public abstract class LegacyBlockItemRewriter new MappedLegacyBlockItem(52, (short) -1, null, false)); + MappedLegacyBlockItem data = replacementData.computeIfAbsent(Block.rawById(52), s -> new MappedLegacyBlockItem(52, (short) -1, null, false)); data.setBlockEntityHandler((b, tag) -> { EntityIdRewriter.toClientSpawner(tag, true); return tag; diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java index 9f21aa36..36965961 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/BlockItemPackets1_13.java @@ -26,6 +26,7 @@ import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.block_entity_ha import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.providers.BackwardsBlockEntityProvider; import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.BackwardsBlockStorage; import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.NoteBlockStorage; +import com.viaversion.viabackwards.utils.Block; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.BlockChangeRecord; @@ -99,11 +100,11 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit } if (SpawnEggRewriter.getEntityId(oldId).isPresent()) { - wrapper.write(Type.VAR_INT, 383 << 4); + wrapper.write(Type.VAR_INT, Block.rawById(383)); return; } - wrapper.write(Type.VAR_INT, oldId >> 4); + wrapper.write(Type.VAR_INT, Block.getId(oldId)); }); protocol.registerClientbound(ClientboundPackets1_13.BLOCK_ACTION, new PacketHandlers() { @@ -750,7 +751,7 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit // Save original id int originalId = (item.identifier() << 16 | item.data() & 0xFFFF); - int rawId = (item.identifier() << 4 | item.data() & 0xF); + int rawId = Block.toRawId(item.identifier(), item.data()); // NBT Additions if (isDamageable(item.identifier())) { @@ -831,7 +832,7 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit if (item.identifier() == 229) { // purple shulker box newId = 362; // directly set the new id -> base/colorless shulker box } else if (item.identifier() == 31 && item.data() == 0) { // Shrub was removed - rawId = 32 << 4; // Dead Bush + rawId = Block.rawById(32); // Dead Bush } else if (protocol.getMappingData().getItemMappings().inverse().getNewId(rawId & ~0xF) != -1) { rawId &= ~0xF; // Remove data } else { diff --git a/common/src/main/java/com/viaversion/viabackwards/utils/Block.java b/common/src/main/java/com/viaversion/viabackwards/utils/Block.java index 291440be..645400cc 100644 --- a/common/src/main/java/com/viaversion/viabackwards/utils/Block.java +++ b/common/src/main/java/com/viaversion/viabackwards/utils/Block.java @@ -39,6 +39,26 @@ public class Block { return new Block(this.id, data); } + public static int getId(final int rawId) { + return rawId >> 4; + } + + public static int getData(final int rawId) { + return rawId & 15; + } + + public static int rawById(final int id) { + return id << 4; + } + + public static int rawByData(final int data) { + return data & ~15; + } + + public static int toRawId(final int id, final int data) { + return (id << 4) | (data & 15); + } + @Override public boolean equals(final Object o) { if (this == o) return true;