Skip to content

Commit

Permalink
fix(text-serializer-gson): correctly handle removed data components
Browse files Browse the repository at this point in the history
Fixes GH-1131
  • Loading branch information
zml2008 committed Dec 22, 2024
1 parent 2fe0cff commit 3ce75bf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
Expand All @@ -49,6 +50,7 @@
final class ShowItemSerializer extends TypeAdapter<HoverEvent.ShowItem> {
@SuppressWarnings("deprecation")
private static final String LEGACY_SHOW_ITEM_TAG = net.kyori.adventure.text.serializer.json.JSONComponentConstants.SHOW_ITEM_TAG;
private static final String DATA_COMPONENT_REMOVAL_PREFIX = "!";

private final Gson gson;
private final boolean emitDefaultQuantity;
Expand Down Expand Up @@ -94,12 +96,22 @@ public HoverEvent.ShowItem read(final JsonReader in) throws IOException {
} else if (fieldName.equals(SHOW_ITEM_COMPONENTS)) {
in.beginObject();
while (in.peek() != JsonToken.END_OBJECT) {
final Key id = Key.key(in.nextName());
final String name = in.nextName();
final Key id;
final boolean removed;
if (name.startsWith(DATA_COMPONENT_REMOVAL_PREFIX)) {
id = Key.key(name.substring(1));
removed = true;
} else {
id = Key.key(name);
removed = false;
}

final JsonElement tree = this.gson.fromJson(in, JsonElement.class);
if (dataComponents == null) {
dataComponents = new HashMap<>();
}
dataComponents.put(id, GsonDataComponentValue.gsonDataComponentValue(tree));
dataComponents.put(id, removed ? DataComponentValue.removed() : GsonDataComponentValue.gsonDataComponentValue(tree));
}
in.endObject();
} else {
Expand Down Expand Up @@ -137,8 +149,14 @@ public void write(final JsonWriter out, final HoverEvent.ShowItem value) throws
out.name(SHOW_ITEM_COMPONENTS);
out.beginObject();
for (final Map.Entry<Key, GsonDataComponentValue> entry : value.dataComponentsAs(GsonDataComponentValue.class).entrySet()) {
out.name(entry.getKey().asString());
this.gson.toJson(entry.getValue().element(), out);
final JsonElement el = entry.getValue().element();;
if (el instanceof JsonNull) { // removed
out.name(DATA_COMPONENT_REMOVAL_PREFIX + entry.getKey().asString());
out.beginObject().endObject();
} else {
out.name(entry.getKey().asString());
this.gson.toJson(el, out);
}
}
out.endObject();
} else if (this.itemDataMode != JSONOptions.ShowItemHoverDataMode.EMIT_DATA_COMPONENTS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package net.kyori.adventure.text.serializer.json;

import com.google.gson.JsonObject;
import java.io.IOException;
import java.util.Collections;
import net.kyori.adventure.key.Key;
Expand All @@ -31,6 +32,7 @@
import net.kyori.adventure.nbt.TagStringIO;
import net.kyori.adventure.nbt.api.BinaryTagHolder;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.DataComponentValue;
import net.kyori.adventure.text.event.HoverEvent;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -124,4 +126,30 @@ void testDeserializeWithCountOfOne() throws IOException {
}
);
}

@Test
void testDeserializeWithRemovedComponent() {
this.testObject(
Component.text().hoverEvent(
HoverEvent.showItem(
Key.key("minecraft", "diamond"),
2,
Collections.singletonMap(Key.key("minecraft", "damage"), DataComponentValue.removed())
)
).build(),
json -> {
json.addProperty(JSONComponentConstants.TEXT, "");
json.add(JSONComponentConstants.HOVER_EVENT, object(hover -> {
hover.addProperty(JSONComponentConstants.HOVER_EVENT_ACTION, name(HoverEvent.Action.SHOW_ITEM));
hover.add(JSONComponentConstants.HOVER_EVENT_CONTENTS, object(contents -> {
contents.addProperty(JSONComponentConstants.SHOW_ITEM_ID, "minecraft:diamond");
contents.addProperty(JSONComponentConstants.SHOW_ITEM_COUNT, 2);
contents.add(JSONComponentConstants.SHOW_ITEM_COMPONENTS, object(comps -> {
comps.add("!minecraft:damage", new JsonObject());
}));
}));
}));
}
);
}
}

0 comments on commit 3ce75bf

Please sign in to comment.