From 68a32ed2f0e588859456ebdf12e69b80ec59d177 Mon Sep 17 00:00:00 2001 From: RK_01 <50594595+RaphiMC@users.noreply.github.com> Date: Mon, 23 Oct 2023 23:58:43 +0000 Subject: [PATCH] Fix identifier handling and validation (#3498) --- .../com/viaversion/viaversion/util/Key.java | 12 +- .../BlockEntityRewriter.java | 3 +- .../protocol1_11to1_10/EntityIdRewriter.java | 155 +++++++++--------- .../packets/InventoryPackets.java | 3 +- .../FireConnectionHandler.java | 4 +- .../data/EntityNameRewriter.java | 6 +- .../data/MappingData.java | 25 +-- .../data/SpawnEggRewriter.java | 92 ++++++----- .../packets/InventoryPackets.java | 18 +- .../packets/WorldPackets.java | 14 +- .../providers/PaintingProvider.java | 11 +- .../blockentities/FlowerPotHandler.java | 4 +- .../packets/EntityPackets.java | 14 +- .../protocol1_9to1_8/ItemRewriter.java | 8 +- 14 files changed, 187 insertions(+), 182 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/util/Key.java b/api/src/main/java/com/viaversion/viaversion/util/Key.java index e085555aa..fa80515ee 100644 --- a/api/src/main/java/com/viaversion/viaversion/util/Key.java +++ b/api/src/main/java/com/viaversion/viaversion/util/Key.java @@ -35,14 +35,24 @@ public final class Key { public static String stripMinecraftNamespace(final String identifier) { if (identifier.startsWith("minecraft:")) { return identifier.substring(10); + } else if (identifier.startsWith(":")) { + return identifier.substring(1); } return identifier; } public static String namespaced(final String identifier) { - if (identifier.indexOf(':') == -1) { + final int index = identifier.indexOf(':'); + if (index == -1) { return "minecraft:" + identifier; + } else if (index == 0) { + return "minecraft" + identifier; } return identifier; } + + public static boolean isValid(final String identifier) { + return identifier.matches("([0-9a-z_.-]*:)?[0-9a-z_/.-]*"); + } + } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/BlockEntityRewriter.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/BlockEntityRewriter.java index 0feaf8329..18405ee0a 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/BlockEntityRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/BlockEntityRewriter.java @@ -19,6 +19,7 @@ package com.viaversion.viaversion.protocols.protocol1_11to1_10; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; +import com.viaversion.viaversion.util.Key; public class BlockEntityRewriter { private static BiMap oldToNewNames = HashBiMap.create(); @@ -51,7 +52,7 @@ public class BlockEntityRewriter { } private static void rewrite(String oldName, String newName) { - oldToNewNames.put(oldName, "minecraft:" + newName); + oldToNewNames.put(oldName, Key.namespaced(newName)); } public static BiMap inverse() { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/EntityIdRewriter.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/EntityIdRewriter.java index 61268c3a8..e3f07d575 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/EntityIdRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/EntityIdRewriter.java @@ -23,86 +23,91 @@ import com.github.steveice10.opennbt.tag.builtin.Tag; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import com.viaversion.viaversion.api.minecraft.item.Item; +import com.viaversion.viaversion.util.Key; public class EntityIdRewriter { private static final BiMap oldToNewNames = HashBiMap.create(); static { - oldToNewNames.put("AreaEffectCloud", "minecraft:area_effect_cloud"); - oldToNewNames.put("ArmorStand", "minecraft:armor_stand"); - oldToNewNames.put("Arrow", "minecraft:arrow"); - oldToNewNames.put("Bat", "minecraft:bat"); - oldToNewNames.put("Blaze", "minecraft:blaze"); - oldToNewNames.put("Boat", "minecraft:boat"); - oldToNewNames.put("CaveSpider", "minecraft:cave_spider"); - oldToNewNames.put("Chicken", "minecraft:chicken"); - oldToNewNames.put("Cow", "minecraft:cow"); - oldToNewNames.put("Creeper", "minecraft:creeper"); - oldToNewNames.put("Donkey", "minecraft:donkey"); - oldToNewNames.put("DragonFireball", "minecraft:dragon_fireball"); - oldToNewNames.put("ElderGuardian", "minecraft:elder_guardian"); - oldToNewNames.put("EnderCrystal", "minecraft:ender_crystal"); - oldToNewNames.put("EnderDragon", "minecraft:ender_dragon"); - oldToNewNames.put("Enderman", "minecraft:enderman"); - oldToNewNames.put("Endermite", "minecraft:endermite"); - oldToNewNames.put("EntityHorse", "minecraft:horse"); - oldToNewNames.put("EyeOfEnderSignal", "minecraft:eye_of_ender_signal"); - oldToNewNames.put("FallingSand", "minecraft:falling_block"); - oldToNewNames.put("Fireball", "minecraft:fireball"); - oldToNewNames.put("FireworksRocketEntity", "minecraft:fireworks_rocket"); - oldToNewNames.put("Ghast", "minecraft:ghast"); - oldToNewNames.put("Giant", "minecraft:giant"); - oldToNewNames.put("Guardian", "minecraft:guardian"); - oldToNewNames.put("Husk", "minecraft:husk"); - oldToNewNames.put("Item", "minecraft:item"); - oldToNewNames.put("ItemFrame", "minecraft:item_frame"); - oldToNewNames.put("LavaSlime", "minecraft:magma_cube"); - oldToNewNames.put("LeashKnot", "minecraft:leash_knot"); - oldToNewNames.put("MinecartChest", "minecraft:chest_minecart"); - oldToNewNames.put("MinecartCommandBlock", "minecraft:commandblock_minecart"); - oldToNewNames.put("MinecartFurnace", "minecraft:furnace_minecart"); - oldToNewNames.put("MinecartHopper", "minecraft:hopper_minecart"); - oldToNewNames.put("MinecartRideable", "minecraft:minecart"); - oldToNewNames.put("MinecartSpawner", "minecraft:spawner_minecart"); - oldToNewNames.put("MinecartTNT", "minecraft:tnt_minecart"); - oldToNewNames.put("Mule", "minecraft:mule"); - oldToNewNames.put("MushroomCow", "minecraft:mooshroom"); - oldToNewNames.put("Ozelot", "minecraft:ocelot"); - oldToNewNames.put("Painting", "minecraft:painting"); - oldToNewNames.put("Pig", "minecraft:pig"); - oldToNewNames.put("PigZombie", "minecraft:zombie_pigman"); - oldToNewNames.put("PolarBear", "minecraft:polar_bear"); - oldToNewNames.put("PrimedTnt", "minecraft:tnt"); - oldToNewNames.put("Rabbit", "minecraft:rabbit"); - oldToNewNames.put("Sheep", "minecraft:sheep"); - oldToNewNames.put("Shulker", "minecraft:shulker"); - oldToNewNames.put("ShulkerBullet", "minecraft:shulker_bullet"); - oldToNewNames.put("Silverfish", "minecraft:silverfish"); - oldToNewNames.put("Skeleton", "minecraft:skeleton"); - oldToNewNames.put("SkeletonHorse", "minecraft:skeleton_horse"); - oldToNewNames.put("Slime", "minecraft:slime"); - oldToNewNames.put("SmallFireball", "minecraft:small_fireball"); - oldToNewNames.put("Snowball", "minecraft:snowball"); - oldToNewNames.put("SnowMan", "minecraft:snowman"); - oldToNewNames.put("SpectralArrow", "minecraft:spectral_arrow"); - oldToNewNames.put("Spider", "minecraft:spider"); - oldToNewNames.put("Squid", "minecraft:squid"); - oldToNewNames.put("Stray", "minecraft:stray"); - oldToNewNames.put("ThrownEgg", "minecraft:egg"); - oldToNewNames.put("ThrownEnderpearl", "minecraft:ender_pearl"); - oldToNewNames.put("ThrownExpBottle", "minecraft:xp_bottle"); - oldToNewNames.put("ThrownPotion", "minecraft:potion"); - oldToNewNames.put("Villager", "minecraft:villager"); - oldToNewNames.put("VillagerGolem", "minecraft:villager_golem"); - oldToNewNames.put("Witch", "minecraft:witch"); - oldToNewNames.put("WitherBoss", "minecraft:wither"); - oldToNewNames.put("WitherSkeleton", "minecraft:wither_skeleton"); - oldToNewNames.put("WitherSkull", "minecraft:wither_skull"); - oldToNewNames.put("Wolf", "minecraft:wolf"); - oldToNewNames.put("XPOrb", "minecraft:xp_orb"); - oldToNewNames.put("Zombie", "minecraft:zombie"); - oldToNewNames.put("ZombieHorse", "minecraft:zombie_horse"); - oldToNewNames.put("ZombieVillager", "minecraft:zombie_villager"); + rewrite("AreaEffectCloud", "area_effect_cloud"); + rewrite("ArmorStand", "armor_stand"); + rewrite("Arrow", "arrow"); + rewrite("Bat", "bat"); + rewrite("Blaze", "blaze"); + rewrite("Boat", "boat"); + rewrite("CaveSpider", "cave_spider"); + rewrite("Chicken", "chicken"); + rewrite("Cow", "cow"); + rewrite("Creeper", "creeper"); + rewrite("Donkey", "donkey"); + rewrite("DragonFireball", "dragon_fireball"); + rewrite("ElderGuardian", "elder_guardian"); + rewrite("EnderCrystal", "ender_crystal"); + rewrite("EnderDragon", "ender_dragon"); + rewrite("Enderman", "enderman"); + rewrite("Endermite", "endermite"); + rewrite("EntityHorse", "horse"); + rewrite("EyeOfEnderSignal", "eye_of_ender_signal"); + rewrite("FallingSand", "falling_block"); + rewrite("Fireball", "fireball"); + rewrite("FireworksRocketEntity", "fireworks_rocket"); + rewrite("Ghast", "ghast"); + rewrite("Giant", "giant"); + rewrite("Guardian", "guardian"); + rewrite("Husk", "husk"); + rewrite("Item", "item"); + rewrite("ItemFrame", "item_frame"); + rewrite("LavaSlime", "magma_cube"); + rewrite("LeashKnot", "leash_knot"); + rewrite("MinecartChest", "chest_minecart"); + rewrite("MinecartCommandBlock", "commandblock_minecart"); + rewrite("MinecartFurnace", "furnace_minecart"); + rewrite("MinecartHopper", "hopper_minecart"); + rewrite("MinecartRideable", "minecart"); + rewrite("MinecartSpawner", "spawner_minecart"); + rewrite("MinecartTNT", "tnt_minecart"); + rewrite("Mule", "mule"); + rewrite("MushroomCow", "mooshroom"); + rewrite("Ozelot", "ocelot"); + rewrite("Painting", "painting"); + rewrite("Pig", "pig"); + rewrite("PigZombie", "zombie_pigman"); + rewrite("PolarBear", "polar_bear"); + rewrite("PrimedTnt", "tnt"); + rewrite("Rabbit", "rabbit"); + rewrite("Sheep", "sheep"); + rewrite("Shulker", "shulker"); + rewrite("ShulkerBullet", "shulker_bullet"); + rewrite("Silverfish", "silverfish"); + rewrite("Skeleton", "skeleton"); + rewrite("SkeletonHorse", "skeleton_horse"); + rewrite("Slime", "slime"); + rewrite("SmallFireball", "small_fireball"); + rewrite("Snowball", "snowball"); + rewrite("SnowMan", "snowman"); + rewrite("SpectralArrow", "spectral_arrow"); + rewrite("Spider", "spider"); + rewrite("Squid", "squid"); + rewrite("Stray", "stray"); + rewrite("ThrownEgg", "egg"); + rewrite("ThrownEnderpearl", "ender_pearl"); + rewrite("ThrownExpBottle", "xp_bottle"); + rewrite("ThrownPotion", "potion"); + rewrite("Villager", "villager"); + rewrite("VillagerGolem", "villager_golem"); + rewrite("Witch", "witch"); + rewrite("WitherBoss", "wither"); + rewrite("WitherSkeleton", "wither_skeleton"); + rewrite("WitherSkull", "wither_skull"); + rewrite("Wolf", "wolf"); + rewrite("XPOrb", "xp_orb"); + rewrite("Zombie", "zombie"); + rewrite("ZombieHorse", "zombie_horse"); + rewrite("ZombieVillager", "zombie_villager"); + } + + private static void rewrite(String oldName, String newName) { + oldToNewNames.put(oldName, Key.namespaced(newName)); } public static void toClient(CompoundTag tag) { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/packets/InventoryPackets.java index e9b449522..9cea87e02 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/packets/InventoryPackets.java @@ -25,6 +25,7 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPacke import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; import com.viaversion.viaversion.rewriter.ItemRewriter; import com.viaversion.viaversion.rewriter.RecipeRewriter; +import com.viaversion.viaversion.util.Key; public class InventoryPackets extends ItemRewriter { @@ -88,7 +89,7 @@ public class InventoryPackets extends ItemRewriter set, String suffix) { for (String woodType : WOOD_TYPES) { - set.add("minecraft:" + woodType + suffix); + set.add(Key.namespaced(woodType + suffix)); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/EntityNameRewriter.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/EntityNameRewriter.java index 6e290382d..7c6aaf575 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/EntityNameRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/EntityNameRewriter.java @@ -17,6 +17,8 @@ */ package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data; +import com.viaversion.viaversion.util.Key; + import java.util.HashMap; import java.util.Map; @@ -59,7 +61,7 @@ public class EntityNameRewriter { private static void reg(String past, String future) { - entityNames.put("minecraft:" + past, "minecraft:" + future); + entityNames.put(Key.namespaced(past), Key.namespaced(future)); } public static String rewrite(String entName) { @@ -67,7 +69,7 @@ public class EntityNameRewriter { if (entityName != null) { return entityName; } - entityName = entityNames.get("minecraft:" + entName); + entityName = entityNames.get(Key.namespaced(entName)); if (entityName != null) { return entityName; } else diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/MappingData.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/MappingData.java index 54b4e1425..5a8161171 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/MappingData.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/MappingData.java @@ -28,21 +28,18 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; import com.viaversion.viaversion.api.Via; -import com.viaversion.viaversion.api.data.BiMappings; -import com.viaversion.viaversion.api.data.Int2IntMapBiMappings; -import com.viaversion.viaversion.api.data.MappingDataBase; -import com.viaversion.viaversion.api.data.MappingDataLoader; -import com.viaversion.viaversion.api.data.Mappings; +import com.viaversion.viaversion.api.data.*; import com.viaversion.viaversion.util.GsonUtil; import com.viaversion.viaversion.util.Int2IntBiHashMap; import com.viaversion.viaversion.util.Key; +import org.checkerframework.checker.nullness.qual.Nullable; + import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; -import org.checkerframework.checker.nullness.qual.Nullable; public class MappingData extends MappingDataBase { private final Map blockTags = new HashMap<>(); @@ -86,7 +83,7 @@ public class MappingData extends MappingDataBase { for (Map.Entry entry : object.entrySet()) { String oldChannel = entry.getKey(); String newChannel = entry.getValue().getAsString(); - if (!isValid1_13Channel(newChannel)) { + if (!Key.isValid(newChannel)) { Via.getPlatform().getLogger().warning("Channel '" + newChannel + "' is not a valid 1.13 plugin channel, please check your configuration!"); continue; } @@ -156,21 +153,11 @@ public class MappingData extends MappingDataBase { } public static String validateNewChannel(String newId) { - if (!isValid1_13Channel(newId)) { + if (!Key.isValid(newId)) { return null; // Not valid } - int separatorIndex = newId.indexOf(':'); - // Vanilla parses an empty and a missing namespace as the minecraft namespace - if (separatorIndex == -1) { - return "minecraft:" + newId; - } else if (separatorIndex == 0) { - return "minecraft" + newId; - } - return newId; - } - public static boolean isValid1_13Channel(String channelId) { - return channelId.matches("([0-9a-z_.-]+:)?[0-9a-z_/.-]+"); + return Key.namespaced(newId); } private void loadTags(Map output, CompoundTag newTags) { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/SpawnEggRewriter.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/SpawnEggRewriter.java index 28a0ee10c..ef121dcc4 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/SpawnEggRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/SpawnEggRewriter.java @@ -19,6 +19,8 @@ package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; +import com.viaversion.viaversion.util.Key; + import java.util.Optional; public class SpawnEggRewriter { @@ -27,54 +29,54 @@ public class SpawnEggRewriter { static { // Class yz.java in 18w14b // Register spawn eggs (generated with GT) - registerSpawnEgg("minecraft:bat"); - registerSpawnEgg("minecraft:blaze"); - registerSpawnEgg("minecraft:cave_spider"); - registerSpawnEgg("minecraft:chicken"); - registerSpawnEgg("minecraft:cow"); - registerSpawnEgg("minecraft:creeper"); - registerSpawnEgg("minecraft:donkey"); - registerSpawnEgg("minecraft:elder_guardian"); - registerSpawnEgg("minecraft:enderman"); - registerSpawnEgg("minecraft:endermite"); - registerSpawnEgg("minecraft:evocation_illager"); - registerSpawnEgg("minecraft:ghast"); - registerSpawnEgg("minecraft:guardian"); - registerSpawnEgg("minecraft:horse"); - registerSpawnEgg("minecraft:husk"); - registerSpawnEgg("minecraft:llama"); - registerSpawnEgg("minecraft:magma_cube"); - registerSpawnEgg("minecraft:mooshroom"); - registerSpawnEgg("minecraft:mule"); - registerSpawnEgg("minecraft:ocelot"); + registerSpawnEgg("bat"); + registerSpawnEgg("blaze"); + registerSpawnEgg("cave_spider"); + registerSpawnEgg("chicken"); + registerSpawnEgg("cow"); + registerSpawnEgg("creeper"); + registerSpawnEgg("donkey"); + registerSpawnEgg("elder_guardian"); + registerSpawnEgg("enderman"); + registerSpawnEgg("endermite"); + registerSpawnEgg("evocation_illager"); + registerSpawnEgg("ghast"); + registerSpawnEgg("guardian"); + registerSpawnEgg("horse"); + registerSpawnEgg("husk"); + registerSpawnEgg("llama"); + registerSpawnEgg("magma_cube"); + registerSpawnEgg("mooshroom"); + registerSpawnEgg("mule"); + registerSpawnEgg("ocelot"); - registerSpawnEgg("minecraft:parrot"); - registerSpawnEgg("minecraft:pig"); - registerSpawnEgg("minecraft:polar_bear"); - registerSpawnEgg("minecraft:rabbit"); - registerSpawnEgg("minecraft:sheep"); - registerSpawnEgg("minecraft:shulker"); - registerSpawnEgg("minecraft:silverfish"); - registerSpawnEgg("minecraft:skeleton"); - registerSpawnEgg("minecraft:skeleton_horse"); - registerSpawnEgg("minecraft:slime"); - registerSpawnEgg("minecraft:spider"); - registerSpawnEgg("minecraft:squid"); - registerSpawnEgg("minecraft:stray"); - registerSpawnEgg("minecraft:vex"); - registerSpawnEgg("minecraft:villager"); - registerSpawnEgg("minecraft:vindication_illager"); - registerSpawnEgg("minecraft:witch"); - registerSpawnEgg("minecraft:wither_skeleton"); - registerSpawnEgg("minecraft:wolf"); - registerSpawnEgg("minecraft:zombie"); - registerSpawnEgg("minecraft:zombie_horse"); - registerSpawnEgg("minecraft:zombie_pigman"); - registerSpawnEgg("minecraft:zombie_villager"); + registerSpawnEgg("parrot"); + registerSpawnEgg("pig"); + registerSpawnEgg("polar_bear"); + registerSpawnEgg("rabbit"); + registerSpawnEgg("sheep"); + registerSpawnEgg("shulker"); + registerSpawnEgg("silverfish"); + registerSpawnEgg("skeleton"); + registerSpawnEgg("skeleton_horse"); + registerSpawnEgg("slime"); + registerSpawnEgg("spider"); + registerSpawnEgg("squid"); + registerSpawnEgg("stray"); + registerSpawnEgg("vex"); + registerSpawnEgg("villager"); + registerSpawnEgg("vindication_illager"); + registerSpawnEgg("witch"); + registerSpawnEgg("wither_skeleton"); + registerSpawnEgg("wolf"); + registerSpawnEgg("zombie"); + registerSpawnEgg("zombie_horse"); + registerSpawnEgg("zombie_pigman"); + registerSpawnEgg("zombie_villager"); } - private static void registerSpawnEgg(String key) { - spawnEggs.put(key, spawnEggs.size()); + private static void registerSpawnEgg(String name) { + spawnEggs.put(Key.namespaced(name), spawnEggs.size()); } // Make it a non-existing block id diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java index dfa677b29..2f6cc3198 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java @@ -18,13 +18,7 @@ package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets; import com.github.steveice10.opennbt.conversion.ConverterRegistry; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.opennbt.tag.builtin.IntTag; -import com.github.steveice10.opennbt.tag.builtin.ListTag; -import com.github.steveice10.opennbt.tag.builtin.NumberTag; -import com.github.steveice10.opennbt.tag.builtin.ShortTag; -import com.github.steveice10.opennbt.tag.builtin.StringTag; -import com.github.steveice10.opennbt.tag.builtin.Tag; +import com.github.steveice10.opennbt.tag.builtin.*; import com.google.common.base.Joiner; import com.google.common.primitives.Ints; import com.viaversion.viaversion.api.Via; @@ -41,6 +35,8 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.MappingData import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.SoundSource; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.SpawnEggRewriter; import com.viaversion.viaversion.rewriter.ItemRewriter; +import com.viaversion.viaversion.util.Key; + import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -364,7 +360,7 @@ public class InventoryPackets extends ItemRewriter { - String sound = wrapper.get(Type.STRING, 0).replace("minecraft:", ""); + String sound = Key.stripMinecraftNamespace(wrapper.get(Type.STRING, 0)); String newSoundId = NamedSoundRewriter.getNewId(sound); wrapper.set(Type.STRING, 0, newSoundId); }); @@ -445,7 +445,7 @@ public class WorldPackets { final Tag idTag = tag.get("id"); if (idTag instanceof StringTag) { // No longer block entities - final String id = ((StringTag) idTag).getValue(); + final String id = Key.namespaced(((StringTag) idTag).getValue()); if (id.equals("minecraft:noteblock") || id.equals("minecraft:flower_pot")) { iterator.remove(); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/PaintingProvider.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/PaintingProvider.java index 9d02d71ca..f8665d975 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/PaintingProvider.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/PaintingProvider.java @@ -18,6 +18,8 @@ package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers; import com.viaversion.viaversion.api.platform.providers.Provider; +import com.viaversion.viaversion.util.Key; + import java.util.HashMap; import java.util.Locale; import java.util.Map; @@ -56,13 +58,10 @@ public class PaintingProvider implements Provider { } private void add(String motive) { - paintings.put("minecraft:" + motive, paintings.size()); + paintings.put(Key.namespaced(motive), paintings.size()); } public Optional getIntByIdentifier(String motive) { - // Handle older versions - if (!motive.startsWith("minecraft:")) - motive = "minecraft:" + motive.toLowerCase(Locale.ROOT); - return Optional.ofNullable(paintings.get(motive)); + return Optional.ofNullable(paintings.get(Key.namespaced(motive.toLowerCase(Locale.ROOT)))); } -} \ No newline at end of file +} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/blockentities/FlowerPotHandler.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/blockentities/FlowerPotHandler.java index 3c29aaff4..20cd6fda4 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/blockentities/FlowerPotHandler.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/providers/blockentities/FlowerPotHandler.java @@ -20,7 +20,9 @@ package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.block import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider; +import com.viaversion.viaversion.util.Key; import com.viaversion.viaversion.util.Pair; + import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -66,7 +68,7 @@ public class FlowerPotHandler implements BlockEntityProvider.BlockEntityHandler // Convert item to String without namespace or to Byte if (item instanceof String) { - item = ((String) item).replace("minecraft:", ""); + item = Key.stripMinecraftNamespace((String) item); } else if (item instanceof Number) { item = ((Number) item).byteValue(); } else { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java index 7e5a6aa98..1895f5eb2 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java @@ -17,13 +17,7 @@ */ package com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets; -import com.github.steveice10.opennbt.tag.builtin.ByteTag; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.opennbt.tag.builtin.FloatTag; -import com.github.steveice10.opennbt.tag.builtin.IntTag; -import com.github.steveice10.opennbt.tag.builtin.ListTag; -import com.github.steveice10.opennbt.tag.builtin.LongTag; -import com.github.steveice10.opennbt.tag.builtin.StringTag; +import com.github.steveice10.opennbt.tag.builtin.*; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.minecraft.WorldIdentifiers; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_16; @@ -38,6 +32,8 @@ import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_ import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.metadata.MetadataRewriter1_16To1_15_2; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16; +import com.viaversion.viaversion.util.Key; + import java.util.Arrays; import java.util.UUID; @@ -242,8 +238,8 @@ public class EntityPackets { String key = wrapper.read(Type.STRING); String attributeIdentifier = protocol.getMappingData().getAttributeMappings().get(key); if (attributeIdentifier == null) { - attributeIdentifier = "minecraft:" + key; - if (!com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.MappingData.isValid1_13Channel(attributeIdentifier)) { + attributeIdentifier = Key.namespaced(key); + if (!Key.isValid(attributeIdentifier)) { if (!Via.getConfig().isSuppressConversionWarnings()) { Via.getPlatform().getLogger().warning("Invalid attribute: " + key); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/ItemRewriter.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/ItemRewriter.java index b16711085..562c52fc0 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/ItemRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/ItemRewriter.java @@ -22,8 +22,10 @@ import com.github.steveice10.opennbt.tag.builtin.ListTag; import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.Tag; import com.viaversion.viaversion.api.minecraft.item.Item; +import com.viaversion.viaversion.util.Key; import it.unimi.dsi.fastutil.ints.Int2IntMap; import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; + import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -177,7 +179,7 @@ public class ItemRewriter { int data = 0; if (tag != null && tag.get("Potion") instanceof StringTag) { StringTag potion = tag.get("Potion"); - String potionName = potion.getValue().replace("minecraft:", ""); + String potionName = Key.stripMinecraftNamespace(potion.getValue()); if (POTION_NAME_TO_ID.containsKey(potionName)) { data = POTION_NAME_TO_ID.get(potionName); } @@ -193,7 +195,7 @@ public class ItemRewriter { item.setIdentifier(373); // Potion if (tag != null && tag.get("Potion") instanceof StringTag) { StringTag potion = tag.get("Potion"); - String potionName = potion.getValue().replace("minecraft:", ""); + String potionName = Key.stripMinecraftNamespace(potion.getValue()); if (POTION_NAME_TO_ID.containsKey(potionName)) { data = POTION_NAME_TO_ID.get(potionName) + 8192; } @@ -275,7 +277,7 @@ public class ItemRewriter { item.setData((short) (item.data() - 8192)); } String name = potionNameFromDamage(item.data()); - StringTag potion = new StringTag("minecraft:" + name); + StringTag potion = new StringTag(Key.namespaced(name)); tag.put("Potion", potion); item.setTag(tag); item.setData((short) 0);