Catch specific asXTag calls

This commit is contained in:
Nassim Jahnke 2023-12-30 18:48:13 +01:00
parent 928b14c591
commit 5d21492858
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
2 changed files with 65 additions and 8 deletions

View File

@ -2,8 +2,9 @@ import os
import re
import subprocess
via_nbt_version = '3.4.0'
via_nbt_version = '3.5.0'
# All of this would work better with bytecode rewriting, but here we go
replacements = {
# Gradle build script changes (less chance of running into conflicts by putting it here instead of the patch)
'name = "lenni0451"': 'name = "viaversion"',
@ -88,6 +89,12 @@ def deep(path):
handle_file(p)
def to_camel_case(s):
words = s.split('_')
camel_case_words = [words[0].capitalize()] + [word.capitalize() for word in words[1:]]
return ''.join(camel_case_words)
def replace_get_value(content, obj):
# First apply nullable replacements with the generic replacement, THEN the rest
content = replace_nullable_get(obj, 'Compound', '', 'null', content)
@ -104,10 +111,15 @@ def replace_get_value(content, obj):
content = replace_nonnull_get(obj, 'LongArray', '.getValue()', 'new long[0]', content)
# Booleans are stored as byte tags
content = re.sub(r'tag\.getBoolean\(([^)]+)\)',
content = re.sub(fr'{obj}\.getBoolean\(([^)]+)\)',
fr'({obj}.get(\1) instanceof ByteTag ? ((ByteTag) {obj}.get(\1)).asBoolean() : false)',
content)
# something.get(y).asXTag() -> ((XTag) something.get(y))
content = re.sub(fr'{obj}\.get\(([^)]+)\).as(\w+Tag)\(\)',
fr'((\2) {obj}.get(\1))',
content)
numeric_types = {'Byte', 'Short', 'Int', 'Long', 'Float', 'Double'}
for numeric_type in numeric_types:
content = replace_nonnull_get(obj, numeric_type, f'.as{numeric_type}()', '0', content)
@ -153,15 +165,15 @@ def handle_file(path):
# tag.contains(s, Tag.X) -> tag.get(s) instanceof XTag
changed_content = re.sub(r'(\w+)\.contains\(([^,)]+), Tag\.(\w+)\)',
lambda m: f'({m.group(1)}.get({m.group(2)}) instanceof {m.group(3).capitalize()}Tag)',
lambda m: f'({m.group(1)}.get({m.group(2)}) instanceof {to_camel_case(m.group(3))}Tag)',
changed_content)
changed_content = re.sub(r'\.contains\(([^,)]+), Tag\.(\w+)\)',
lambda m: f'.get({m.group(1)}) instanceof {m.group(2).capitalize()}Tag',
lambda m: f'.get({m.group(1)}) instanceof {to_camel_case(m.group(2))}Tag',
changed_content)
# Tag.X.equals(tagType) -> tagType instance XTag
changed_content = re.sub(r'Tag\.(\w+).equals\((\w+)\)',
lambda m: f'({m.group(2)} instanceof {m.group(1).capitalize()}Tag)',
lambda m: f'({m.group(2)} instanceof {to_camel_case(m.group(1))}Tag)',
changed_content)
# tag.getX -> cast to tag with default value

View File

@ -36,10 +36,10 @@ index 8bec86c..3525056 100644
protected Tag readValue(final StringReader_v1_12 reader) throws SNbtDeserializeException {
diff --git a/MCStructs-text/src/test/java/net/lenni0451/mcstructs/text/serializer/TextComponentCodecTest.java b/MCStructs-text/src/test/java/net/lenni0451/mcstructs/text/serializer/TextComponentCodecTest.java
index 86e32ec..9eb184c 100644
index 4fdfde0..dd48c1a 100644
--- a/MCStructs-text/src/test/java/net/lenni0451/mcstructs/text/serializer/TextComponentCodecTest.java
+++ b/MCStructs-text/src/test/java/net/lenni0451/mcstructs/text/serializer/TextComponentCodecTest.java
@@ -60,9 +60,9 @@ class TextComponentCodecTest {
@@ -61,9 +61,9 @@ class TextComponentCodecTest {
@Test
void legacyItemDeserialization() throws SNbtSerializeException {
@ -52,7 +52,7 @@ index 86e32ec..9eb184c 100644
ATextComponent legacyComponent = new StringComponent("test")
.setStyle(new Style()
.setHoverEvent(new TextHoverEvent(HoverEventAction.SHOW_ITEM, new StringComponent(SNbtSerializer.LATEST.serialize(legacyNbt))))
@@ -79,10 +79,10 @@ class TextComponentCodecTest {
@@ -80,10 +80,10 @@ class TextComponentCodecTest {
@Test
void legacyEntityDeserialization() throws SNbtSerializeException {
UUID randomUUID = UUID.randomUUID();
@ -67,6 +67,51 @@ index 86e32ec..9eb184c 100644
ATextComponent legacyComponent = new StringComponent("test")
.setStyle(new Style()
.setHoverEvent(new TextHoverEvent(HoverEventAction.SHOW_ENTITY, new StringComponent(SNbtSerializer.LATEST.serialize(legacyNbt))))
@@ -100,19 +100,31 @@ class TextComponentCodecTest {
@Test
void arrayWithTag() {
- ListTag tags = new ListTag()
- .add(new CompoundTag()
- .putString("translate", "test")
- .addByteArray("with", (byte) 1, (byte) 2, (byte) 3))
- .add(new CompoundTag()
- .putString("translate", "test")
- .addIntArray("with", 1, 2, 3))
- .add(new CompoundTag()
- .putString("translate", "test")
- .addLongArray("with", 1, 2, 3))
- .add(new CompoundTag()
- .putString("translate", "test")
- .addList("with", 1, 2, 3));
+ CompoundTag translateWithByteArray = new CompoundTag();
+ translateWithByteArray.putString("translate", "test");
+ translateWithByteArray.put("with", new ByteArrayTag(new byte[]{1, 2, 3}));
+
+ CompoundTag translateWithIntArray = new CompoundTag();
+ translateWithIntArray.putString("translate", "test");
+ translateWithIntArray.put("with", new IntArrayTag(new int[]{1, 2, 3}));
+
+ CompoundTag translateWithLongArray = new CompoundTag();
+ translateWithLongArray.putString("translate", "test");
+ translateWithLongArray.put("with", new LongArrayTag(new long[]{1, 2, 3}));
+
+ CompoundTag translateWithList = new CompoundTag();
+ ListTag numberList = new ListTag();
+ numberList.add(new IntTag(1));
+ numberList.add(new IntTag(2));
+ numberList.add(new IntTag(3));
+ translateWithList.putString("translate", "test");
+ translateWithList.put("with", numberList);
+
+ ListTag tags = new ListTag();
+ tags.add(translateWithByteArray);
+ tags.add(translateWithIntArray);
+ tags.add(translateWithLongArray);
+ tags.add(translateWithList);
ATextComponent component = new TranslationComponent("test", (byte) 1, (byte) 2, (byte) 3)
.append(new TranslationComponent("test", 1, 2, 3))
.append(new TranslationComponent("test", 1L, 2L, 3L))
diff --git a/build.gradle b/build.gradle
index a8c4682..6929dc4 100644
--- a/build.gradle