From 09eda340513dcd005e5640ed01a60471237a1d7f Mon Sep 17 00:00:00 2001 From: EnZaXD <60033407+FlorianMichael@users.noreply.github.com> Date: Tue, 16 Apr 2024 21:05:45 +0200 Subject: [PATCH] Use VVs IdAndData class for block id conversions (#719) --- .../api/data/MappedLegacyBlockItem.java | 8 +- .../rewriters/LegacyBlockItemRewriter.java | 34 ++++---- .../packets/BlockItemPackets1_11.java | 4 +- .../packets/BlockItemPackets1_13.java | 10 +-- .../viaversion/viabackwards/utils/Block.java | 77 ------------------- 5 files changed, 28 insertions(+), 105 deletions(-) delete mode 100644 common/src/main/java/com/viaversion/viabackwards/utils/Block.java diff --git a/common/src/main/java/com/viaversion/viabackwards/api/data/MappedLegacyBlockItem.java b/common/src/main/java/com/viaversion/viabackwards/api/data/MappedLegacyBlockItem.java index eff12d93..a5d352de 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/data/MappedLegacyBlockItem.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/data/MappedLegacyBlockItem.java @@ -17,8 +17,8 @@ */ package com.viaversion.viabackwards.api.data; -import com.viaversion.viabackwards.utils.Block; import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; +import com.viaversion.viaversion.util.IdAndData; import org.checkerframework.checker.nullness.qual.Nullable; public class MappedLegacyBlockItem { @@ -26,14 +26,14 @@ public class MappedLegacyBlockItem { private final int id; private final short data; private final String name; - private final Block block; + private final IdAndData block; private BlockEntityHandler blockEntityHandler; public MappedLegacyBlockItem(int id, short data, @Nullable String name, boolean block) { this.id = id; this.data = data; this.name = name != null ? "§f" + name : null; - this.block = block ? new Block(id, data) : null; + this.block = block ? new IdAndData(id, data) : null; } public int getId() { @@ -52,7 +52,7 @@ public class MappedLegacyBlockItem { return block != null; } - public Block getBlock() { + public IdAndData getBlock() { return block; } 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 a43fb212..fdeaff00 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 @@ -22,7 +22,6 @@ import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viabackwards.api.data.MappedLegacyBlockItem; import com.viaversion.viabackwards.api.data.BackwardsMappingDataLoader; import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.BlockColors; -import com.viaversion.viabackwards.utils.Block; import com.viaversion.viaversion.api.minecraft.BlockChangeRecord; import com.viaversion.viaversion.api.minecraft.chunks.Chunk; import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; @@ -51,6 +50,7 @@ import com.viaversion.viaversion.util.ComponentUtil; import java.util.HashMap; import java.util.Map; import java.util.Optional; +import com.viaversion.viaversion.util.IdAndData; import org.checkerframework.checker.nullness.qual.Nullable; public abstract class LegacyBlockItemRewriter> 12 & 15); + final IdAndData block = handleBlock(objectData & 4095, objectData >> 12 & 15); if (block == null) return; wrapper.set(Type.INT, 0, block.getId() | block.getData() << 12); @@ -216,11 +216,11 @@ public abstract class LegacyBlockItemRewriter> 4; int meta = block & 0xF; - Block b = handleBlock(btype, meta); + IdAndData b = handleBlock(btype, meta); if (b != null) { - palette.setIdByIndex(j, Block.toRawId(b.getId(), b.getData())); + palette.setIdByIndex(j, IdAndData.toRawData(b.getId(), b.getData())); } // We already know that is has a handler @@ -336,13 +336,13 @@ public abstract class LegacyBlockItemRewriter new MappedLegacyBlockItem(52, (short) -1, null, false)); + MappedLegacyBlockItem data = replacementData.computeIfAbsent(IdAndData.toRawData(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 3427f269..b69d9931 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,7 +26,6 @@ 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; @@ -56,6 +55,7 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.BlockIdData; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.SpawnEggRewriter; import com.viaversion.viaversion.util.ComponentUtil; +import com.viaversion.viaversion.util.IdAndData; import com.viaversion.viaversion.util.Key; import com.viaversion.viaversion.util.Pair; import java.util.ArrayList; @@ -100,11 +100,11 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit } if (SpawnEggRewriter.getEntityId(oldId).isPresent()) { - wrapper.write(Type.VAR_INT, Block.rawById(383)); + wrapper.write(Type.VAR_INT, IdAndData.toRawData(383)); return; } - wrapper.write(Type.VAR_INT, Block.getId(oldId)); + wrapper.write(Type.VAR_INT, IdAndData.getId(oldId)); }); protocol.registerClientbound(ClientboundPackets1_13.BLOCK_ACTION, new PacketHandlers() { @@ -751,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 = Block.toRawId(item.identifier(), item.data()); + int rawId = IdAndData.toRawData(item.identifier(), item.data()); // NBT Additions if (isDamageable(item.identifier())) { @@ -832,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 = Block.rawById(32); // Dead Bush + rawId = IdAndData.toRawData(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 deleted file mode 100644 index 645400cc..00000000 --- a/common/src/main/java/com/viaversion/viabackwards/utils/Block.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards - * Copyright (C) 2016-2024 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.viabackwards.utils; - -public class Block { - private final int id; - private final short data; - - public Block(int id, int data) { - this.id = id; - this.data = (short) data; - } - - public int getId() { - return id; - } - - public int getData() { - return data; - } - - public Block withData(int data) { - 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; - if (o == null || getClass() != o.getClass()) return false; - Block block = (Block) o; - if (id != block.id) return false; - return data == block.data; - } - - @Override - public int hashCode() { - int result = id; - result = 31 * result + data; - return result; - } -}