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 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,
Priority priority, String replacewith) {
this.regex = regex;
@ -31,7 +28,7 @@ public final class ChatFormatter {
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
*/
@ -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 <mode>
@ -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;
}
}

View file

@ -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("(?<!\\\\)\\_((?:\\\\\\_|[^\\_])+[^\\_\\\\])\\_");
private static final Pattern ITALIC_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;
// Returns e.setCancelled
@ -80,9 +88,6 @@ public class ChatProcessing {
.setPriority(Priority.Low).build());
String formattedmessage = message;
formattedmessage = formattedmessage.replace("\\", "\\\\");
formattedmessage = formattedmessage.replace("\"", "\\\"");
// ^ Tellraw support, needed for both the message and suggestmsg
String suggestmsg = formattedmessage;
@ -92,6 +97,8 @@ public class ChatProcessing {
.setReplacewith("$1").build());
formatters.add(new ChatFormatterBuilder().setRegex(UNDERLINED_PATTERN)
.setFormat(ChatFormatter.Format.Underlined).setReplacewith("$1").build());
formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile("\\\\([\\*\\_\\\\])")).setReplacewith("$1")
.build());
// URLs + Rainbow text
formatters.add(new ChatFormatterBuilder().setRegex(URL_PATTERN).setFormat(ChatFormatter.Format.Underlined)
@ -121,7 +128,7 @@ public class ChatProcessing {
Player p = Bukkit.getPlayer(match);
if (p == null) {
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";
}
ChatPlayer mpp = ChatPlayer.GetFromPlayer(p);
@ -140,8 +147,8 @@ public class ChatProcessing {
if (PlayerListener.nicknames.containsKey(match)) {
Player p = Bukkit.getPlayer(PlayerListener.nicknames.get(match));
if (p == null) {
PluginMain.Instance.getLogger().warning("Error: Can't find player nicknamed " + match
+ " but it was reported as online.");
PluginMain.Instance.getLogger().warning(
"Error: Can't find player nicknamed " + match + " but was reported as online.");
return "§c" + match + "§r";
}
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"); } }
*/
StringBuilder json = new StringBuilder();
json.append("[\"\",");
json.append(String.format(
"%s{\"text\":\"[%s]%s\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"Copy message\",\"color\":\"blue\"}},\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"%s\"}},",
(mp != null && mp.ChatOnly
? "{\"text\":\"[C]\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Chat only\",\"color\":\"blue\"}]}}},"
: ""),
currentchannel.DisplayName, (mp != null && !mp.RPMode ? "[OOC]" : ""), suggestmsg));
json.append("{\"text\":\" <\"},");
json.append(String.format("{\"text\":\"%s%s\",", (player != null ? player.getDisplayName() : sender.getName()),
(mp != null ? mp.GetFormattedFlair() : "")));
json.append("\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[");
json.append(String.format("{\"text\":\"Playername: %s\n\",",
(player != null ? player.getName() : sender.getName())));
json.append(String.format("\"color\":\"aqua\"},{\"text\":\"World: %s\n\",\"color\":\"white\"},",
(player != null ? player.getWorld().getName() : "-")));
json.append(String.format("{\"text\":\"Respect: %s%s%s\",\"color\":\"white\"}]}}},",
(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.append("{\"text\":\"> \",\"color\":\"white\"}");
/*
* 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)
* 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!");

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