Skip to content

Commit bdc862b

Browse files
authored
Merge pull request #1145 from KyoriPowered/fix/1131
fix(text-serializer-gson): correctly handle removed data components
2 parents 2fe0cff + 3ce75bf commit bdc862b

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/ShowItemSerializer.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import com.google.gson.Gson;
2727
import com.google.gson.JsonElement;
28+
import com.google.gson.JsonNull;
2829
import com.google.gson.JsonParseException;
2930
import com.google.gson.TypeAdapter;
3031
import com.google.gson.stream.JsonReader;
@@ -49,6 +50,7 @@
4950
final class ShowItemSerializer extends TypeAdapter<HoverEvent.ShowItem> {
5051
@SuppressWarnings("deprecation")
5152
private static final String LEGACY_SHOW_ITEM_TAG = net.kyori.adventure.text.serializer.json.JSONComponentConstants.SHOW_ITEM_TAG;
53+
private static final String DATA_COMPONENT_REMOVAL_PREFIX = "!";
5254

5355
private final Gson gson;
5456
private final boolean emitDefaultQuantity;
@@ -94,12 +96,22 @@ public HoverEvent.ShowItem read(final JsonReader in) throws IOException {
9496
} else if (fieldName.equals(SHOW_ITEM_COMPONENTS)) {
9597
in.beginObject();
9698
while (in.peek() != JsonToken.END_OBJECT) {
97-
final Key id = Key.key(in.nextName());
99+
final String name = in.nextName();
100+
final Key id;
101+
final boolean removed;
102+
if (name.startsWith(DATA_COMPONENT_REMOVAL_PREFIX)) {
103+
id = Key.key(name.substring(1));
104+
removed = true;
105+
} else {
106+
id = Key.key(name);
107+
removed = false;
108+
}
109+
98110
final JsonElement tree = this.gson.fromJson(in, JsonElement.class);
99111
if (dataComponents == null) {
100112
dataComponents = new HashMap<>();
101113
}
102-
dataComponents.put(id, GsonDataComponentValue.gsonDataComponentValue(tree));
114+
dataComponents.put(id, removed ? DataComponentValue.removed() : GsonDataComponentValue.gsonDataComponentValue(tree));
103115
}
104116
in.endObject();
105117
} else {
@@ -137,8 +149,14 @@ public void write(final JsonWriter out, final HoverEvent.ShowItem value) throws
137149
out.name(SHOW_ITEM_COMPONENTS);
138150
out.beginObject();
139151
for (final Map.Entry<Key, GsonDataComponentValue> entry : value.dataComponentsAs(GsonDataComponentValue.class).entrySet()) {
140-
out.name(entry.getKey().asString());
141-
this.gson.toJson(entry.getValue().element(), out);
152+
final JsonElement el = entry.getValue().element();;
153+
if (el instanceof JsonNull) { // removed
154+
out.name(DATA_COMPONENT_REMOVAL_PREFIX + entry.getKey().asString());
155+
out.beginObject().endObject();
156+
} else {
157+
out.name(entry.getKey().asString());
158+
this.gson.toJson(el, out);
159+
}
142160
}
143161
out.endObject();
144162
} else if (this.itemDataMode != JSONOptions.ShowItemHoverDataMode.EMIT_DATA_COMPONENTS) {

text-serializer-json/src/testFixtures/java/net/kyori/adventure/text/serializer/json/ShowItemSerializerTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package net.kyori.adventure.text.serializer.json;
2525

26+
import com.google.gson.JsonObject;
2627
import java.io.IOException;
2728
import java.util.Collections;
2829
import net.kyori.adventure.key.Key;
@@ -31,6 +32,7 @@
3132
import net.kyori.adventure.nbt.TagStringIO;
3233
import net.kyori.adventure.nbt.api.BinaryTagHolder;
3334
import net.kyori.adventure.text.Component;
35+
import net.kyori.adventure.text.event.DataComponentValue;
3436
import net.kyori.adventure.text.event.HoverEvent;
3537
import org.junit.jupiter.api.Test;
3638

@@ -124,4 +126,30 @@ void testDeserializeWithCountOfOne() throws IOException {
124126
}
125127
);
126128
}
129+
130+
@Test
131+
void testDeserializeWithRemovedComponent() {
132+
this.testObject(
133+
Component.text().hoverEvent(
134+
HoverEvent.showItem(
135+
Key.key("minecraft", "diamond"),
136+
2,
137+
Collections.singletonMap(Key.key("minecraft", "damage"), DataComponentValue.removed())
138+
)
139+
).build(),
140+
json -> {
141+
json.addProperty(JSONComponentConstants.TEXT, "");
142+
json.add(JSONComponentConstants.HOVER_EVENT, object(hover -> {
143+
hover.addProperty(JSONComponentConstants.HOVER_EVENT_ACTION, name(HoverEvent.Action.SHOW_ITEM));
144+
hover.add(JSONComponentConstants.HOVER_EVENT_CONTENTS, object(contents -> {
145+
contents.addProperty(JSONComponentConstants.SHOW_ITEM_ID, "minecraft:diamond");
146+
contents.addProperty(JSONComponentConstants.SHOW_ITEM_COUNT, 2);
147+
contents.add(JSONComponentConstants.SHOW_ITEM_COMPONENTS, object(comps -> {
148+
comps.add("!minecraft:damage", new JsonObject());
149+
}));
150+
}));
151+
}));
152+
}
153+
);
154+
}
127155
}

0 commit comments

Comments
 (0)