Skip to content

Commit

Permalink
fix(text-serializer-gson): Include show item quantity always
Browse files Browse the repository at this point in the history
this matches new Vanilla behavior.
  • Loading branch information
zml2008 committed Mar 18, 2024
1 parent 72ad416 commit 1677bbc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
} else if (HOVER_ACTION_TYPE.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) HoverEventActionSerializer.INSTANCE;
} else if (SHOW_ITEM_TYPE.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) ShowItemSerializer.create(gson);
return (TypeAdapter<T>) ShowItemSerializer.create(gson, this.features);
} else if (SHOW_ENTITY_TYPE.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) ShowEntitySerializer.create(gson);
} else if (COLOR_WRAPPER_TYPE.isAssignableFrom(rawType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,25 @@
import net.kyori.adventure.key.Key;
import net.kyori.adventure.nbt.api.BinaryTagHolder;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.serializer.json.JSONOptions;
import net.kyori.option.OptionState;
import org.jetbrains.annotations.Nullable;

import static net.kyori.adventure.text.serializer.json.JSONComponentConstants.SHOW_ITEM_COUNT;
import static net.kyori.adventure.text.serializer.json.JSONComponentConstants.SHOW_ITEM_ID;
import static net.kyori.adventure.text.serializer.json.JSONComponentConstants.SHOW_ITEM_TAG;

final class ShowItemSerializer extends TypeAdapter<HoverEvent.ShowItem> {
static TypeAdapter<HoverEvent.ShowItem> create(final Gson gson) {
return new ShowItemSerializer(gson).nullSafe();
static TypeAdapter<HoverEvent.ShowItem> create(final Gson gson, final OptionState opt) {
return new ShowItemSerializer(gson, opt.value(JSONOptions.EMIT_DEFAULT_ITEM_HOVER_QUANTITY)).nullSafe();
}

private final Gson gson;
private final boolean emitDefaultQuantity;

private ShowItemSerializer(final Gson gson) {
private ShowItemSerializer(final Gson gson, final boolean emitDefaultQuantity) {
this.gson = gson;
this.emitDefaultQuantity = emitDefaultQuantity;
}

@Override
Expand Down Expand Up @@ -96,7 +100,7 @@ public void write(final JsonWriter out, final HoverEvent.ShowItem value) throws
this.gson.toJson(value.item(), SerializerFactory.KEY_TYPE, out);

final int count = value.count();
if (count != 1) {
if (count != 1 || this.emitDefaultQuantity) {
out.name(SHOW_ITEM_COUNT);
out.value(count);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ private JSONOptions() {
private static final int VERSION_INITIAL = 0;
private static final int VERSION_1_16 = 2526; // 20w16a
private static final int VERSION_1_20_3 = 3679; // 23w40a
private static final int VERSION_1_20_5 = 3819; // 24w09a

/**
* Whether to emit RGB text.
Expand Down Expand Up @@ -82,6 +83,14 @@ private JSONOptions() {
* @since 4.15.0
*/
public static final Option<Boolean> VALIDATE_STRICT_EVENTS = Option.booleanOption(key("validate/strict_events"), true);
/**
* Whether to emit the default hover event item stack quantity of {@code 1}.
*
* <p>When enabled, this matches Vanilla as of 1.20.5.</p>
*
* @since 4.17.0
*/
public static final Option<Boolean> EMIT_DEFAULT_ITEM_HOVER_QUANTITY = Option.booleanOption(key("emit/default_item_hover_quantity"), true);

/**
* Versioned by world data version.
Expand All @@ -93,6 +102,7 @@ private JSONOptions() {
.value(EMIT_RGB, false)
.value(EMIT_HOVER_SHOW_ENTITY_ID_AS_INT_ARRAY, false)
.value(VALIDATE_STRICT_EVENTS, false)
.value(EMIT_DEFAULT_ITEM_HOVER_QUANTITY, false)
)
.version(
VERSION_1_16,
Expand All @@ -105,6 +115,10 @@ private JSONOptions() {
.value(EMIT_HOVER_SHOW_ENTITY_ID_AS_INT_ARRAY, true)
.value(VALIDATE_STRICT_EVENTS, true)
)
.version(
VERSION_1_20_5,
b -> b.value(EMIT_DEFAULT_ITEM_HOVER_QUANTITY, true)
)
.build();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ void testDeserializeWithCountOfOne() throws IOException {
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, 1);
contents.addProperty(JSONComponentConstants.SHOW_ITEM_TAG, "{display:{Name:\"A test!\"}}");
}));
}));
Expand Down

0 comments on commit 1677bbc

Please sign in to comment.