From 762fca77e3abe3e7966d85638b9eb0d482edb87a Mon Sep 17 00:00:00 2001 From: rtm516 Date: Fri, 26 Jun 2020 23:20:09 +0100 Subject: [PATCH] Fix HoverEvent messages --- .../data/message/ItemHoverMessage.java | 71 +++++++++++++++++++ .../data/message/MessageSerializer.java | 33 ++++++--- 2 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/github/steveice10/mc/protocol/data/message/ItemHoverMessage.java diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/ItemHoverMessage.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/ItemHoverMessage.java new file mode 100644 index 00000000..b5796741 --- /dev/null +++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/ItemHoverMessage.java @@ -0,0 +1,71 @@ +package com.github.steveice10.mc.protocol.data.message; + +import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; +import com.github.steveice10.mc.protocol.data.message.style.MessageStyle; +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import java.util.List; + +@EqualsAndHashCode(callSuper = true) +public class ItemHoverMessage extends Message { + public static class Builder extends Message.Builder { + @NonNull + private String id = ""; + private int count; + private CompoundTag tag; + + public Builder id(@NonNull String id) { + this.id = id; + return this; + } + + public Builder count(@NonNull int count) { + this.count = count; + return this; + } + + public Builder tag(@NonNull CompoundTag tag) { + this.tag = tag; + return this; + } + + @Override + public Builder copy(@NonNull ItemHoverMessage message) { + super.copy(message); + this.id = message.getId(); + this.count = message.getCount(); + this.tag = message.getTag(); + return this; + } + + @Override + public ItemHoverMessage build() { + return new ItemHoverMessage(this.style, this.extra, this.id, this.count, this.tag); + } + } + + private final String id; + private final int count; + private final CompoundTag tag; + + private ItemHoverMessage(MessageStyle style, List extra, String id, int count, CompoundTag tag) { + super(style, extra); + this.id = id; + this.count = count; + this.tag = tag; + } + + public String getId() { + return this.id; + } + + public int getCount() { + return this.count; + } + + public CompoundTag getTag() { + return this.tag; + } +} diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/MessageSerializer.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/MessageSerializer.java index eb8eadeb..6eb9f61c 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/message/MessageSerializer.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/MessageSerializer.java @@ -7,11 +7,9 @@ import com.github.steveice10.mc.protocol.data.message.style.ClickEvent; import com.github.steveice10.mc.protocol.data.message.style.HoverAction; import com.github.steveice10.mc.protocol.data.message.style.HoverEvent; import com.github.steveice10.mc.protocol.data.message.style.MessageStyle; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.JsonPrimitive; +import com.github.steveice10.opennbt.conversion.builtin.CompoundTagConverter; +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.google.gson.*; import java.util.ArrayList; import java.util.List; @@ -121,11 +119,21 @@ public class MessageSerializer { } else { throw new IllegalArgumentException("Unknown NBT message type in json: " + json); } - } else if(json.has("type")) { - return new EntityHoverMessage.Builder() - .type(json.get("type").getAsString()) - .id(json.get("id").getAsString()) - .name(fromJson(json.get("name"))); + } else if(json.has("type") && json.has("id")) { + EntityHoverMessage.Builder builder = new EntityHoverMessage.Builder(); + builder.type(json.get("type").getAsString()); + builder.id(json.get("id").getAsString()); + if (json.has("name")) + builder.name(fromJson(json.get("name"))); + return builder; + } else if(json.has("id")) { + ItemHoverMessage.Builder builder = new ItemHoverMessage.Builder(); + builder.id(json.get("id").getAsString()); + if (json.has("count")) + builder.count(json.get("count").getAsInt()); + if (json.has("tag")) + builder.tag(new Gson().fromJson(json.get("tag"), CompoundTag.class)); + return builder; } else { throw new IllegalArgumentException("Unknown message type in json: " + json); } @@ -180,6 +188,11 @@ public class MessageSerializer { json.addProperty("type", entityHoverMessage.getType()); json.addProperty("id", entityHoverMessage.getId()); json.add("name", toJson(entityHoverMessage.getName())); + } else if(message instanceof ItemHoverMessage) { + ItemHoverMessage entityHoverMessage = (ItemHoverMessage) message; + json.addProperty("id", entityHoverMessage.getId()); + json.addProperty("count", entityHoverMessage.getCount()); + json.add("tag", new Gson().toJsonTree(entityHoverMessage.getTag())); } }