From 232180ee61d6ccd044aa8327f6ea1fa3434060eb Mon Sep 17 00:00:00 2001 From: RK_01 <50594595+RaphiMC@users.noreply.github.com> Date: Mon, 23 Oct 2023 23:55:13 +0000 Subject: [PATCH] Fix identifier handling in old protocols (#624) --- .../data/ShoulderTracker.java | 5 ++--- .../data/EntityNameRewrites.java | 10 ++++----- .../data/NamedSoundMapping.java | 7 +++---- .../packets/BlockItemPackets1_13.java | 21 +++++-------------- .../packets/BlockItemPackets1_14.java | 11 ++++------ .../Protocol1_14_2To1_14_3.java | 3 ++- 6 files changed, 20 insertions(+), 37 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/data/ShoulderTracker.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/data/ShoulderTracker.java index f13d31f4..b8096675 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/data/ShoulderTracker.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/data/ShoulderTracker.java @@ -26,6 +26,7 @@ import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.ClientboundPackets1_12; import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8; +import com.viaversion.viaversion.util.Key; public class ShoulderTracker extends StoredObject { private int entityId; @@ -74,9 +75,7 @@ public class ShoulderTracker extends StoredObject { } private String getName(String current) { - if (current.startsWith("minecraft:")) { - current = current.substring(10); - } + current = Key.stripMinecraftNamespace(current); String[] array = current.split("_"); StringBuilder builder = new StringBuilder(); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/EntityNameRewrites.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/EntityNameRewrites.java index b06fa1a7..db166a26 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/EntityNameRewrites.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/EntityNameRewrites.java @@ -18,6 +18,8 @@ package com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data; +import com.viaversion.viaversion.util.Key; + import java.util.HashMap; import java.util.Map; @@ -42,15 +44,11 @@ public class EntityNameRewrites { private static void reg(String past, String future) { - ENTITY_NAMES.put("minecraft:" + future, "minecraft:" + past); + ENTITY_NAMES.put(Key.namespaced(future), Key.namespaced(past)); } public static String rewrite(String entName) { - String entityName = ENTITY_NAMES.get(entName); - if (entityName != null) { - return entityName; - } - entityName = ENTITY_NAMES.get("minecraft:" + entName); + String entityName = ENTITY_NAMES.get(Key.namespaced(entName)); if (entityName != null) { return entityName; } else diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/NamedSoundMapping.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/NamedSoundMapping.java index 18acce0b..588b07fe 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/NamedSoundMapping.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/data/NamedSoundMapping.java @@ -18,6 +18,8 @@ package com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.NamedSoundRewriter; +import com.viaversion.viaversion.util.Key; + import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; @@ -37,9 +39,6 @@ public class NamedSoundMapping { } public static String getOldId(String sound1_13) { - if (sound1_13.startsWith("minecraft:")) { - sound1_13 = sound1_13.substring(10); - } - return SOUNDS.get(sound1_13); + return SOUNDS.get(Key.stripMinecraftNamespace(sound1_13)); } } 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 8beff0c6..d0b1c6fe 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 @@ -41,14 +41,7 @@ import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_13; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_9_3; import com.viaversion.viaversion.libs.opennbt.conversion.ConverterRegistry; -import com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag; -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.ListTag; -import com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag; -import com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag; -import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag; -import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.*; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ServerboundPackets1_12_1; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter; @@ -57,12 +50,8 @@ 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.Key; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Optional; + +import java.util.*; public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewriters.ItemRewriter { @@ -592,7 +581,7 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit for (Tag oldTag : blockTag) { Object value = oldTag.getValue(); String[] newValues = value instanceof String ? - BlockIdData.fallbackReverseMapping.get(((String) value).replace("minecraft:", "")) : null; + BlockIdData.fallbackReverseMapping.get(Key.stripMinecraftNamespace((String) value)) : null; if (newValues != null) { for (String newValue : newValues) { newCanPlaceOn.add(new StringTag(newValue)); @@ -843,7 +832,7 @@ public class BlockItemPackets1_13 extends com.viaversion.viabackwards.api.rewrit ListTag newCanPlaceOn = new ListTag(StringTag.class); for (Tag oldTag : blockTag) { Object value = oldTag.getValue(); - String oldId = value.toString().replace("minecraft:", ""); + String oldId = Key.stripMinecraftNamespace(value.toString()); int key = Ints.tryParse(oldId); String numberConverted = BlockIdData.numberIdToString.get(key); if (numberConverted != null) { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java index 3e90cad4..830e49d7 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java @@ -25,12 +25,7 @@ import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLi import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.minecraft.ClientWorld; import com.viaversion.viaversion.api.minecraft.Environment; -import com.viaversion.viaversion.api.minecraft.chunks.Chunk; -import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; -import com.viaversion.viaversion.api.minecraft.chunks.ChunkSectionLight; -import com.viaversion.viaversion.api.minecraft.chunks.ChunkSectionLightImpl; -import com.viaversion.viaversion.api.minecraft.chunks.DataPalette; -import com.viaversion.viaversion.api.minecraft.chunks.PaletteType; +import com.viaversion.viaversion.api.minecraft.chunks.*; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_14; import com.viaversion.viaversion.api.minecraft.item.Item; @@ -54,6 +49,8 @@ import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPacke import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; import com.viaversion.viaversion.rewriter.BlockRewriter; import com.viaversion.viaversion.rewriter.RecipeRewriter; +import com.viaversion.viaversion.util.Key; + import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -261,7 +258,7 @@ public class BlockItemPackets1_14 extends com.viaversion.viabackwards.api.rewrit for (int i = 0; i < size; i++) { String type = wrapper.read(Type.STRING); String id = wrapper.read(Type.STRING); // Recipe Identifier - type = type.replace("minecraft:", ""); + type = Key.stripMinecraftNamespace(type); if (removedTypes.contains(type)) { switch (type) { case "blasting": diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_2to1_14_3/Protocol1_14_2To1_14_3.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_2to1_14_3/Protocol1_14_2To1_14_3.java index a842912c..94dc10c4 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_2to1_14_3/Protocol1_14_2To1_14_3.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_2to1_14_3/Protocol1_14_2To1_14_3.java @@ -22,6 +22,7 @@ import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; import com.viaversion.viaversion.rewriter.RecipeRewriter; +import com.viaversion.viaversion.util.Key; public class Protocol1_14_2To1_14_3 extends BackwardsProtocol { @@ -60,7 +61,7 @@ public class Protocol1_14_2To1_14_3 extends BackwardsProtocol