Skip to content

Commit 97b411a

Browse files
committed
Manually handle text component boolean entries
Fixes ViaVersion#4558
1 parent 24f6c4f commit 97b411a

File tree

5 files changed

+69
-6
lines changed

5 files changed

+69
-6
lines changed

api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataKey.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ public record StructuredDataKey<T>(String identifier, Type<T> type) {
9595
public static final StructuredDataKey<Integer> DAMAGE = new StructuredDataKey<>("damage", Types.VAR_INT);
9696
public static final StructuredDataKey<Unbreakable> UNBREAKABLE1_20_5 = new StructuredDataKey<>("unbreakable", Unbreakable.TYPE);
9797
public static final StructuredDataKey<Unit> UNBREAKABLE1_21_5 = new StructuredDataKey<>("unbreakable", Types.EMPTY);
98-
public static final StructuredDataKey<Tag> CUSTOM_NAME = new StructuredDataKey<>("custom_name", Types.TAG);
99-
public static final StructuredDataKey<Tag> ITEM_NAME = new StructuredDataKey<>("item_name", Types.TAG);
98+
public static final StructuredDataKey<Tag> CUSTOM_NAME = new StructuredDataKey<>("custom_name", Types.TEXT_COMPONENT_TAG);
99+
public static final StructuredDataKey<Tag> ITEM_NAME = new StructuredDataKey<>("item_name", Types.TEXT_COMPONENT_TAG);
100100
public static final StructuredDataKey<Key> ITEM_MODEL = new StructuredDataKey<>("item_model", Types.RESOURCE_LOCATION);
101-
public static final StructuredDataKey<Tag[]> LORE = new StructuredDataKey<>("lore", new ArrayType<>(Types.TAG, 256));
101+
public static final StructuredDataKey<Tag[]> LORE = new StructuredDataKey<>("lore", new ArrayType<>(Types.TEXT_COMPONENT_TAG, 256));
102102
public static final StructuredDataKey<Integer> RARITY = new StructuredDataKey<>("rarity", EnumTypes.RARITY);
103103
public static final StructuredDataKey<Enchantments> ENCHANTMENTS1_20_5 = new StructuredDataKey<>("enchantments", Enchantments.TYPE1_20_5);
104104
public static final StructuredDataKey<Enchantments> ENCHANTMENTS1_21_5 = new StructuredDataKey<>("enchantments", Enchantments.TYPE1_21_5);

api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/AttributeModifiers1_21.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void write(final Ops ops, final AttributeModifier value) {
173173
map.writeMap("display", display -> {
174174
display.write("type", Types.STRING, Display.DISPLAY_TYPES[value.display.id()]);
175175
if (value.display instanceof final OverrideText overrideText) {
176-
display.write("value", Types.TAG, overrideText.component);
176+
display.write("value", Types.TEXT_COMPONENT_TAG, overrideText.component);
177177
}
178178
});
179179
});

api/src/main/java/com/viaversion/viaversion/api/type/Types.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
import com.viaversion.viaversion.api.type.types.misc.SoundEventType;
117117
import com.viaversion.viaversion.api.type.types.misc.TagKeyType;
118118
import com.viaversion.viaversion.api.type.types.misc.TagType;
119+
import com.viaversion.viaversion.api.type.types.misc.TextComponentTagType;
119120
import com.viaversion.viaversion.api.type.types.misc.VillagerDataType;
120121
import com.viaversion.viaversion.api.type.types.version.Types1_20_5;
121122
import com.viaversion.viaversion.util.Key;
@@ -200,6 +201,7 @@ public final class Types {
200201
public static final Type<Tag> TAG = new TagType();
201202
public static final Type<Tag[]> TAG_ARRAY = new ArrayType<>(TAG);
202203
public static final Type<Tag> OPTIONAL_TAG = new TagType.OptionalTagType();
204+
public static final Type<Tag> TEXT_COMPONENT_TAG = new TextComponentTagType(); // only strictly needed for hashing
203205
public static final Type<Tag> CUSTOM_CLICK_ACTION_TAG = new LengthPrefixedTagType(65536);
204206

205207
public static final Type<GlobalBlockPosition> GLOBAL_POSITION = new GlobalBlockPositionType();

api/src/main/java/com/viaversion/viaversion/api/type/types/misc/TagType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ public void write(final Ops ops, final Tag value) {
105105
} else if (value instanceof CompoundTag compoundTag) {
106106
ops.writeMap(map -> {
107107
for (final Map.Entry<String, Tag> entry : compoundTag.entrySet()) {
108-
map.write(entry.getKey(), Types.TAG, entry.getValue());
108+
map.write(entry.getKey(), this, entry.getValue());
109109
}
110110
});
111111
} else if (value instanceof ListTag<?> listTag) {
112112
ops.writeList(list -> {
113113
for (final Tag tag : listTag) {
114-
list.write(Types.TAG, tag);
114+
list.write(this, tag);
115115
}
116116
});
117117
} else if (value instanceof IntArrayTag intArrayTag) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
3+
* Copyright (C) 2016-2025 ViaVersion and contributors
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
package com.viaversion.viaversion.api.type.types.misc;
24+
25+
import com.viaversion.nbt.tag.ByteTag;
26+
import com.viaversion.nbt.tag.CompoundTag;
27+
import com.viaversion.nbt.tag.Tag;
28+
import com.viaversion.viaversion.api.minecraft.codec.Ops;
29+
import com.viaversion.viaversion.api.type.Types;
30+
import java.util.Map;
31+
import java.util.Set;
32+
33+
/**
34+
* Only strictly needed for hashing purposes for now.
35+
*/
36+
public class TextComponentTagType extends TagType {
37+
38+
private static final Set<String> BOOLEAN_KEYS = Set.of("bold", "italic", "underlined", "strikethrough", "obfuscated", "interpret");
39+
40+
@Override
41+
public void write(final Ops ops, final Tag value) {
42+
if (value instanceof final CompoundTag compoundTag) {
43+
ops.writeMap(map -> {
44+
for (final Map.Entry<String, Tag> entry : compoundTag.entrySet()) {
45+
write(map, entry.getKey(), entry.getValue());
46+
}
47+
});
48+
} else {
49+
super.write(ops, value);
50+
}
51+
}
52+
53+
private void write(final Ops.MapSerializer map, final String key, final Tag value) {
54+
// Better than fully parsing and re-serializing the component
55+
if (value instanceof final ByteTag byteTag && BOOLEAN_KEYS.contains(key)) {
56+
map.write(key, Types.BOOLEAN, byteTag.asBoolean());
57+
} else {
58+
map.write(key, this, value);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)