From bd8dbe020b732b9af540358f7b9f11c8ff031a26 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 5 Sep 2016 20:37:08 +0200 Subject: [PATCH] Added #44... And some fixes --- .../thebuttonmcchat/ChatFormatter.java | 51 ++++------ .../thebuttonmcchat/ChatProcessing.java | 99 ++++++++++--------- .../thebuttonmcchat/TellrawEvent.java | 78 +++++++++++++++ .../thebuttonmcchat/TellrawPart.java | 73 ++++++++++++++ .../TellrawSerializableEnum.java | 5 + .../thebuttonmcchat/TellrawSerializer.java | 21 ++++ 6 files changed, 252 insertions(+), 75 deletions(-) create mode 100644 src/buttondevteam/thebuttonmcchat/TellrawEvent.java create mode 100644 src/buttondevteam/thebuttonmcchat/TellrawPart.java create mode 100644 src/buttondevteam/thebuttonmcchat/TellrawSerializableEnum.java create mode 100644 src/buttondevteam/thebuttonmcchat/TellrawSerializer.java diff --git a/src/buttondevteam/thebuttonmcchat/ChatFormatter.java b/src/buttondevteam/thebuttonmcchat/ChatFormatter.java index b875482..5b930db 100644 --- a/src/buttondevteam/thebuttonmcchat/ChatFormatter.java +++ b/src/buttondevteam/thebuttonmcchat/ChatFormatter.java @@ -17,9 +17,6 @@ public final class ChatFormatter { private Priority priority; private String replacewith; - private static final String[] RainbowPresserColors = new String[] { "red", "gold", "yellow", "green", "blue", - "dark_purple" }; // TODO: Move out to ChatProcessing - public ChatFormatter(Pattern regex, Format format, Color color, Function onmatch, String openlink, Priority priority, String replacewith) { this.regex = regex; @@ -31,7 +28,7 @@ public final class ChatFormatter { this.replacewith = replacewith; } - public static String Combine(List formatters, String str) { + public static void Combine(List formatters, String str, TellrawPart tp) { /* * This method assumes that there is always a global formatter */ @@ -125,7 +122,6 @@ public final class ChatFormatter { cont = false; } } - StringBuilder finalstring = new StringBuilder(); for (int i = 0; i < sections.size(); i++) { FormattedSection section = sections.get(i); DebugCommand.SendDebugMessage("Applying section: " + section); @@ -135,7 +131,7 @@ public final class ChatFormatter { Format format = null; String openlink = null; String replacewith = null; - section.Formatters.sort((cf1, cf2) -> cf1.priority.compareTo(cf2.priority)); + section.Formatters.sort((cf2, cf1) -> cf1.priority.compareTo(cf2.priority)); for (ChatFormatter formatter : section.Formatters) { DebugCommand.SendDebugMessage("Applying formatter: " + formatter); if (formatter.onmatch != null) @@ -149,32 +145,23 @@ public final class ChatFormatter { if (formatter.replacewith != null) replacewith = formatter.replacewith; } - finalstring.append(",{\"text\":\""); + TellrawPart newtp = new TellrawPart(""); if (replacewith != null) - finalstring.append(replacewith.replace("$1", section.Matches.get(0))); + newtp.setText(replacewith.replace("$1", section.Matches.get(0))); else - finalstring.append(originaltext); - finalstring.append("\""); - if (color != null) { - finalstring.append(",\"color\":\""); - finalstring.append(color.name); - finalstring.append("\""); - } - if (format != null) { - finalstring.append(",\""); - finalstring.append(format.name); - finalstring.append("\":\"true\""); - } + newtp.setText(originaltext); + if (color != null) + newtp.setColor(color); + if (format != null) + newtp.setFormat(format); if (openlink != null && openlink.length() > 0) { - finalstring.append(String.format( - ",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"%s\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click to open\",\"color\":\"blue\"}]}}", - (section.Matches.size() > 0 ? openlink.replace("$1", section.Matches.get(0)) : openlink))); + newtp.setClickEvent(TellrawEvent.create(TellrawEvent.ClickAC, TellrawEvent.ClickAction.OPEN_URL, + (section.Matches.size() > 0 ? openlink.replace("$1", section.Matches.get(0)) : openlink))) + .setHoverEvent(TellrawEvent.create(TellrawEvent.HoverAC, TellrawEvent.HoverAction.SHOW_TEXT, + new TellrawPart("Click to open").setColor(Color.Blue))); } - finalstring.append("}"); + tp.addExtra(newtp); } - DebugCommand.SendDebugMessage("Finalstring: " + finalstring); - return finalstring.toString(); - } @Override @@ -183,7 +170,7 @@ public final class ChatFormatter { .append(", ").append(priority).append(")").toString(); } - public enum Format { // TODO: Flag? + public enum Format implements TellrawSerializableEnum { // TODO: Flag? Bold("bold"), Underlined("underlined"), Italic("italic"), Strikethrough("strikethrough"), Obfuscated( "obfuscated"); // TODO: Add format codes to /u c @@ -193,12 +180,13 @@ public final class ChatFormatter { this.name = name; } - public String GetName() { + @Override + public String getName() { return name; } } - public enum Color { + public enum Color implements TellrawSerializableEnum { Black("black"), DarkBlue("dark_blue"), DarkGreen("dark_green"), DarkAqua("dark_aqua"), DarkRed( "dark_red"), DarkPurple("dark_purple"), Gold("gold"), Gray("gray"), DarkGray("dark_gray"), Blue( "blue"), Green("green"), Aqua("aqua"), Red( @@ -210,7 +198,8 @@ public final class ChatFormatter { this.name = name; } - public String GetName() { + @Override + public String getName() { return name; } } diff --git a/src/buttondevteam/thebuttonmcchat/ChatProcessing.java b/src/buttondevteam/thebuttonmcchat/ChatProcessing.java index 4871302..8999ab0 100644 --- a/src/buttondevteam/thebuttonmcchat/ChatProcessing.java +++ b/src/buttondevteam/thebuttonmcchat/ChatProcessing.java @@ -10,13 +10,19 @@ import org.bukkit.entity.Player; 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.palmergames.bukkit.towny.exceptions.NotRegisteredException; import com.palmergames.bukkit.towny.object.Nation; import com.palmergames.bukkit.towny.object.Resident; import com.palmergames.bukkit.towny.object.Town; +import buttondevteam.thebuttonmcchat.ChatFormatter.Color; import buttondevteam.thebuttonmcchat.ChatFormatter.Priority; import buttondevteam.thebuttonmcchat.commands.UnlolCommand; +import buttondevteam.thebuttonmcchat.commands.ucmds.admin.DebugCommand; public class ChatProcessing { private static final Pattern CONSOLE_PING_PATTERN = Pattern.compile("(?i)" + Pattern.quote("@console")); @@ -26,6 +32,8 @@ public class ChatProcessing { private static final Pattern UNDERLINED_PATTERN = Pattern.compile("(? \",\"color\":\"white\"}"); - - /* - * int index = -1; ArrayList list = new ArrayList(); while ((index = message.indexOf("#", index + 1)) != -1) { int index2 = message.indexOf(" ", index + 1); if (index2 == -1) - * index2 = message.length(); int index3 = message.indexOf("#", index + 1); if (index3 != -1 && index3 < index2) // A # occurs before a // space index2 = index3; String original = - * message.substring(index + 1, index2); list.add(original); } if (!hadurls) { for (String original : list) // Hashtags formattedmessage = formattedmessage .replace( "#" + original, - * String.format( - * "\",\"color\":\"%s\"},{\"text\":\"#%s\",\"color\":\"blue\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://twitter.com/hashtag/%s\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Open on Twitter\",\"color\":\"blue\"}]}}},{\"text\":\"" - * , colormode, original, original)); } - */ - - /* - * json.append(String.format("{\"text\":\"%s\",\"color\":\"%s\"}]", ChatFormatter.Combine(formatters, formattedmessage), colormode)); - */ - json.append(ChatFormatter.Combine(formatters, formattedmessage)); - json.append("]"); - String jsonstr = json.toString(); + TellrawPart json = new TellrawPart(""); // TODO: Put flair into hovertext + if (mp != null && mp.ChatOnly) { + 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(" <")); + 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("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("> ")); + ChatFormatter.Combine(formatters, formattedmessage, json); + String jsonstr = new Gson().toJson(json); if (jsonstr.length() >= 32767) { sender.sendMessage( "§cError: Message too large. Try shortening it, or remove hashtags and other formatting."); return true; } + DebugCommand.SendDebugMessage(jsonstr); if (currentchannel.equals(Channel.TownChat) || currentchannel.equals(Channel.NationChat)) { if (player == null) { sender.sendMessage("§cYou are not a player!"); diff --git a/src/buttondevteam/thebuttonmcchat/TellrawEvent.java b/src/buttondevteam/thebuttonmcchat/TellrawEvent.java new file mode 100644 index 0000000..7a1d1cc --- /dev/null +++ b/src/buttondevteam/thebuttonmcchat/TellrawEvent.java @@ -0,0 +1,78 @@ +package buttondevteam.thebuttonmcchat; + +import java.io.Serializable; + +public final class TellrawEvent implements Serializable { + 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 TellrawEvent(Class cl, T action, String value) { + this.hoverEvent = HoverAction.class.equals(cl); + this.action = action; + this.value = value; + } + + private TellrawEvent(Class cl, T action, TellrawPart value) { + this.hoverEvent = HoverAction.class.equals(cl); + this.action = action; + this.valueobj = value; + } + + public static final Class HoverAC = HoverAction.class; + public static final Class ClickAC = ClickAction.class; + + public static TellrawEvent create(Class cl, V action, String value) { + return new TellrawEvent<>(cl, action, value); + } + + public static TellrawEvent create(Class cl, V action, TellrawPart value) { + return new TellrawEvent<>(cl, action, value); + } + + public boolean isHoverEvent() { + return hoverEvent; + } + + public T getAction() { + return action; + } + + public String getValue() { + return value; + } + + public enum ClickAction implements Action { + OPEN_URL("open_url"), RUN_COMMAND("run_command"), SUGGEST_COMMAND("suggest_command"); + private String action; + + ClickAction(String action) { + this.action = action; + } + + @Override + public String getName() { + return action; + } + } + + public enum HoverAction implements Action { + SHOW_TEXT("show_text"), SHOW_ITEM("show_item"), SHOW_ACHIEVEMENT("show_achievement"), SHOW_ENTITY( + "show_entity"); + private String action; + + HoverAction(String action) { + this.action = action; + } + + @Override + public String getName() { + return action; + } + } + + public static interface Action extends TellrawSerializableEnum { + } +} diff --git a/src/buttondevteam/thebuttonmcchat/TellrawPart.java b/src/buttondevteam/thebuttonmcchat/TellrawPart.java new file mode 100644 index 0000000..1c56ea0 --- /dev/null +++ b/src/buttondevteam/thebuttonmcchat/TellrawPart.java @@ -0,0 +1,73 @@ +package buttondevteam.thebuttonmcchat; + +import java.io.Serializable; +import java.util.ArrayList; +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 List extra = new ArrayList<>(); + private String text; + private TellrawEvent hoverEvent; + private TellrawEvent clickEvent; + + public TellrawPart(String text) { + this.text = text; + } + + public ChatFormatter.Color getColor() { + return color; + } + + public TellrawPart setColor(ChatFormatter.Color color) { + this.color = color; + return this; + } + + public ChatFormatter.Format getFormat() { + return format; + } + + public TellrawPart setFormat(ChatFormatter.Format format) { + this.format = format; + return this; + } + + public Iterable getExtra() { + return extra; + } + + public TellrawPart addExtra(TellrawPart extra) { + this.extra.add(extra); + return this; + } + + public String getText() { + return text; + } + + public TellrawPart setText(String text) { + this.text = text; + return this; + } + + public TellrawEvent getHoverEvent() { + return hoverEvent; + } + + public TellrawPart setHoverEvent(TellrawEvent hoverEvent) { + this.hoverEvent = hoverEvent; + return this; + } + + public TellrawEvent getClickEvent() { + return clickEvent; + } + + public TellrawPart setClickEvent(TellrawEvent clickEvent) { + this.clickEvent = clickEvent; + return this; + } +} diff --git a/src/buttondevteam/thebuttonmcchat/TellrawSerializableEnum.java b/src/buttondevteam/thebuttonmcchat/TellrawSerializableEnum.java new file mode 100644 index 0000000..04295af --- /dev/null +++ b/src/buttondevteam/thebuttonmcchat/TellrawSerializableEnum.java @@ -0,0 +1,5 @@ +package buttondevteam.thebuttonmcchat; + +public interface TellrawSerializableEnum { + public String getName(); +} diff --git a/src/buttondevteam/thebuttonmcchat/TellrawSerializer.java b/src/buttondevteam/thebuttonmcchat/TellrawSerializer.java new file mode 100644 index 0000000..b367e4e --- /dev/null +++ b/src/buttondevteam/thebuttonmcchat/TellrawSerializer.java @@ -0,0 +1,21 @@ +package buttondevteam.thebuttonmcchat; + +import java.io.IOException; + +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +public class TellrawSerializer extends TypeAdapter { + + @Override + public TellrawSerializableEnum read(JsonReader reader) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void write(JsonWriter writer, TellrawSerializableEnum enumval) throws IOException { + writer.value(enumval.getName()); + } + +}