Skip to content

Commit

Permalink
Merge pull request #890 from LOOHP/legacy-show-achievement
Browse files Browse the repository at this point in the history
Added legacy show achievements
  • Loading branch information
zml2008 authored Jun 7, 2023
2 parents 8ac26ed + 8d6213a commit 8dcdbe2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
28 changes: 27 additions & 1 deletion api/src/main/java/net/kyori/adventure/text/event/HoverEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,19 @@ public final class HoverEvent<V> implements Examinable, HoverEventSource<V>, Sty
return new HoverEvent<>(Action.SHOW_ENTITY, entity);
}

/**
* Creates a hover event that shows an achievement on hover.
*
* @param value the achievement value
* @return a hover event
* @since 4.14.0
* @deprecated Removed in Vanilla 1.12, but we keep it for backwards compat
*/
@Deprecated
public static @NotNull HoverEvent<String> showAchievement(final @NotNull String value) {
return new HoverEvent<>(Action.SHOW_ACHIEVEMENT, value);
}

/**
* Creates a hover event.
*
Expand Down Expand Up @@ -824,13 +837,26 @@ public static final class Action<V> {
return value.name(renderer.render(value.name, context));
}
});
/**
* Shows a {@link Component} when hovered over.
*
* @since 4.14.0
* @deprecated Removed in Vanilla 1.12, but we keep it for backwards compat
*/
@Deprecated
public static final Action<String> SHOW_ACHIEVEMENT = new Action<>("show_achievement", String.class, true, new Renderer<String>() {
@Override
public <C> @NotNull String render(final @NotNull ComponentRenderer<C> renderer, final @NotNull C context, final @NotNull String value) {
return value;
}
});

/**
* The name map.
*
* @since 4.0.0
*/
public static final Index<String, Action<?>> NAMES = Index.create(constant -> constant.name, SHOW_TEXT, SHOW_ITEM, SHOW_ENTITY);
public static final Index<String, Action<?>> NAMES = Index.create(constant -> constant.name, SHOW_TEXT, SHOW_ITEM, SHOW_ENTITY, SHOW_ACHIEVEMENT);
private final String name;
private final Class<V> type;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ void testShowEntityName() {
assertEquals(n1, se1.name());
}

@Test
void testShowAchievementValue() {
final HoverEvent<String> sa0 = HoverEvent.showAchievement("achievement.bakeCake");
assertEquals("achievement.bakeCake", sa0.value());
final HoverEvent<String> sa1 = sa0.value("achievement.blazeRod");
assertEquals("achievement.bakeCake", sa0.value()); // original should be unmodified
assertEquals("achievement.blazeRod", sa1.value());
}

@Test
void testEquality() {
final UUID entity = UUID.randomUUID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ final class SerializerFactory implements TypeAdapterFactory {
static final Class<HoverEvent.Action> HOVER_ACTION_TYPE = HoverEvent.Action.class;
static final Class<HoverEvent.ShowItem> SHOW_ITEM_TYPE = HoverEvent.ShowItem.class;
static final Class<HoverEvent.ShowEntity> SHOW_ENTITY_TYPE = HoverEvent.ShowEntity.class;
static final Class<String> STRING_TYPE = String.class;
static final Class<TextColorWrapper> COLOR_WRAPPER_TYPE = TextColorWrapper.class;
static final Class<TextColor> COLOR_TYPE = TextColor.class;
static final Class<TextDecoration> TEXT_DECORATION_TYPE = TextDecoration.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ public Style read(final JsonReader in) throws IOException {
final HoverEvent.Action<Object> action = this.gson.fromJson(serializedAction, SerializerFactory.HOVER_ACTION_TYPE);
if (action.readable()) {
final @Nullable Object value;
final Class<?> actionType = action.type();
if (hoverEventObject.has(HOVER_EVENT_CONTENTS)) {
final @Nullable JsonElement rawValue = hoverEventObject.get(HOVER_EVENT_CONTENTS);
final Class<?> actionType = action.type();
if (isNullOrEmpty(rawValue)) {
value = null;
} else if (SerializerFactory.COMPONENT_TYPE.isAssignableFrom(actionType)) {
Expand All @@ -162,9 +162,13 @@ public Style read(final JsonReader in) throws IOException {
final JsonElement element = hoverEventObject.get(HOVER_EVENT_VALUE);
if (isNullOrEmpty(element)) {
value = null;
} else {
} else if (SerializerFactory.COMPONENT_TYPE.isAssignableFrom(actionType)) {
final Component rawValue = this.gson.fromJson(element, SerializerFactory.COMPONENT_TYPE);
value = this.legacyHoverEventContents(action, rawValue);
} else if (SerializerFactory.STRING_TYPE.isAssignableFrom(actionType)) {
value = this.gson.fromJson(element, SerializerFactory.STRING_TYPE);
} else {
value = null;
}
} else {
value = null;
Expand Down Expand Up @@ -264,21 +268,23 @@ public void write(final JsonWriter out, final Style value) throws IOException {
}

final @Nullable HoverEvent<?> hoverEvent = value.hoverEvent();
if (hoverEvent != null) {
if (hoverEvent != null && (hoverEvent.action() != HoverEvent.Action.SHOW_ACHIEVEMENT || this.emitLegacyHover)) {
out.name(HOVER_EVENT);
out.beginObject();
out.name(HOVER_EVENT_ACTION);
final HoverEvent.Action<?> action = hoverEvent.action();
this.gson.toJson(action, SerializerFactory.HOVER_ACTION_TYPE, out);
out.name(HOVER_EVENT_CONTENTS);
if (action == HoverEvent.Action.SHOW_ITEM) {
this.gson.toJson(hoverEvent.value(), SerializerFactory.SHOW_ITEM_TYPE, out);
} else if (action == HoverEvent.Action.SHOW_ENTITY) {
this.gson.toJson(hoverEvent.value(), SerializerFactory.SHOW_ENTITY_TYPE, out);
} else if (action == HoverEvent.Action.SHOW_TEXT) {
this.gson.toJson(hoverEvent.value(), SerializerFactory.COMPONENT_TYPE, out);
} else {
throw new JsonParseException("Don't know how to serialize " + hoverEvent.value());
if (action != HoverEvent.Action.SHOW_ACHIEVEMENT) { // legacy action has no modern contents value
out.name(HOVER_EVENT_CONTENTS);
if (action == HoverEvent.Action.SHOW_ITEM) {
this.gson.toJson(hoverEvent.value(), SerializerFactory.SHOW_ITEM_TYPE, out);
} else if (action == HoverEvent.Action.SHOW_ENTITY) {
this.gson.toJson(hoverEvent.value(), SerializerFactory.SHOW_ENTITY_TYPE, out);
} else if (action == HoverEvent.Action.SHOW_TEXT) {
this.gson.toJson(hoverEvent.value(), SerializerFactory.COMPONENT_TYPE, out);
} else {
throw new JsonParseException("Don't know how to serialize " + hoverEvent.value());
}
}
if (this.emitLegacyHover) {
out.name(HOVER_EVENT_VALUE);
Expand All @@ -300,6 +306,8 @@ public void write(final JsonWriter out, final Style value) throws IOException {
private void serializeLegacyHoverEvent(final HoverEvent<?> hoverEvent, final JsonWriter out) throws IOException {
if (hoverEvent.action() == HoverEvent.Action.SHOW_TEXT) { // serialization is the same
this.gson.toJson(hoverEvent.value(), SerializerFactory.COMPONENT_TYPE, out);
} else if (hoverEvent.action() == HoverEvent.Action.SHOW_ACHIEVEMENT) {
this.gson.toJson(hoverEvent.value(), String.class, out);
} else if (this.legacyHover != null) { // for data formats that require knowledge of SNBT
Component serialized = null;
try {
Expand Down

0 comments on commit 8dcdbe2

Please sign in to comment.