mirror of
https://github.com/GeyserMC/MCProtocolLib.git
synced 2024-12-04 21:01:02 -05:00
Fix messages with HoverEvents causing NPEs and unknown message errors
This commit is contained in:
parent
c2c8e2da2d
commit
157133173c
3 changed files with 91 additions and 7 deletions
|
@ -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<Builder, EntityHoverMessage> {
|
||||
@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<Message> 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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue