From 6ad4a29d30120699268bf2fa4e7685810722941b Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Wed, 20 Dec 2023 12:10:15 +0100 Subject: [PATCH] Allow specifying unmapped data in legacy mappings --- .../rewriters/LegacyBlockItemRewriter.java | 92 ++++++++++++------- .../packets/BlockItemPackets1_11.java | 2 +- 2 files changed, 58 insertions(+), 36 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 60bf59d3..a995f8b3 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 @@ -50,7 +50,7 @@ public abstract class LegacyBlockItemRewriter> extends ItemRewriterBase { private static final Map> LEGACY_MAPPINGS = new HashMap<>(); - protected final Int2ObjectMap replacementData; + protected final Int2ObjectMap replacementData; // Raw id -> mapped data static { JsonObject jsonObject = VBMappingDataLoader.loadFromDataDir("legacy-mappings.json"); @@ -58,34 +58,49 @@ public abstract class LegacyBlockItemRewriter mappings = new Int2ObjectOpenHashMap<>(8); LEGACY_MAPPINGS.put(entry.getKey(), mappings); for (Map.Entry dataEntry : entry.getValue().getAsJsonObject().entrySet()) { - JsonObject object = dataEntry.getValue().getAsJsonObject(); - int id = object.getAsJsonPrimitive("id").getAsInt(); - JsonPrimitive jsonData = object.getAsJsonPrimitive("data"); - short data = jsonData != null ? jsonData.getAsShort() : 0; - String name = object.getAsJsonPrimitive("name").getAsString(); - JsonPrimitive blockField = object.getAsJsonPrimitive("block"); - boolean block = blockField != null && blockField.getAsBoolean(); + addMapping(dataEntry.getKey(), dataEntry.getValue().getAsJsonObject(), mappings); + } + } + } - if (dataEntry.getKey().indexOf('-') != -1) { - // Range of ids - String[] split = dataEntry.getKey().split("-", 2); - int from = Integer.parseInt(split[0]); - int to = Integer.parseInt(split[1]); + private static void addMapping(String key, JsonObject object, Int2ObjectMap mappings) { + int id = object.getAsJsonPrimitive("id").getAsInt(); + JsonPrimitive jsonData = object.getAsJsonPrimitive("data"); + short data = jsonData != null ? jsonData.getAsShort() : 0; + String name = object.getAsJsonPrimitive("name").getAsString(); + JsonPrimitive blockField = object.getAsJsonPrimitive("block"); + boolean block = blockField != null && blockField.getAsBoolean(); - // Special block color handling - if (name.contains("%color%")) { - for (int i = from; i <= to; i++) { - mappings.put(i, new MappedLegacyBlockItem(id, data, name.replace("%color%", BlockColors.get(i - from)), block)); - } - } else { - MappedLegacyBlockItem mappedBlockItem = new MappedLegacyBlockItem(id, data, name, block); - for (int i = from; i <= to; i++) { - mappings.put(i, mappedBlockItem); - } - } - } else { - mappings.put(Integer.parseInt(dataEntry.getKey()), new MappedLegacyBlockItem(id, data, name, block)); - } + if (key.indexOf('-') == -1) { + int unmappedId; + int dataSeparatorIndex = key.indexOf(':'); + if (dataSeparatorIndex != -1) { + // Include data + short unmappedData = Short.parseShort(key.substring(dataSeparatorIndex + 1)); + unmappedId = Integer.parseInt(key.substring(0, dataSeparatorIndex)); + unmappedId = (unmappedId << 4) | (unmappedData & 15); + } else { + unmappedId = Integer.parseInt(key) << 4; + } + + mappings.put(unmappedId, new MappedLegacyBlockItem(id, data, name, block)); + return; + } + + // Range of ids + String[] split = key.split("-", 2); + int from = Integer.parseInt(split[0]); + int to = Integer.parseInt(split[1]); + + // Special block color handling + if (name.contains("%color%")) { + for (int i = from; i <= to; i++) { + mappings.put(i << 4, new MappedLegacyBlockItem(id, data, name.replace("%color%", BlockColors.get(i - from)), block)); + } + } else { + MappedLegacyBlockItem mappedBlockItem = new MappedLegacyBlockItem(id, data, name, block); + for (int i = from; i <= to; i++) { + mappings.put(i << 4, mappedBlockItem); } } } @@ -99,7 +114,7 @@ public abstract class LegacyBlockItemRewriter tags = new HashMap<>(); @@ -184,9 +209,8 @@ public abstract class LegacyBlockItemRewriter> 4; - MappedLegacyBlockItem settings = replacementData.get(btype); + MappedLegacyBlockItem settings = getMappedBlockItem(block); if (settings != null && settings.hasBlockEntityHandler()) { settings.getBlockEntityHandler().handleOrNewCompoundTag(block, tag); } @@ -215,7 +239,7 @@ public abstract class LegacyBlockItemRewriter> 4; - int meta = block & 15; - MappedLegacyBlockItem settings = replacementData.get(btype); + MappedLegacyBlockItem settings = getMappedBlockItem(block); if (settings == null || !settings.hasBlockEntityHandler()) continue; Pos pos = new Pos(x, (y + (i << 4)), z); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java index 3a9bb7fd..a0735c2d 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java @@ -306,7 +306,7 @@ public class BlockItemPackets1_11 extends LegacyBlockItemRewriter new MappedLegacyBlockItem(52, (short) -1, null, false)); + MappedLegacyBlockItem data = replacementData.computeIfAbsent(52 << 4, s -> new MappedLegacyBlockItem(52, (short) -1, null, false)); data.setBlockEntityHandler((b, tag) -> { EntityIdRewriter.toClientSpawner(tag, true); return tag;