diff --git a/src/main/java/net/minestom/server/chat/ChatHoverEvent.java b/src/main/java/net/minestom/server/chat/ChatHoverEvent.java index a236ff9b4..f10722f71 100644 --- a/src/main/java/net/minestom/server/chat/ChatHoverEvent.java +++ b/src/main/java/net/minestom/server/chat/ChatHoverEvent.java @@ -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 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 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 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()); } } diff --git a/src/main/java/net/minestom/server/chat/RichMessage.java b/src/main/java/net/minestom/server/chat/RichMessage.java index 986e1727c..52a70d0a0 100644 --- a/src/main/java/net/minestom/server/chat/RichMessage.java +++ b/src/main/java/net/minestom/server/chat/RichMessage.java @@ -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();