From 6648bc6901d5a6ffc1abda0aceb4a2836a66119e Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 8 Sep 2016 18:27:10 +0200 Subject: [PATCH] FIXED tellraw json serializing - mostly --- src/buttondevteam/chat/ChatProcessing.java | 79 ++++++++++++------- .../chat/formatting/TellrawEvent.java | 7 +- .../chat/formatting/TellrawPart.java | 24 +++++- .../chat/formatting/TellrawSerializer.java | 43 +++++++--- 4 files changed, 108 insertions(+), 45 deletions(-) diff --git a/src/buttondevteam/chat/ChatProcessing.java b/src/buttondevteam/chat/ChatProcessing.java index 0ddc3f2..a68a63c 100644 --- a/src/buttondevteam/chat/ChatProcessing.java +++ b/src/buttondevteam/chat/ChatProcessing.java @@ -1,6 +1,7 @@ package buttondevteam.chat; import java.util.ArrayList; +import java.util.Collection; import java.util.regex.Pattern; import org.bukkit.Bukkit; @@ -11,9 +12,7 @@ import org.bukkit.scoreboard.Objective; import com.earth2me.essentials.Essentials; import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; +import com.google.gson.GsonBuilder; import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import com.palmergames.bukkit.towny.object.Nation; import com.palmergames.bukkit.towny.object.Resident; @@ -25,6 +24,8 @@ import buttondevteam.chat.formatting.ChatFormatter; import buttondevteam.chat.formatting.ChatFormatterBuilder; import buttondevteam.chat.formatting.TellrawEvent; import buttondevteam.chat.formatting.TellrawPart; +import buttondevteam.chat.formatting.TellrawSerializableEnum; +import buttondevteam.chat.formatting.TellrawSerializer; import buttondevteam.chat.formatting.ChatFormatter.Color; import buttondevteam.chat.formatting.ChatFormatter.Priority; @@ -193,29 +194,49 @@ public class ChatProcessing { json.addExtra(new TellrawPart("[C]").setHoverEvent( TellrawEvent.create(TellrawEvent.HoverAC, TellrawEvent.HoverAction.SHOW_TEXT, "Chat only"))); } - json.addExtra(new TellrawPart((currentchannel.DisplayName) + (mp != null && !mp.RPMode ? "[OOC]" : "")) - .setHoverEvent(TellrawEvent.create(TellrawEvent.HoverAC, TellrawEvent.HoverAction.SHOW_TEXT, - new TellrawPart("Copy message").setColor(Color.Blue))) - .setClickEvent(TellrawEvent.create(TellrawEvent.ClickAC, TellrawEvent.ClickAction.SUGGEST_COMMAND, - suggestmsg))); + json.addExtra( + new TellrawPart(("[" + currentchannel.DisplayName) + "]" + (mp != null && !mp.RPMode ? "[OOC]" : "")) + .setHoverEvent(TellrawEvent.create(TellrawEvent.HoverAC, TellrawEvent.HoverAction.SHOW_TEXT, + new TellrawPart("Copy message").setColor(Color.Blue))) + .setClickEvent(TellrawEvent.create(TellrawEvent.ClickAC, + TellrawEvent.ClickAction.SUGGEST_COMMAND, suggestmsg))); json.addExtra(new TellrawPart(" <")); - json.addExtra(new TellrawPart((player != null ? player.getDisplayName() : sender.getName())).setHoverEvent( - TellrawEvent.create(TellrawEvent.HoverAC, TellrawEvent.HoverAction.SHOW_TEXT, new TellrawPart("") - .addExtra(new TellrawPart(String.format("Flair: %s", mp.GetFormattedFlair()))) - .addExtra(new TellrawPart(String.format("Playername: %s\n", - (player != null ? player.getName() : sender.getName()))).setColor(Color.Aqua)) - .addExtra(new TellrawPart( - String.format("World: %s\n", (player != null ? player.getWorld().getName() : "-")))) - .addExtra(new TellrawPart(String.format("Respect: %s%s%s", - (mp != null ? (mp.FCount / (double) mp.FDeaths) : "Infinite"), - (mp != null && mp.UserName != null && !mp.UserName.isEmpty() - ? "\nUserName: " + mp.UserName : ""), - (mp != null && mp.PlayerName.equals("\nAlpha_Bacca44") - ? "\nDeaths: " + PlayerListener.AlphaDeaths - : ""))))))); + json.addExtra( + new TellrawPart( + (player != null ? player.getDisplayName() : sender.getName())) + .setHoverEvent( + TellrawEvent + .create(TellrawEvent.HoverAC, TellrawEvent.HoverAction.SHOW_TEXT, + new TellrawPart("") + .addExtra(new TellrawPart(String.format("Flair: %s", + (mp != null ? mp.GetFormattedFlair() : "-")))) + .addExtra(new TellrawPart( + String.format("\nPlayername: %s\n", + (player != null ? player.getName() + : sender.getName()))) + .setColor(Color.Aqua)) + .addExtra(new TellrawPart(String.format("World: %s\n", + (player != null ? player.getWorld().getName() + : "-")))) + .addExtra(new TellrawPart(String.format( + "Respect: %s%s%s", + (mp != null ? (mp.FCount / (double) mp.FDeaths) + : "Infinite"), + (mp != null && mp.UserName != null + && !mp.UserName.isEmpty() + ? "\nUserName: " + mp.UserName + : ""), + (mp != null && mp.PlayerName.equals( + "\nAlpha_Bacca44") ? "\nDeaths: " + + PlayerListener.AlphaDeaths + : ""))))))); json.addExtra(new TellrawPart("> ")); ChatFormatter.Combine(formatters, formattedmessage, json); - String jsonstr = new Gson().toJson(json); + Gson gson = new GsonBuilder() + .registerTypeHierarchyAdapter(TellrawSerializableEnum.class, new TellrawSerializer.TwEnum()) + .registerTypeHierarchyAdapter(Collection.class, new TellrawSerializer.TwCollection()) + .disableHtmlEscaping().create(); + String jsonstr = gson.toJson(json); if (jsonstr.length() >= 32767) { sender.sendMessage( "§cError: Message too long. Try shortening it, or remove hashtags and other formatting."); @@ -266,7 +287,7 @@ public class ChatProcessing { } } PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, - String.format("tellraw @a[score_town=%d,score_town_min=%d] %s", index, index, json.toString())); + String.format("tellraw @a[score_town=%d,score_town_min=%d] %s", index, index, jsonstr)); } catch (IllegalStateException e) { e.printStackTrace(); player.sendMessage("§cAn error occured while sending the message. (IllegalStateException)"); @@ -312,8 +333,8 @@ public class ChatProcessing { } catch (Exception e) { } } - PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, String - .format("tellraw @a[score_nation=%d,score_nation_min=%d] %s", index, index, json.toString())); + PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, + String.format("tellraw @a[score_nation=%d,score_nation_min=%d] %s", index, index, jsonstr)); } catch (IllegalStateException e) { e.printStackTrace(); player.sendMessage("§cAn error occured while sending the message. (IllegalStateException)"); @@ -337,7 +358,7 @@ public class ChatProcessing { obj.getScore(p.getName()).setScore(0); } PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, - String.format("tellraw @a[score_admin=%d,score_admin_min=%d] %s", 1, 1, json.toString())); + String.format("tellraw @a[score_admin=%d,score_admin_min=%d] %s", 1, 1, jsonstr)); } catch (IllegalStateException e) { e.printStackTrace(); player.sendMessage("§cAn error occured while sending the message. (IllegalStateException)"); @@ -361,7 +382,7 @@ public class ChatProcessing { obj.getScore(p.getName()).setScore(0); } PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, - String.format("tellraw @a[score_mod=%d,score_mod_min=%d] %s", 1, 1, json.toString())); + String.format("tellraw @a[score_mod=%d,score_mod_min=%d] %s", 1, 1, jsonstr)); } catch (IllegalStateException e) { e.printStackTrace(); player.sendMessage("§cAn error occured while sending the message. (IllegalStateException)"); @@ -373,7 +394,7 @@ public class ChatProcessing { } } else PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, - String.format("tellraw @a %s", json.toString())); + String.format("tellraw @a %s", jsonstr)); PluginMain.Instance.getServer().getConsoleSender() .sendMessage(String.format("[%s] <%s%s> %s", currentchannel.DisplayName, (player != null ? player.getDisplayName() : sender.getName()), diff --git a/src/buttondevteam/chat/formatting/TellrawEvent.java b/src/buttondevteam/chat/formatting/TellrawEvent.java index 7f43dc5..3600aca 100644 --- a/src/buttondevteam/chat/formatting/TellrawEvent.java +++ b/src/buttondevteam/chat/formatting/TellrawEvent.java @@ -6,8 +6,7 @@ public final class TellrawEvent implements Serial private static final long serialVersionUID = -1681364161210561505L; private boolean hoverEvent; private T action; - private String value; - private TellrawPart valueobj; // TODO: FIx serializer, actually add own serializer, etc. + private Object value; private TellrawEvent(Class cl, T action, String value) { this.hoverEvent = HoverAction.class.equals(cl); @@ -18,7 +17,7 @@ public final class TellrawEvent implements Serial private TellrawEvent(Class cl, T action, TellrawPart value) { this.hoverEvent = HoverAction.class.equals(cl); this.action = action; - this.valueobj = value; + this.value = value; } public static final Class HoverAC = HoverAction.class; @@ -40,7 +39,7 @@ public final class TellrawEvent implements Serial return action; } - public String getValue() { + public Object getValue() { return value; } diff --git a/src/buttondevteam/chat/formatting/TellrawPart.java b/src/buttondevteam/chat/formatting/TellrawPart.java index 505f942..ee7dde8 100644 --- a/src/buttondevteam/chat/formatting/TellrawPart.java +++ b/src/buttondevteam/chat/formatting/TellrawPart.java @@ -7,7 +7,12 @@ import java.util.List; public final class TellrawPart implements Serializable { private static final long serialVersionUID = 4125357644462144024L; private ChatFormatter.Color color; - private ChatFormatter.Format format; + private transient ChatFormatter.Format format; + private boolean italics; + private boolean bold; + private boolean underlined; + private boolean strikethrough; + private boolean obfuscated; private List extra = new ArrayList<>(); private String text; private TellrawEvent hoverEvent; @@ -32,6 +37,23 @@ public final class TellrawPart implements Serializable { public TellrawPart setFormat(ChatFormatter.Format format) { this.format = format; + this.italics = false; + this.bold = false; + this.underlined = false; + this.strikethrough = false; + this.obfuscated = false; + if (format.equals(ChatFormatter.Format.Italic)) + this.italics = true; + else if (format.equals(ChatFormatter.Format.Bold)) + this.bold = true; + else if (format.equals(ChatFormatter.Format.Underlined)) + this.underlined = true; + else if (format.equals(ChatFormatter.Format.Strikethrough)) + this.strikethrough = true; + else if (format.equals(ChatFormatter.Format.Obfuscated)) + this.obfuscated = true; + else // TODO: Don't serialize false values, find out why is it bugging + throw new UnsupportedOperationException("Trying to set to an unknown format!"); return this; } diff --git a/src/buttondevteam/chat/formatting/TellrawSerializer.java b/src/buttondevteam/chat/formatting/TellrawSerializer.java index 7bee357..e8354d2 100644 --- a/src/buttondevteam/chat/formatting/TellrawSerializer.java +++ b/src/buttondevteam/chat/formatting/TellrawSerializer.java @@ -1,21 +1,42 @@ package buttondevteam.chat.formatting; import java.io.IOException; +import java.lang.reflect.Type; +import java.util.Collection; -import com.google.gson.TypeAdapter; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonWriter; +import com.google.gson.*; +import com.google.gson.stream.*; -public class TellrawSerializer extends TypeAdapter { +public abstract class TellrawSerializer { + public static class TwEnum extends TypeAdapter { + @Override + public TellrawSerializableEnum read(JsonReader reader) throws IOException { + throw new UnsupportedOperationException(); + } - @Override - public TellrawSerializableEnum read(JsonReader reader) throws IOException { - throw new UnsupportedOperationException(); + @Override + public void write(JsonWriter writer, TellrawSerializableEnum enumval) throws IOException { + if (enumval == null) + writer.nullValue(); + else + writer.value(enumval.getName()); + } } - @Override - public void write(JsonWriter writer, TellrawSerializableEnum enumval) throws IOException { - writer.value(enumval.getName()); - } + public static class TwCollection implements JsonSerializer> { + @Override + public JsonElement serialize(Collection src, Type typeOfSrc, JsonSerializationContext context) { + if (src == null || src.isEmpty()) + return null; + JsonArray array = new JsonArray(); + + for (Object child : src) { + JsonElement element = context.serialize(child); + array.add(element); + } + + return array; + } + } }