From 157133173ce5cf736530e774ccf9d27949e53de9 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Fri, 26 Jun 2020 21:49:37 +0100 Subject: [PATCH] Fix messages with HoverEvents causing NPEs and unknown message errors --- .../data/message/EntityHoverMessage.java | 70 +++++++++++++++++++ .../data/message/MessageSerializer.java | 18 ++++- .../data/message/style/HoverEvent.java | 10 +-- 3 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/github/steveice10/mc/protocol/data/message/EntityHoverMessage.java diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/EntityHoverMessage.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/EntityHoverMessage.java new file mode 100644 index 00000000..c308139a --- /dev/null +++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/EntityHoverMessage.java @@ -0,0 +1,70 @@ +package com.github.steveice10.mc.protocol.data.message; + +import com.github.steveice10.mc.protocol.data.message.style.MessageStyle; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import java.util.List; + +@EqualsAndHashCode(callSuper = true) +public class EntityHoverMessage extends Message { + public static class Builder extends Message.Builder { + @NonNull + private String type = ""; + @NonNull + private String id = ""; + private Message name; + + public Builder type(@NonNull String type) { + this.type = type; + return this; + } + + public Builder id(@NonNull String id) { + this.id = id; + return this; + } + + public Builder name(@NonNull Message name) { + this.name = name; + return this; + } + + @Override + public Builder copy(@NonNull EntityHoverMessage message) { + super.copy(message); + this.type = message.getType(); + this.id = message.getId(); + this.name = message.getName(); + return this; + } + + @Override + public EntityHoverMessage build() { + return new EntityHoverMessage(this.style, this.extra, this.type, this.id, this.name); + } + } + + private final String type; + private final String id; + private final Message name; + + private EntityHoverMessage(MessageStyle style, List extra, String type, String id, Message name) { + super(style, extra); + this.type = type; + this.id = id; + this.name = name; + } + + public String getType() { + return this.type; + } + + public String getId() { + return this.id; + } + + public Message getName() { + return this.name; + } +} 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 7e119e78..75e9b723 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 @@ -60,6 +60,10 @@ public class MessageSerializer { return toJson(message).toString(); } + public static String toJsonAsString(Message message) { + return toJson(message).getAsString(); + } + public static JsonElement toJson(Message message) { if(message instanceof TextMessage && message.getStyle().equals(MessageStyle.DEFAULT) && message.getExtra().isEmpty()) { return new JsonPrimitive(((TextMessage) message).getText()); @@ -121,6 +125,11 @@ 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 { throw new IllegalArgumentException("Unknown message type in json: " + json); } @@ -170,6 +179,11 @@ public class MessageSerializer { } else if(message instanceof StorageNbtMessage) { json.addProperty("storage", ((StorageNbtMessage) nbtMessage).getId()); } + } else if(message instanceof EntityHoverMessage) { + EntityHoverMessage entityHoverMessage = (EntityHoverMessage) message; + json.addProperty("type", entityHoverMessage.getType()); + json.addProperty("id", entityHoverMessage.getId()); + json.add("name", toJson(entityHoverMessage.getName())); } } @@ -192,7 +206,7 @@ public class MessageSerializer { if(json.has("hoverEvent")) { JsonObject hover = json.get("hoverEvent").getAsJsonObject(); - style.hoverEvent(new HoverEvent(HoverAction.byName(hover.get("action").getAsString()), fromJson(hover.get("value")))); + style.hoverEvent(new HoverEvent(HoverAction.byName(hover.get("action").getAsString()), fromJson(hover.get("contents")))); } if(json.has("insertion")) { @@ -221,7 +235,7 @@ public class MessageSerializer { if(style.getHoverEvent() != null) { JsonObject hover = new JsonObject(); hover.addProperty("action", style.getHoverEvent().getAction().toString()); - hover.add("value", toJson(style.getHoverEvent().getValue())); + hover.add("contents", toJson(style.getHoverEvent().getContents())); json.add("hoverEvent", hover); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/style/HoverEvent.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/style/HoverEvent.java index 3d41915b..e9652cf4 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/message/style/HoverEvent.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/style/HoverEvent.java @@ -11,18 +11,18 @@ public class HoverEvent { @NonNull private final HoverAction action; @NonNull - private final Message value; + private final Message contents; - public HoverEvent(@NonNull HoverAction action, @NonNull Message value) { + public HoverEvent(@NonNull HoverAction action, @NonNull Message contents) { this.action = action; - this.value = value; + this.contents = contents; } public HoverAction getAction() { return this.action; } - public Message getValue() { - return this.value; + public Message getContents() { + return this.contents; } }