Fix RichMessage

This commit is contained in:
Felix Cravic 2020-07-08 12:14:05 +02:00
parent 109afe7a54
commit 83b6665702
3 changed files with 26 additions and 10 deletions

View File

@ -2,6 +2,7 @@ package net.minestom.server.chat;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import net.minestom.server.entity.Entity; import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityType;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
public class ChatHoverEvent { public class ChatHoverEvent {
@ -26,19 +27,24 @@ public class ChatHoverEvent {
return new ChatHoverEvent("show_text", text.getJsonObject()); return new ChatHoverEvent("show_text", text.getJsonObject());
} }
public static ChatHoverEvent showText(String text) { public static ChatHoverEvent showText(String text) {
return new ChatHoverEvent("show_text", text); return new ChatHoverEvent("show_text", text);
} }
public static ChatHoverEvent showItem(ItemStack itemStack) { public static ChatHoverEvent showItem(ItemStack itemStack) {
throw new UnsupportedOperationException("Feature in progress"); return new ChatHoverEvent("show_item", "{id:4}");
//return new ChatHoverEvent("show_item", parsedItem);
} }
public static ChatHoverEvent showEntity(Entity entity) { public static ChatHoverEvent showEntity(Entity entity) {
throw new UnsupportedOperationException("Feature in progress"); final String id = entity.getUuid().toString();
//return new ChatHoverEvent("show_entity", parsedEntity); final String type = EntityType.fromId(entity.getEntityType())
.getNamespaceID().replace("minecraft:", "");
// TODO name
JsonObject object = new JsonObject();
object.addProperty("id", id);
object.addProperty("type", type);
return new ChatHoverEvent("show_entity", object);
} }
protected String getAction() { protected String getAction() {

View File

@ -83,7 +83,14 @@ public class ColoredText {
return getJsonObject().toString(); return getJsonObject().toString();
} }
protected JsonObject getJsonObject() { /**
* Get the Json representation of this colored text
* <p>
* Used to send a message
*
* @return the Json representation of the text
*/
public JsonObject getJsonObject() {
List<JsonObject> components = getComponents(); List<JsonObject> components = getComponents();
// No message, return empty object // No message, return empty object
@ -251,6 +258,7 @@ public class ColoredText {
object.addProperty("color", color); object.addProperty("color", color);
} }
object.addProperty("bold", getBoolean(specialComponentContainer.bold)); object.addProperty("bold", getBoolean(specialComponentContainer.bold));
object.addProperty("italic", getBoolean(specialComponentContainer.italic)); object.addProperty("italic", getBoolean(specialComponentContainer.italic));
object.addProperty("underlined", getBoolean(specialComponentContainer.underlined)); object.addProperty("underlined", getBoolean(specialComponentContainer.underlined));

View File

@ -62,15 +62,17 @@ public class RichMessage {
} }
private JsonObject getJsonObject() { private JsonObject getJsonObject() {
List<RichComponent> cacheComponents = new ArrayList<>(components);
// No component, return empty json object // No component, return empty json object
if (components.isEmpty()) if (cacheComponents.isEmpty())
return new JsonObject(); return new JsonObject();
RichComponent firstComponent = components.remove(0); RichComponent firstComponent = cacheComponents.remove(0);
List<JsonObject> firstComponentObjects = getComponentObject(firstComponent); List<JsonObject> firstComponentObjects = getComponentObject(firstComponent);
JsonObject mainObject = firstComponentObjects.remove(0); JsonObject mainObject = firstComponentObjects.remove(0);
if (components.isEmpty() && firstComponentObjects.isEmpty()) if (cacheComponents.isEmpty() && firstComponentObjects.isEmpty())
return mainObject; return mainObject;
JsonArray extraArray = new JsonArray(); JsonArray extraArray = new JsonArray();
@ -78,7 +80,7 @@ public class RichMessage {
extraArray.add(firstComponentObject); extraArray.add(firstComponentObject);
} }
for (RichComponent component : components) { for (RichComponent component : cacheComponents) {
List<JsonObject> componentObjects = getComponentObject(component); List<JsonObject> componentObjects = getComponentObject(component);
for (JsonObject componentObject : componentObjects) { for (JsonObject componentObject : componentObjects) {
extraArray.add(componentObject); extraArray.add(componentObject);