From 5d0d6f603b186caebe9ca89643ca9ac15da6c997 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Thu, 2 Mar 2023 13:22:16 +0100 Subject: [PATCH] Update VV usage --- .../api/data/BackwardsMappings.java | 50 +- .../api/data/VBMappingDataLoader.java | 134 +- .../viabackwards/api/data/VBMappings.java | 5 +- .../block_entity_handlers/PistonHandler.java | 33 +- .../data/BackwardsMappings.java | 31 +- .../packets/BlockItemPackets1_13.java | 10 +- .../Protocol1_13_2To1_14.java | 4 +- .../data/BackwardsMappings.java | 2 +- .../packets/BlockItemPackets1_16.java | 3 +- .../packets/EntityPackets1_16.java | 3 +- .../data/BiomeMappings.java | 8 +- .../Protocol1_16_4To1_17.java | 7 +- .../data/BackwardsMappings.java | 2 +- .../packets/BlockItemPackets1_18.java | 5 +- .../data/BackwardsMappings.java | 7 +- .../data/BackwardsMappings.java | 7 +- .../packets/BlockItemPackets1_19_3.java | 3 +- .../packets/BlockItemPackets1_19_4.java | 3 +- .../viabackwards/data/biome-mappings.json | 52 +- .../viabackwards/data/mapping-1.12to1.13.json | 11278 +++++++-------- .../data/mapping-1.13.2to1.14.json | 1374 +- .../data/mapping-1.13to1.13.2.json | 32 +- .../viabackwards/data/mapping-1.14to1.15.json | 100 +- .../viabackwards/data/mapping-1.15to1.16.json | 11722 ++++++++-------- .../data/mapping-1.16.2to1.17.json | 1874 +-- .../data/mapping-1.16to1.16.2.json | 32 +- .../viabackwards/data/mapping-1.17to1.18.json | 4 +- .../viabackwards/data/mapping-1.18to1.19.json | 972 +- .../data/mapping-1.19.3to1.19.4.json | 222 +- .../data/mapping-1.19to1.19.3.json | 1482 +- gradle/libs.versions.toml | 2 +- settings.gradle.kts | 2 +- 32 files changed, 14748 insertions(+), 14717 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/api/data/BackwardsMappings.java b/common/src/main/java/com/viaversion/viabackwards/api/data/BackwardsMappings.java index f16a8d5e..330a181b 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/data/BackwardsMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/data/BackwardsMappings.java @@ -26,6 +26,7 @@ import com.viaversion.viaversion.api.data.Mappings; import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap; import com.viaversion.viaversion.libs.gson.JsonObject; +import com.viaversion.viaversion.util.Key; import java.util.Map; import java.util.logging.Logger; import org.checkerframework.checker.nullness.qual.Nullable; @@ -33,7 +34,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; public class BackwardsMappings extends MappingDataBase { private final Class> vvProtocolClass; - private Int2ObjectMap backwardsItemMappings; + protected Int2ObjectMap backwardsItemMappings; private Map backwardsSoundMappings; private Map entityNames; @@ -46,21 +47,20 @@ public class BackwardsMappings extends MappingDataBase { Preconditions.checkArgument(vvProtocolClass == null || !vvProtocolClass.isAssignableFrom(BackwardsProtocol.class)); this.vvProtocolClass = vvProtocolClass; // Just re-use ViaVersion's item id map - loadItems = false; } @Override - protected final void loadExtras(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings) { + protected final void loadExtras(JsonObject unmappedIdentifiers, JsonObject mappedIdentifiers, @Nullable JsonObject diffMappings) { if (diffMappings != null) { JsonObject diffItems = diffMappings.getAsJsonObject("items"); - if (diffItems != null) { - backwardsItemMappings = VBMappingDataLoader.loadItemMappings(oldMappings.getAsJsonObject("items"), - newMappings.getAsJsonObject("items"), diffItems, shouldWarnOnMissing("items")); + if (diffItems != null && mappedIdentifiers.get("items").isJsonArray() && unmappedIdentifiers.get("items").isJsonArray()) { + backwardsItemMappings = VBMappingDataLoader.loadItemMappings(unmappedIdentifiers.getAsJsonArray("items"), + mappedIdentifiers.getAsJsonArray("items"), diffItems, shouldWarnOnMissing("items")); } JsonObject diffSounds = diffMappings.getAsJsonObject("sounds"); if (diffSounds != null) { - backwardsSoundMappings = VBMappingDataLoader.objectToNamespacedMap(diffSounds); + backwardsSoundMappings = VBMappingDataLoader.objectToMap(diffSounds); } JsonObject diffEntityNames = diffMappings.getAsJsonObject("entitynames"); @@ -74,24 +74,28 @@ public class BackwardsMappings extends MappingDataBase { itemMappings = Via.getManager().getProtocolManager().getProtocol(vvProtocolClass).getMappingData().getItemMappings().inverse(); } - loadVBExtras(oldMappings, newMappings); + loadVBExtras(unmappedIdentifiers, mappedIdentifiers, diffMappings); } @Override - protected @Nullable Mappings loadFromArray(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings, String key) { - if (!oldMappings.has(key) || !newMappings.has(key)) return null; + protected @Nullable Mappings loadFromArray(JsonObject unmappedIdentifiers, JsonObject mappedIdentifiers, @Nullable JsonObject diffMappings, String key) { + if (!unmappedIdentifiers.has(key) || !mappedIdentifiers.has(key) || !shouldLoad(key)) { + return null; + } JsonObject diff = diffMappings != null ? diffMappings.getAsJsonObject(key) : null; - return VBMappings.vbBuilder().unmapped(oldMappings.getAsJsonArray(key)).mapped(newMappings.getAsJsonArray(key)) + return VBMappings.vbBuilder().unmapped(unmappedIdentifiers.getAsJsonArray(key)).mapped(mappedIdentifiers.getAsJsonArray(key)) .diffMappings(diff).warnOnMissing(shouldWarnOnMissing(key)).build(); } @Override - protected @Nullable Mappings loadFromObject(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings, String key) { - if (!oldMappings.has(key) || !newMappings.has(key)) return null; + protected @Nullable Mappings loadFromObject(JsonObject unmappedIdentifiers, JsonObject mappedIdentifiers, @Nullable JsonObject diffMappings, String key) { + if (!unmappedIdentifiers.has(key) || !mappedIdentifiers.has(key) || !shouldLoad(key)) { + return null; + } JsonObject diff = diffMappings != null ? diffMappings.getAsJsonObject(key) : null; - return VBMappings.vbBuilder().unmapped(oldMappings.getAsJsonObject(key)).mapped(newMappings.getAsJsonObject(key)) + return VBMappings.vbBuilder().unmapped(unmappedIdentifiers.getAsJsonObject(key)).mapped(mappedIdentifiers.getAsJsonObject(key)) .diffMappings(diff).warnOnMissing(shouldWarnOnMissing(key)).build(); } @@ -103,13 +107,18 @@ public class BackwardsMappings extends MappingDataBase { /** * To be overridden. */ - protected void loadVBExtras(JsonObject unmapped, JsonObject mapped) { + protected void loadVBExtras(JsonObject unmappedIdentifiers, JsonObject mappedIdentifiers, JsonObject diffMappings) { } protected boolean shouldWarnOnMissing(String key) { return !key.equals("blocks") && !key.equals("statistics") && !key.equals("entities"); } + @Override + protected boolean shouldLoad(final String key) { + return !key.equals("items"); + } + @Override protected Logger getLogger() { return ViaBackwards.getPlatform().getLogger(); @@ -121,7 +130,7 @@ public class BackwardsMappings extends MappingDataBase { @Override public int getNewItemId(int id) { // Don't warn on missing here - return this.itemMappings.get(id); + return this.itemMappings.getNewId(id); } @Override @@ -133,7 +142,7 @@ public class BackwardsMappings extends MappingDataBase { @Override public int getOldItemId(final int id) { // Warn on missing - return checkValidity(id, this.itemMappings.inverse().get(id), "item"); + return checkValidity(id, this.itemMappings.inverse().getNewId(id), "item"); } public @Nullable MappedItem getMappedItem(int id) { @@ -144,12 +153,7 @@ public class BackwardsMappings extends MappingDataBase { if (backwardsSoundMappings == null) { return null; } - - if (id.indexOf(':') == -1) { - id = "minecraft:" + id; - } - - return backwardsSoundMappings.get(id); + return backwardsSoundMappings.get(Key.stripMinecraftNamespace(id)); } public @Nullable String mappedEntityName(String entityName) { diff --git a/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappingDataLoader.java b/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappingDataLoader.java index 94b96df4..ccfab1e4 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappingDataLoader.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappingDataLoader.java @@ -23,6 +23,7 @@ import com.viaversion.viaversion.api.data.MappingDataLoader; import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap; import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap; import com.viaversion.viaversion.libs.fastutil.objects.Object2IntMap; +import com.viaversion.viaversion.libs.gson.JsonArray; import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.libs.gson.JsonIOException; import com.viaversion.viaversion.libs.gson.JsonObject; @@ -42,7 +43,9 @@ public final class VBMappingDataLoader { public static JsonObject loadFromDataDir(String name) { File file = new File(ViaBackwards.getPlatform().getDataFolder(), name); - if (!file.exists()) return loadData(name); + if (!file.exists()) { + return loadData(name); + } // Load the file from the platform's directory if present try (FileReader reader = new FileReader(file)) { @@ -72,61 +75,66 @@ public final class VBMappingDataLoader { } } - public static void mapIdentifiers(int[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers, JsonObject diffIdentifiers, boolean warnOnMissing) { - Object2IntMap newIdentifierMap = MappingDataLoader.indexedObjectToMap(newIdentifiers); - for (Map.Entry entry : oldIdentifiers.entrySet()) { - String key = entry.getValue().getAsString(); - int mappedId = newIdentifierMap.getInt(key); - if (mappedId == -1) { - if (diffIdentifiers != null) { - // Search in diff mappings - JsonPrimitive diffValueJson = diffIdentifiers.getAsJsonPrimitive(key); - String diffValue = diffValueJson != null ? diffValueJson.getAsString() : null; - - int dataIndex; - if (diffValue == null && (dataIndex = key.indexOf('[')) != -1 - && (diffValueJson = diffIdentifiers.getAsJsonPrimitive(key.substring(0, dataIndex))) != null) { - // Check for wildcard mappings - diffValue = diffValueJson.getAsString(); - - // Keep original properties if value ends with [ - if (diffValue.endsWith("[")) { - diffValue += key.substring(dataIndex + 1); - } - } - - if (diffValue != null) { - mappedId = newIdentifierMap.getInt(diffValue); - } - } - - if (mappedId == -1) { - // Nothing found :( - if (warnOnMissing && !Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - ViaBackwards.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( "); - } - continue; - } + public static void mapIdentifiers(int[] output, JsonObject unmappedIdentifiers, JsonObject mappedIdentifiers, JsonObject diffIdentifiers, boolean warnOnMissing) { + Object2IntMap newIdentifierMap = MappingDataLoader.indexedObjectToMap(mappedIdentifiers); + for (Map.Entry entry : unmappedIdentifiers.entrySet()) { + int mappedId = mapIdentifierEntry(entry.getValue().getAsString(), newIdentifierMap, diffIdentifiers, warnOnMissing); + if (mappedId != -1) { + output[Integer.parseInt(entry.getKey())] = mappedId; } - - output[Integer.parseInt(entry.getKey())] = mappedId; } } - public static Map objectToNamespacedMap(JsonObject object) { - Map mappings = new HashMap<>(object.size()); - for (Map.Entry entry : object.entrySet()) { - String key = entry.getKey(); - if (key.indexOf(':') == -1) { - key = "minecraft:" + key; + public static void mapIdentifiers(int[] output, JsonArray unmappedIdentifiers, JsonArray mappedIdentifiers, JsonObject diffIdentifiers, boolean warnOnMissing) { + Object2IntMap newIdentifierMap = MappingDataLoader.arrayToMap(mappedIdentifiers); + for (int id = 0; id < unmappedIdentifiers.size(); id++) { + JsonElement unmappedIdentifier = unmappedIdentifiers.get(id); + int mappedId = mapIdentifierEntry(unmappedIdentifier.getAsString(), newIdentifierMap, diffIdentifiers, warnOnMissing); + if (mappedId != -1) { + output[id] = mappedId; } - String value = entry.getValue().getAsString(); - if (value.indexOf(':') == -1) { - value = "minecraft:" + value; - } - mappings.put(key, value); } - return mappings; + } + + private static int mapIdentifierEntry(String key, Object2IntMap mappedIdentifiers, @Nullable JsonObject diffIdentifiers, boolean warnOnMissing) { + int mappedId = mappedIdentifiers.getInt(key); + if (mappedId == -1) { + if (diffIdentifiers != null) { + // Search in diff mappings + JsonPrimitive diffValueJson = diffIdentifiers.getAsJsonPrimitive(key); + String diffValue = diffValueJson != null ? diffValueJson.getAsString() : null; + if (diffValue != null && diffValue.isEmpty()) { + return -1; + } + + int dataIndex; + if (diffValue == null && (dataIndex = key.indexOf('[')) != -1 + && (diffValueJson = diffIdentifiers.getAsJsonPrimitive(key.substring(0, dataIndex))) != null) { + // Check for wildcard mappings + diffValue = diffValueJson.getAsString(); + if (diffValue != null && diffValue.isEmpty()) { + return -1; + } + + // Keep original properties if value ends with [ + if (diffValue.endsWith("[")) { + diffValue += key.substring(dataIndex + 1); + } + } + + if (diffValue != null) { + mappedId = mappedIdentifiers.getInt(diffValue); + } + } + + if (mappedId == -1) { + if (warnOnMissing && !Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { + ViaBackwards.getPlatform().getLogger().warning("No key for " + key + " :( "); + } + return -1; + } + } + return mappedId; } public static Map objectToMap(JsonObject object) { @@ -137,38 +145,40 @@ public final class VBMappingDataLoader { return mappings; } - public static Int2ObjectMap loadItemMappings(JsonObject oldMapping, JsonObject newMapping, JsonObject diffMapping, boolean warnOnMissing) { + public static Int2ObjectMap loadItemMappings(JsonArray unmappedIdentifiers, JsonArray mappedIdentifiers, JsonObject diffMapping, boolean warnOnMissing) { + return loadItemMappings(MappingDataLoader.arrayToMap(unmappedIdentifiers), MappingDataLoader.arrayToMap(mappedIdentifiers), diffMapping, warnOnMissing); + } + + public static Int2ObjectMap loadItemMappings(Object2IntMap unmappedIdentifiers, Object2IntMap mappedIdentifiers, JsonObject diffMapping, boolean warnOnMissing) { Int2ObjectMap itemMapping = new Int2ObjectOpenHashMap<>(diffMapping.size(), 0.99F); - Object2IntMap newIdentifierMap = MappingDataLoader.indexedObjectToMap(newMapping); - Object2IntMap oldIdentifierMap = MappingDataLoader.indexedObjectToMap(oldMapping); for (Map.Entry entry : diffMapping.entrySet()) { JsonObject object = entry.getValue().getAsJsonObject(); - String mappedIdName = object.getAsJsonPrimitive("id").getAsString(); - int mappedId = newIdentifierMap.getInt(mappedIdName); + String mappedIdentifier = object.getAsJsonPrimitive("id").getAsString(); + int mappedId = mappedIdentifiers.getInt(mappedIdentifier); if (mappedId == -1) { if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - ViaBackwards.getPlatform().getLogger().warning("No key for " + mappedIdName + " :( "); + ViaBackwards.getPlatform().getLogger().warning("Mapped item " + mappedIdentifier + " does not exist :( "); } continue; } - int oldId = oldIdentifierMap.getInt(entry.getKey()); - if (oldId == -1) { + int unmappedId = unmappedIdentifiers.getInt(entry.getKey()); + if (unmappedId == -1) { if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { - ViaBackwards.getPlatform().getLogger().warning("No old entry for " + mappedIdName + " :( "); + ViaBackwards.getPlatform().getLogger().warning("Unmapped item " + entry.getKey() + " doesn't exist :( "); } continue; } String name = object.getAsJsonPrimitive("name").getAsString(); JsonPrimitive customModelData = object.getAsJsonPrimitive("custom_model_data"); - itemMapping.put(oldId, new MappedItem(mappedId, name, customModelData != null ? customModelData.getAsInt() : null)); + itemMapping.put(unmappedId, new MappedItem(mappedId, name, customModelData != null ? customModelData.getAsInt() : null)); } // Look for missing keys if (warnOnMissing && !Via.getConfig().isSuppressConversionWarnings()) { - for (Object2IntMap.Entry entry : oldIdentifierMap.object2IntEntrySet()) { - if (!newIdentifierMap.containsKey(entry.getKey()) && !itemMapping.containsKey(entry.getIntValue())) { + for (Object2IntMap.Entry entry : unmappedIdentifiers.object2IntEntrySet()) { + if (!mappedIdentifiers.containsKey(entry.getKey()) && !itemMapping.containsKey(entry.getIntValue())) { ViaBackwards.getPlatform().getLogger().warning("No item mapping for " + entry.getKey() + " :( "); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappings.java b/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappings.java index 6430fea4..884b423a 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/data/VBMappings.java @@ -49,15 +49,14 @@ public final class VBMappings extends IntArrayMappings { if (mapped.isJsonObject()) { VBMappingDataLoader.mapIdentifiers(mappings, toJsonObject(unmapped.getAsJsonArray()), mapped.getAsJsonObject(), diffMappings, warnOnMissing); } else { - // Use the normal loader - MappingDataLoader.mapIdentifiers(mappings, unmapped.getAsJsonArray(), mapped.getAsJsonArray(), diffMappings, warnOnMissing); + VBMappingDataLoader.mapIdentifiers(mappings, unmapped.getAsJsonArray(), mapped.getAsJsonArray(), diffMappings, warnOnMissing); } } else if (mapped.isJsonArray()) { VBMappingDataLoader.mapIdentifiers(mappings, unmapped.getAsJsonObject(), toJsonObject(mapped.getAsJsonArray()), diffMappings, warnOnMissing); } else { VBMappingDataLoader.mapIdentifiers(mappings, unmapped.getAsJsonObject(), mapped.getAsJsonObject(), diffMappings, warnOnMissing); } - return supplier.supply(mappings, mappedSize); + return new VBMappings(mappings, mappedSize); } } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java index 33fa90dc..3b9eee44 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java @@ -22,14 +22,13 @@ import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.providers.Backw import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.data.MappingDataLoader; -import com.viaversion.viaversion.libs.gson.JsonElement; -import com.viaversion.viaversion.libs.gson.JsonObject; +import com.viaversion.viaversion.libs.gson.JsonArray; +import com.viaversion.viaversion.libs.gson.JsonPrimitive; import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.blockconnections.ConnectionData; -import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; import java.util.StringJoiner; @@ -40,28 +39,24 @@ public class PistonHandler implements BackwardsBlockEntityProvider.BackwardsBloc public PistonHandler() { if (Via.getConfig().isServersideBlockConnections()) { - Map keyToId; - try { - Field field = ConnectionData.class.getDeclaredField("keyToId"); - field.setAccessible(true); - keyToId = (Map) field.get(null); - } catch (IllegalAccessException | NoSuchFieldException e) { - e.printStackTrace(); - return; - } - + Map keyToId = ConnectionData.getKeyToId(); for (Map.Entry entry : keyToId.entrySet()) { - if (!entry.getKey().contains("piston")) continue; + if (!entry.getKey().contains("piston")) { + continue; + } addEntries(entry.getKey(), entry.getValue()); } } else { - JsonObject mappings = MappingDataLoader.getMappingsCache().get("mapping-1.13.json").getAsJsonObject("blockstates"); - for (Map.Entry blockState : mappings.entrySet()) { - String key = blockState.getValue().getAsString(); - if (!key.contains("piston")) continue; + JsonArray mappings = MappingDataLoader.getMappingsCache().get("mapping-1.13.json").getAsJsonArray("blockstates"); + for (int id = 0; id < mappings.size(); id++) { + JsonPrimitive state = mappings.get(id).getAsJsonPrimitive(); + String key = state.getAsString(); + if (!key.contains("piston")) { + continue; + } - addEntries(key, Integer.parseInt(blockState.getKey())); + addEntries(key, id); } } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java index 8a0020c2..efa5d5df 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/BackwardsMappings.java @@ -19,6 +19,7 @@ package com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data; import com.viaversion.viabackwards.ViaBackwards; +import com.viaversion.viabackwards.api.data.VBMappingDataLoader; import com.viaversion.viabackwards.api.data.VBMappings; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.data.IntArrayMappings; @@ -27,6 +28,7 @@ import com.viaversion.viaversion.api.data.Mappings; import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap; import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap; import com.viaversion.viaversion.libs.fastutil.objects.Object2IntMap; +import com.viaversion.viaversion.libs.gson.JsonArray; import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.libs.gson.JsonObject; import com.viaversion.viaversion.libs.gson.JsonPrimitive; @@ -46,9 +48,14 @@ public class BackwardsMappings extends com.viaversion.viabackwards.api.data.Back } @Override - public void loadVBExtras(JsonObject unmapped, JsonObject mapped) { + public void loadVBExtras(JsonObject unmappedIdentifiers, JsonObject mappedIdentifiers, JsonObject diffMappings) { + JsonObject diffItems = diffMappings.getAsJsonObject("items"); + JsonArray unmappedItems = unmappedIdentifiers.getAsJsonArray("items"); + JsonObject mappedItems = mappedIdentifiers.getAsJsonObject("items"); + backwardsItemMappings = VBMappingDataLoader.loadItemMappings(MappingDataLoader.arrayToMap(unmappedItems), MappingDataLoader.indexedObjectToMap(mappedItems), diffItems, false); + enchantmentMappings = VBMappings.vbBuilder().warnOnMissing(false) - .unmapped(unmapped.getAsJsonArray("enchantments")).mapped(mapped.getAsJsonObject("legacy_enchantments")).build(); + .unmapped(unmappedIdentifiers.getAsJsonArray("enchantments")).mapped(mappedIdentifiers.getAsJsonObject("legacy_enchantments")).build(); for (Map.Entry entry : StatisticMappings.CUSTOM_STATS.entrySet()) { statisticMappings.put(entry.getValue().intValue(), entry.getKey()); } @@ -95,17 +102,26 @@ public class BackwardsMappings extends com.viaversion.viabackwards.api.data.Back } @Override - protected @Nullable Mappings loadFromObject(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings, String key) { + protected @Nullable Mappings loadFromArray(JsonObject unmappedIdentifiers, JsonObject mappedIdentifiers, @Nullable JsonObject diffMappings, String key) { if (key.equals("blockstates")) { int[] oldToNew = new int[8582]; Arrays.fill(oldToNew, -1); - mapIdentifiers(oldToNew, oldMappings.getAsJsonObject("blockstates"), newMappings.getAsJsonObject("blocks"), diffMappings.getAsJsonObject("blockstates")); + mapIdentifiers(oldToNew, toJsonObject(unmappedIdentifiers.getAsJsonArray("blockstates")), mappedIdentifiers.getAsJsonObject("blocks"), diffMappings.getAsJsonObject("blockstates")); return IntArrayMappings.of(oldToNew, -1); } else { - return super.loadFromObject(oldMappings, newMappings, diffMappings, key); + return super.loadFromArray(unmappedIdentifiers, mappedIdentifiers, diffMappings, key); } } + private JsonObject toJsonObject(final JsonArray array) { + final JsonObject object = new JsonObject(); + for (int i = 0; i < array.size(); i++) { + final JsonElement element = array.get(i); + object.add(Integer.toString(i), element); + } + return object; + } + @Override public int getNewBlockStateId(int id) { // Comparator funkyness: https://github.com/ViaVersion/ViaBackwards/issues/524 @@ -156,4 +172,9 @@ public class BackwardsMappings extends com.viaversion.viabackwards.api.data.Back public Map getTranslateMappings() { return translateMappings; } + + @Override + protected boolean shouldLoad(final String key) { + return super.shouldLoad(key) && !key.equals("blocks"); + } } 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 780cc7f0..09113fd4 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 @@ -91,7 +91,7 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit protected void registerPackets() { protocol.registerClientbound(ClientboundPackets1_13.COOLDOWN, wrapper -> { int itemId = wrapper.read(Type.VAR_INT); - int oldId = protocol.getMappingData().getItemMappings().get(itemId); + int oldId = protocol.getMappingData().getItemMappings().getNewId(itemId); if (oldId == -1) { wrapper.cancel(); return; @@ -376,7 +376,7 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit int id = wrapper.get(Type.INT, 0); int data = wrapper.get(Type.INT, 1); if (id == 1010) { // Play record - wrapper.set(Type.INT, 1, protocol.getMappingData().getItemMappings().get(data) >> 4); + wrapper.set(Type.INT, 1, protocol.getMappingData().getItemMappings().getNewId(data) >> 4); } else if (id == 2001) { // Block break + block break sound data = protocol.getMappingData().getNewBlockStateId(data); int blockId = data >> 4; @@ -804,7 +804,7 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit item.setIdentifier(identifier); int newId = -1; - if (!protocol.getMappingData().getItemMappings().inverse().containsKey(rawId)) { + if (protocol.getMappingData().getItemMappings().inverse().getNewId(rawId) == -1) { if (!isDamageable(item.identifier()) && item.identifier() != 358) { // Map if (tag == null) item.setTag(tag = new CompoundTag()); tag.put(extraNbtTag, new IntTag(originalId)); // Data will be lost, saving original id @@ -814,7 +814,7 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit 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 - } else if (protocol.getMappingData().getItemMappings().inverse().containsKey(rawId & ~0xF)) { + } else if (protocol.getMappingData().getItemMappings().inverse().getNewId(rawId & ~0xF) != -1) { rawId &= ~0xF; // Remove data } else { if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { @@ -825,7 +825,7 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit } if (newId == -1) { - newId = protocol.getMappingData().getItemMappings().inverse().get(rawId); + newId = protocol.getMappingData().getItemMappings().inverse().getNewId(rawId); } item.setIdentifier(newId); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java index 6bc314be..20217529 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java @@ -80,7 +80,7 @@ public class Protocol1_13_2To1_14 extends BackwardsProtocol entry : Protocol1_16To1_15_2.MAPPINGS.getAttributeMappings().entrySet()) { attributeMappings.put(entry.getValue(), entry.getKey()); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java index 12459a0f..b6021b3b 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java @@ -47,6 +47,7 @@ import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.types.Chunk1_16T import com.viaversion.viaversion.rewriter.BlockRewriter; import com.viaversion.viaversion.rewriter.RecipeRewriter; import com.viaversion.viaversion.util.CompactArrayUtil; +import com.viaversion.viaversion.util.Key; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -71,7 +72,7 @@ public class BlockItemPackets1_16 extends com.viaversion.viabackwards.api.rewrit int newSize = size; for (int i = 0; i < size; i++) { String originalType = wrapper.read(Type.STRING); - String type = originalType.replace("minecraft:", ""); + String type = Key.stripMinecraftNamespace(originalType); if (type.equals("smithing")) { newSize--; diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java index e3eb81e4..4f147754 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java @@ -41,6 +41,7 @@ import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; +import com.viaversion.viaversion.util.Key; public class EntityPackets1_16 extends EntityRewriter { @@ -190,7 +191,7 @@ public class EntityPackets1_16 extends EntityRewriter entry : LEGACY_BIOMES.object2IntEntrySet()) { - MODERN_TO_LEGACY_ID.put("minecraft:" + entry.getKey(), entry.getIntValue()); + MODERN_TO_LEGACY_ID.put(entry.getKey(), entry.getIntValue()); } final JsonObject mappings = VBMappingDataLoader.loadFromDataDir("biome-mappings.json"); @@ -133,10 +134,7 @@ public final class BiomeMappings { } public static int toLegacyBiome(String biome) { - if (biome.indexOf(':') == -1) { - biome = "minecraft:" + biome; - } - final int legacyBiome = MODERN_TO_LEGACY_ID.getInt(biome); + final int legacyBiome = MODERN_TO_LEGACY_ID.getInt(Key.stripMinecraftNamespace(biome)); if (legacyBiome == -1) { if (!Via.getConfig().isSuppressConversionWarnings()) { ViaBackwards.getPlatform().getLogger().warning("Biome with id " + biome + " has no legacy biome mapping (custom datapack?)"); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/Protocol1_16_4To1_17.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/Protocol1_16_4To1_17.java index 5458dfab..09a13e19 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/Protocol1_16_4To1_17.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/Protocol1_16_4To1_17.java @@ -44,6 +44,7 @@ import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPacke import com.viaversion.viaversion.rewriter.IdRewriteFunction; import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; +import com.viaversion.viaversion.util.Key; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -84,11 +85,7 @@ public final class Protocol1_16_4To1_17 extends BackwardsProtocol tagList = new ArrayList<>(); tags.put(resourceKey, tagList); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/data/BackwardsMappings.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/data/BackwardsMappings.java index 4999eb7e..1b35b425 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/data/BackwardsMappings.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/data/BackwardsMappings.java @@ -32,7 +32,7 @@ public final class BackwardsMappings extends com.viaversion.viabackwards.api.dat } @Override - protected void loadVBExtras(final JsonObject unmapped, final JsonObject mapped) { + protected void loadVBExtras(final JsonObject unmappedIdentifiers, final JsonObject mappedIdentifiers, JsonObject diffMappings) { for (final Object2IntMap.Entry entry : Protocol1_18To1_17_1.MAPPINGS.blockEntityIds().object2IntEntrySet()) { blockEntities.put(entry.getIntValue(), entry.getKey()); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/packets/BlockItemPackets1_18.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/packets/BlockItemPackets1_18.java index d614f806..71b7d24e 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/packets/BlockItemPackets1_18.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_17_1to1_18/packets/BlockItemPackets1_18.java @@ -41,6 +41,7 @@ import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17T import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.ClientboundPackets1_18; import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.types.Chunk1_18Type; import com.viaversion.viaversion.rewriter.RecipeRewriter; +import com.viaversion.viaversion.util.Key; import com.viaversion.viaversion.util.MathUtil; import java.util.ArrayList; import java.util.BitSet; @@ -151,7 +152,7 @@ public final class BlockItemPackets1_18 extends ItemRewriter { @@ -68,7 +69,7 @@ public final class BlockItemPackets1_19_3 extends ItemRewriter { final int size = wrapper.passthrough(Type.VAR_INT); for (int i = 0; i < size; i++) { - final String type = wrapper.passthrough(Type.STRING).replace("minecraft:", ""); + final String type = Key.stripMinecraftNamespace(wrapper.passthrough(Type.STRING)); wrapper.passthrough(Type.STRING); // Recipe Identifier switch (type) { case "crafting_shapeless": { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_3to1_19_4/packets/BlockItemPackets1_19_4.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_3to1_19_4/packets/BlockItemPackets1_19_4.java index 49e68ab1..cca28902 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_3to1_19_4/packets/BlockItemPackets1_19_4.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_3to1_19_4/packets/BlockItemPackets1_19_4.java @@ -27,6 +27,7 @@ import com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.ServerboundPac import com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.rewriter.RecipeRewriter1_19_3; import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ClientboundPackets1_19_4; import com.viaversion.viaversion.rewriter.BlockRewriter; +import com.viaversion.viaversion.util.Key; public final class BlockItemPackets1_19_4 extends ItemRewriter { @@ -94,7 +95,7 @@ public final class BlockItemPackets1_19_4 extends ItemRewriter