Fix HoverEvent messages

This commit is contained in:
rtm516 2020-06-26 23:20:09 +01:00
parent b43dcab2e1
commit 762fca77e3
2 changed files with 94 additions and 10 deletions

View file

@ -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<Builder, ItemHoverMessage> {
@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<Message> 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;
}
}

View file

@ -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()));
}
}