delegate hover event for items/entities to adventure, fixes #161

This commit is contained in:
Kieran Wallbanks 2021-03-05 16:44:58 +00:00
parent b6dd2fa8f1
commit 426b93db8f
2 changed files with 31 additions and 9 deletions

View File

@ -1,12 +1,20 @@
package net.minestom.server.chat;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityType;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.util.UUID;
/**
* Represents a hover event for a specific portion of the message.
*/
@ -38,12 +46,12 @@ public class ChatHoverEvent {
}
@Nullable
protected String getValue() {
public String getValue() {
return value;
}
@Nullable
protected JsonObject getValueObject() {
public JsonObject getValueObject() {
return valueObject;
}
@ -81,8 +89,17 @@ public class ChatHoverEvent {
*/
@NotNull
public static ChatHoverEvent showItem(@NotNull ItemStack itemStack) {
final String json = itemStack.toNBT().toSNBT();
return new ChatHoverEvent("show_item", json);
HoverEvent<HoverEvent.ShowItem> event = HoverEvent.showItem(itemStack.getMaterial().key(), itemStack.getAmount());
JsonObject obj = GsonComponentSerializer.gson().serializer().toJsonTree(Component.empty().hoverEvent(event)).getAsJsonObject();
obj = obj.get("hoverEvent").getAsJsonObject().get("contents").getAsJsonObject();
if (itemStack.getItemMeta() != null) {
NBTCompound compound = new NBTCompound();
itemStack.getItemMeta().write(compound);
obj.add("tag", new JsonPrimitive(compound.toSNBT()));
}
return new ChatHoverEvent("show_item", obj);
}
/**
@ -93,9 +110,14 @@ public class ChatHoverEvent {
*/
@NotNull
public static ChatHoverEvent showEntity(@NotNull Entity entity) {
NBTCompound compound = new NBTCompound()
.setString("id", entity.getUuid().toString())
.setString("type", entity.getEntityType().getNamespaceID());
return new ChatHoverEvent("show_entity", compound.toSNBT());
HoverEvent<HoverEvent.ShowEntity> event = HoverEvent.showEntity(entity.getEntityType().key(), entity.getUuid());
JsonObject obj = GsonComponentSerializer.gson().serializer().toJsonTree(Component.empty().hoverEvent(event)).getAsJsonObject();
return new ChatHoverEvent("show_entity", obj.get("hoverEvent").getAsJsonObject().get("contents").getAsJsonObject());
}
public static ChatHoverEvent showEntity(UUID uuid, EntityType entityType) {
HoverEvent<HoverEvent.ShowEntity> event = HoverEvent.showEntity(entityType.key(), uuid);
JsonObject obj = GsonComponentSerializer.gson().serializer().toJsonTree(Component.empty().hoverEvent(event)).getAsJsonObject();
return new ChatHoverEvent("show_entity", obj.get("hoverEvent").getAsJsonObject().get("contents").getAsJsonObject());
}
}

View File

@ -160,7 +160,7 @@ public class RichMessage extends JsonMessage {
// The value is a JsonObject
hoverObject = new JsonObject();
hoverObject.addProperty("action", hoverEvent.getAction());
hoverObject.add("value", hoverEvent.getValueObject());
hoverObject.add("contents", hoverEvent.getValueObject());
} else {
// The value is a raw string
final String hoverValue = hoverEvent.getValue();