Added #44... And some fixes

This commit is contained in:
Norbi Peti 2016-09-05 20:37:08 +02:00
parent 3dc6f08a66
commit bd8dbe020b
6 changed files with 252 additions and 75 deletions

View file

@ -17,9 +17,6 @@ public final class ChatFormatter {
private Priority priority; private Priority priority;
private String replacewith; 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<String, String> onmatch, String openlink, public ChatFormatter(Pattern regex, Format format, Color color, Function<String, String> onmatch, String openlink,
Priority priority, String replacewith) { Priority priority, String replacewith) {
this.regex = regex; this.regex = regex;
@ -31,7 +28,7 @@ public final class ChatFormatter {
this.replacewith = replacewith; this.replacewith = replacewith;
} }
public static String Combine(List<ChatFormatter> formatters, String str) { public static void Combine(List<ChatFormatter> formatters, String str, TellrawPart tp) {
/* /*
* This method assumes that there is always a global formatter * This method assumes that there is always a global formatter
*/ */
@ -125,7 +122,6 @@ public final class ChatFormatter {
cont = false; cont = false;
} }
} }
StringBuilder finalstring = new StringBuilder();
for (int i = 0; i < sections.size(); i++) { for (int i = 0; i < sections.size(); i++) {
FormattedSection section = sections.get(i); FormattedSection section = sections.get(i);
DebugCommand.SendDebugMessage("Applying section: " + section); DebugCommand.SendDebugMessage("Applying section: " + section);
@ -135,7 +131,7 @@ public final class ChatFormatter {
Format format = null; Format format = null;
String openlink = null; String openlink = null;
String replacewith = 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) { for (ChatFormatter formatter : section.Formatters) {
DebugCommand.SendDebugMessage("Applying formatter: " + formatter); DebugCommand.SendDebugMessage("Applying formatter: " + formatter);
if (formatter.onmatch != null) if (formatter.onmatch != null)
@ -149,32 +145,23 @@ public final class ChatFormatter {
if (formatter.replacewith != null) if (formatter.replacewith != null)
replacewith = formatter.replacewith; replacewith = formatter.replacewith;
} }
finalstring.append(",{\"text\":\""); TellrawPart newtp = new TellrawPart("");
if (replacewith != null) if (replacewith != null)
finalstring.append(replacewith.replace("$1", section.Matches.get(0))); newtp.setText(replacewith.replace("$1", section.Matches.get(0)));
else else
finalstring.append(originaltext); newtp.setText(originaltext);
finalstring.append("\""); if (color != null)
if (color != null) { newtp.setColor(color);
finalstring.append(",\"color\":\""); if (format != null)
finalstring.append(color.name); newtp.setFormat(format);
finalstring.append("\"");
}
if (format != null) {
finalstring.append(",\"");
finalstring.append(format.name);
finalstring.append("\":\"true\"");
}
if (openlink != null && openlink.length() > 0) { if (openlink != null && openlink.length() > 0) {
finalstring.append(String.format( newtp.setClickEvent(TellrawEvent.create(TellrawEvent.ClickAC, TellrawEvent.ClickAction.OPEN_URL,
",\"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)))
(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 @Override
@ -183,7 +170,7 @@ public final class ChatFormatter {
.append(", ").append(priority).append(")").toString(); .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( Bold("bold"), Underlined("underlined"), Italic("italic"), Strikethrough("strikethrough"), Obfuscated(
"obfuscated"); "obfuscated");
// TODO: Add format codes to /u c <mode> // TODO: Add format codes to /u c <mode>
@ -193,12 +180,13 @@ public final class ChatFormatter {
this.name = name; this.name = name;
} }
public String GetName() { @Override
public String getName() {
return name; return name;
} }
} }
public enum Color { public enum Color implements TellrawSerializableEnum {
Black("black"), DarkBlue("dark_blue"), DarkGreen("dark_green"), DarkAqua("dark_aqua"), DarkRed( 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( "dark_red"), DarkPurple("dark_purple"), Gold("gold"), Gray("gray"), DarkGray("dark_gray"), Blue(
"blue"), Green("green"), Aqua("aqua"), Red( "blue"), Green("green"), Aqua("aqua"), Red(
@ -210,7 +198,8 @@ public final class ChatFormatter {
this.name = name; this.name = name;
} }
public String GetName() { @Override
public String getName() {
return name; return name;
} }
} }

View file

@ -10,13 +10,19 @@ import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Objective; import org.bukkit.scoreboard.Objective;
import com.earth2me.essentials.Essentials; 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.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Nation; import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident; import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town; import com.palmergames.bukkit.towny.object.Town;
import buttondevteam.thebuttonmcchat.ChatFormatter.Color;
import buttondevteam.thebuttonmcchat.ChatFormatter.Priority; import buttondevteam.thebuttonmcchat.ChatFormatter.Priority;
import buttondevteam.thebuttonmcchat.commands.UnlolCommand; import buttondevteam.thebuttonmcchat.commands.UnlolCommand;
import buttondevteam.thebuttonmcchat.commands.ucmds.admin.DebugCommand;
public class ChatProcessing { public class ChatProcessing {
private static final Pattern CONSOLE_PING_PATTERN = Pattern.compile("(?i)" + Pattern.quote("@console")); 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("(?<!\\\\)\\_((?:\\\\\\_|[^\\_])+[^\\_\\\\])\\_"); private static final Pattern UNDERLINED_PATTERN = Pattern.compile("(?<!\\\\)\\_((?:\\\\\\_|[^\\_])+[^\\_\\\\])\\_");
private static final Pattern ITALIC_PATTERN = Pattern.compile("(?<!\\\\)\\*((?:\\\\\\*|[^\\*])+[^\\*\\\\])\\*"); private static final Pattern ITALIC_PATTERN = Pattern.compile("(?<!\\\\)\\*((?:\\\\\\*|[^\\*])+[^\\*\\\\])\\*");
private static final Pattern BOLD_PATTERN = Pattern.compile("(?<!\\\\)\\*\\*((?:\\\\\\*|[^\\*])+[^\\*\\\\])\\*\\*"); private static final Pattern BOLD_PATTERN = Pattern.compile("(?<!\\\\)\\*\\*((?:\\\\\\*|[^\\*])+[^\\*\\\\])\\*\\*");
private static final String[] RainbowPresserColors = new String[] { "red", "gold", "yellow", "green", "blue",
"dark_purple" }; // TODO
private static boolean pingedconsole = false; private static boolean pingedconsole = false;
// Returns e.setCancelled // Returns e.setCancelled
@ -80,9 +88,6 @@ public class ChatProcessing {
.setPriority(Priority.Low).build()); .setPriority(Priority.Low).build());
String formattedmessage = message; String formattedmessage = message;
formattedmessage = formattedmessage.replace("\\", "\\\\");
formattedmessage = formattedmessage.replace("\"", "\\\"");
// ^ Tellraw support, needed for both the message and suggestmsg
String suggestmsg = formattedmessage; String suggestmsg = formattedmessage;
@ -92,6 +97,8 @@ public class ChatProcessing {
.setReplacewith("$1").build()); .setReplacewith("$1").build());
formatters.add(new ChatFormatterBuilder().setRegex(UNDERLINED_PATTERN) formatters.add(new ChatFormatterBuilder().setRegex(UNDERLINED_PATTERN)
.setFormat(ChatFormatter.Format.Underlined).setReplacewith("$1").build()); .setFormat(ChatFormatter.Format.Underlined).setReplacewith("$1").build());
formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile("\\\\([\\*\\_\\\\])")).setReplacewith("$1")
.build());
// URLs + Rainbow text // URLs + Rainbow text
formatters.add(new ChatFormatterBuilder().setRegex(URL_PATTERN).setFormat(ChatFormatter.Format.Underlined) formatters.add(new ChatFormatterBuilder().setRegex(URL_PATTERN).setFormat(ChatFormatter.Format.Underlined)
@ -121,7 +128,7 @@ public class ChatProcessing {
Player p = Bukkit.getPlayer(match); Player p = Bukkit.getPlayer(match);
if (p == null) { if (p == null) {
PluginMain.Instance.getLogger() PluginMain.Instance.getLogger()
.warning("Error: Can't find player " + match + " but it was reported as online."); .warning("Error: Can't find player " + match + " but was reported as online.");
return "§c" + match + "§r"; return "§c" + match + "§r";
} }
ChatPlayer mpp = ChatPlayer.GetFromPlayer(p); ChatPlayer mpp = ChatPlayer.GetFromPlayer(p);
@ -140,8 +147,8 @@ public class ChatProcessing {
if (PlayerListener.nicknames.containsKey(match)) { if (PlayerListener.nicknames.containsKey(match)) {
Player p = Bukkit.getPlayer(PlayerListener.nicknames.get(match)); Player p = Bukkit.getPlayer(PlayerListener.nicknames.get(match));
if (p == null) { if (p == null) {
PluginMain.Instance.getLogger().warning("Error: Can't find player nicknamed " + match PluginMain.Instance.getLogger().warning(
+ " but it was reported as online."); "Error: Can't find player nicknamed " + match + " but was reported as online.");
return "§c" + match + "§r"; return "§c" + match + "§r";
} }
if (PlayerListener.NotificationSound == null) if (PlayerListener.NotificationSound == null)
@ -177,49 +184,53 @@ public class ChatProcessing {
* "\",\"color\":\"%s\"},{\"text\":\"§b@console§r\",\"color\":\"blue\"},{\"text\":\"" , colormode)); System.out.println("\007"); } } * "\",\"color\":\"%s\"},{\"text\":\"§b@console§r\",\"color\":\"blue\"},{\"text\":\"" , colormode)); System.out.println("\007"); } }
*/ */
StringBuilder json = new StringBuilder(); TellrawPart json = new TellrawPart(""); // TODO: Put flair into hovertext
json.append("[\"\","); if (mp != null && mp.ChatOnly) {
json.append(String.format( json.addExtra(new TellrawPart("[C]").setHoverEvent(
"%s{\"text\":\"[%s]%s\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"Copy message\",\"color\":\"blue\"}},\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"%s\"}},", TellrawEvent.create(TellrawEvent.HoverAC, TellrawEvent.HoverAction.SHOW_TEXT, "Chat only")));
(mp != null && mp.ChatOnly }
? "{\"text\":\"[C]\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Chat only\",\"color\":\"blue\"}]}}}," json.addExtra(new TellrawPart((currentchannel.DisplayName) + (mp != null && !mp.RPMode ? "[OOC]" : ""))
: ""), .setHoverEvent(TellrawEvent.create(TellrawEvent.HoverAC, TellrawEvent.HoverAction.SHOW_TEXT,
currentchannel.DisplayName, (mp != null && !mp.RPMode ? "[OOC]" : ""), suggestmsg)); new TellrawPart("Copy message").setColor(Color.Blue)))
json.append("{\"text\":\" <\"},"); .setClickEvent(TellrawEvent.create(TellrawEvent.ClickAC, TellrawEvent.ClickAction.SUGGEST_COMMAND,
json.append(String.format("{\"text\":\"%s%s\",", (player != null ? player.getDisplayName() : sender.getName()), suggestmsg)));
(mp != null ? mp.GetFormattedFlair() : ""))); json.addExtra(new TellrawPart(" <"));
json.append("\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":["); json.addExtra(
json.append(String.format("{\"text\":\"Playername: %s\n\",", new TellrawPart(
(player != null ? player.getName() : sender.getName()))); (player != null ? player.getDisplayName() : sender.getName()))
json.append(String.format("\"color\":\"aqua\"},{\"text\":\"World: %s\n\",\"color\":\"white\"},", .setHoverEvent(
(player != null ? player.getWorld().getName() : "-"))); TellrawEvent
json.append(String.format("{\"text\":\"Respect: %s%s%s\",\"color\":\"white\"}]}}},", .create(TellrawEvent.HoverAC, TellrawEvent.HoverAction.SHOW_TEXT,
(mp != null ? (mp.FCount / (double) mp.FDeaths) : "Infinite"), new TellrawPart("")
(mp != null && mp.UserName != null && !mp.UserName.isEmpty() ? "\nUserName: " + mp.UserName : ""), .addExtra(new TellrawPart(
(mp != null && mp.PlayerName.equals("\nAlpha_Bacca44") ? "\nDeaths: " + PlayerListener.AlphaDeaths String.format("Playername: %s\n",
: ""))); (player != null ? player.getName()
json.append("{\"text\":\"> \",\"color\":\"white\"}"); : sender.getName())))
.setColor(Color.Aqua))
/* .addExtra(new TellrawPart(String.format("World: %s\n",
* int index = -1; ArrayList<String> list = new ArrayList<String>(); while ((index = message.indexOf("#", index + 1)) != -1) { int index2 = message.indexOf(" ", index + 1); if (index2 == -1) (player != null ? player.getWorld().getName()
* 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, .addExtra(new TellrawPart(String.format(
* String.format( "Respect: %s%s%s",
* "\",\"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\":\"" (mp != null ? (mp.FCount / (double) mp.FDeaths)
* , colormode, original, original)); } : "Infinite"),
*/ (mp != null && mp.UserName != null
&& !mp.UserName.isEmpty()
/* ? "\nUserName: " + mp.UserName
* json.append(String.format("{\"text\":\"%s\",\"color\":\"%s\"}]", ChatFormatter.Combine(formatters, formattedmessage), colormode)); : ""),
*/ (mp != null && mp.PlayerName.equals(
json.append(ChatFormatter.Combine(formatters, formattedmessage)); "\nAlpha_Bacca44") ? "\nDeaths: "
json.append("]"); + PlayerListener.AlphaDeaths
String jsonstr = json.toString(); : "")))))));
json.addExtra(new TellrawPart("> "));
ChatFormatter.Combine(formatters, formattedmessage, json);
String jsonstr = new Gson().toJson(json);
if (jsonstr.length() >= 32767) { if (jsonstr.length() >= 32767) {
sender.sendMessage( sender.sendMessage(
"§cError: Message too large. Try shortening it, or remove hashtags and other formatting."); "§cError: Message too large. Try shortening it, or remove hashtags and other formatting.");
return true; return true;
} }
DebugCommand.SendDebugMessage(jsonstr);
if (currentchannel.equals(Channel.TownChat) || currentchannel.equals(Channel.NationChat)) { if (currentchannel.equals(Channel.TownChat) || currentchannel.equals(Channel.NationChat)) {
if (player == null) { if (player == null) {
sender.sendMessage("§cYou are not a player!"); sender.sendMessage("§cYou are not a player!");

View file

@ -0,0 +1,78 @@
package buttondevteam.thebuttonmcchat;
import java.io.Serializable;
public final class TellrawEvent<T extends TellrawEvent.Action> 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<T> cl, T action, String value) {
this.hoverEvent = HoverAction.class.equals(cl);
this.action = action;
this.value = value;
}
private TellrawEvent(Class<T> cl, T action, TellrawPart value) {
this.hoverEvent = HoverAction.class.equals(cl);
this.action = action;
this.valueobj = value;
}
public static final Class<HoverAction> HoverAC = HoverAction.class;
public static final Class<ClickAction> ClickAC = ClickAction.class;
public static <V extends TellrawEvent.Action> TellrawEvent<V> create(Class<V> cl, V action, String value) {
return new TellrawEvent<>(cl, action, value);
}
public static <V extends TellrawEvent.Action> TellrawEvent<V> create(Class<V> 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 {
}
}

View file

@ -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<TellrawPart> extra = new ArrayList<>();
private String text;
private TellrawEvent<TellrawEvent.HoverAction> hoverEvent;
private TellrawEvent<TellrawEvent.ClickAction> 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<TellrawPart> 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<TellrawEvent.HoverAction> getHoverEvent() {
return hoverEvent;
}
public TellrawPart setHoverEvent(TellrawEvent<TellrawEvent.HoverAction> hoverEvent) {
this.hoverEvent = hoverEvent;
return this;
}
public TellrawEvent<TellrawEvent.ClickAction> getClickEvent() {
return clickEvent;
}
public TellrawPart setClickEvent(TellrawEvent<TellrawEvent.ClickAction> clickEvent) {
this.clickEvent = clickEvent;
return this;
}
}

View file

@ -0,0 +1,5 @@
package buttondevteam.thebuttonmcchat;
public interface TellrawSerializableEnum {
public String getName();
}

View file

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