From 750b71de65e6e53a527aab94976d40b8c97f08a1 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 23 Sep 2019 13:21:42 +0200 Subject: [PATCH] Created FormatterComponent & formatters can be disabled It allows disabling chat processing as a whole or formatting Formatters can be disabled one by one #85 --- .../java/buttondevteam/chat/ChatUtils.java | 26 +- .../chatonly/ChatOnlyComponent.java | 4 +- .../formatter}/ChatProcessing.java | 50 ++-- .../formatter/FormatterComponent.java | 39 +++ .../formatter}/formatting/ChatFormatter.java | 15 +- .../formatting/FormattedSection.java | 2 +- .../formatter}/formatting/TellrawEvent.java | 152 ++++++------ .../formatter}/formatting/TellrawPart.java | 230 +++++++++--------- .../formatting/TellrawSerializer.java | 118 ++++----- .../chat/components/towny/TownyAnnouncer.java | 8 +- .../chat/components/towny/TownyComponent.java | 2 +- .../chat/listener/PlayerListener.java | 31 ++- .../formatter}/ChatFormatIT.java | 28 ++- 13 files changed, 392 insertions(+), 313 deletions(-) rename src/main/java/buttondevteam/chat/{ => components/formatter}/ChatProcessing.java (88%) create mode 100644 src/main/java/buttondevteam/chat/components/formatter/FormatterComponent.java rename src/main/java/buttondevteam/chat/{ => components/formatter}/formatting/ChatFormatter.java (96%) rename src/main/java/buttondevteam/chat/{ => components/formatter}/formatting/FormattedSection.java (94%) rename src/main/java/buttondevteam/chat/{ => components/formatter}/formatting/TellrawEvent.java (93%) rename src/main/java/buttondevteam/chat/{ => components/formatter}/formatting/TellrawPart.java (92%) rename src/main/java/buttondevteam/chat/{ => components/formatter}/formatting/TellrawSerializer.java (90%) rename src/test/java/buttondevteam/chat/{ => components/formatter}/ChatFormatIT.java (86%) diff --git a/src/main/java/buttondevteam/chat/ChatUtils.java b/src/main/java/buttondevteam/chat/ChatUtils.java index c090a35..d716acc 100644 --- a/src/main/java/buttondevteam/chat/ChatUtils.java +++ b/src/main/java/buttondevteam/chat/ChatUtils.java @@ -1,11 +1,19 @@ package buttondevteam.chat; +import buttondevteam.lib.TBMCChatEvent; +import buttondevteam.lib.ThorpeUtils; +import lombok.var; import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import java.util.Optional; +import java.util.function.Function; public final class ChatUtils { - private ChatUtils() {} + public static final String MCORIGIN = "Minecraft"; //Shouldn't change, like ever - TBMCPlayer.getFolderForType(TBMCPlayer.class) capitalized + + private ChatUtils() { + } /** * Dispatch a console command. @@ -33,4 +41,20 @@ public final class ChatUtils { int b = str.indexOf(end, a); return a != -1 && b != -1 ? Optional.of(str.substring(a, b)) : Optional.empty(); } + + /** + * Sends a regular (non-Markdown) chat message. Used as a fallback if the chat processing fails. + * + * @param e The chat event + * @param modifier A function that alters the message to be displayed to the player + */ + public static void sendChatMessage(TBMCChatEvent e, Function modifier) { + var str = "[" + e.getChannel().DisplayName().get() + "] <" + + ThorpeUtils.getDisplayName(e.getSender()) + "> " + e.getMessage(); + str = modifier.apply(str); + for (Player p : Bukkit.getOnlinePlayers()) + if (e.shouldSendTo(p)) + p.sendMessage(str); + Bukkit.getConsoleSender().sendMessage(str); + } } diff --git a/src/main/java/buttondevteam/chat/components/chatonly/ChatOnlyComponent.java b/src/main/java/buttondevteam/chat/components/chatonly/ChatOnlyComponent.java index 7f17848..5f012dd 100644 --- a/src/main/java/buttondevteam/chat/components/chatonly/ChatOnlyComponent.java +++ b/src/main/java/buttondevteam/chat/components/chatonly/ChatOnlyComponent.java @@ -1,8 +1,8 @@ package buttondevteam.chat.components.chatonly; import buttondevteam.chat.ChatPlayer; -import buttondevteam.chat.formatting.TellrawEvent; -import buttondevteam.chat.formatting.TellrawPart; +import buttondevteam.chat.components.formatter.formatting.TellrawEvent; +import buttondevteam.chat.components.formatter.formatting.TellrawPart; import buttondevteam.core.ComponentManager; import buttondevteam.lib.architecture.Component; import buttondevteam.lib.player.TBMCPlayer; diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/components/formatter/ChatProcessing.java similarity index 88% rename from src/main/java/buttondevteam/chat/ChatProcessing.java rename to src/main/java/buttondevteam/chat/components/formatter/ChatProcessing.java index e472147..8b7ae65 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/components/formatter/ChatProcessing.java @@ -1,13 +1,17 @@ -package buttondevteam.chat; +package buttondevteam.chat.components.formatter; +import buttondevteam.chat.ChatPlayer; +import buttondevteam.chat.ChatUtils; +import buttondevteam.chat.PluginMain; +import buttondevteam.chat.VanillaUtils; import buttondevteam.chat.commands.ucmds.admin.DebugCommand; import buttondevteam.chat.components.chatonly.ChatOnlyComponent; +import buttondevteam.chat.components.formatter.formatting.ChatFormatter; +import buttondevteam.chat.components.formatter.formatting.TellrawEvent; +import buttondevteam.chat.components.formatter.formatting.TellrawPart; +import buttondevteam.chat.components.formatter.formatting.TellrawSerializer; import buttondevteam.chat.components.fun.FunComponent; import buttondevteam.chat.components.towny.TownyComponent; -import buttondevteam.chat.formatting.ChatFormatter; -import buttondevteam.chat.formatting.TellrawEvent; -import buttondevteam.chat.formatting.TellrawPart; -import buttondevteam.chat.formatting.TellrawSerializer; import buttondevteam.chat.listener.PlayerListener; import buttondevteam.core.ComponentManager; import buttondevteam.core.component.channel.Channel; @@ -101,12 +105,11 @@ public class ChatProcessing { .registerTypeAdapter(Boolean.class, new TellrawSerializer.TwBool()) .registerTypeAdapter(boolean.class, new TellrawSerializer.TwBool()).disableHtmlEscaping().create(); private static final String[] testPlayers = {"Koiiev", "iie", "Alisolarflare", "NorbiPeti", "Arsen_Derby_FTW", "carrot_lynx"}; - public static final String MCORIGIN = "Minecraft"; //Shouldn't change, like ever - TBMCPlayer.getFolderForType(TBMCPlayer.class) capitalized private ChatProcessing() { } - public static boolean ProcessChat(TBMCChatEvent e) { + public static boolean ProcessChat(TBMCChatEvent e, FormatterComponent component) { Channel channel = e.getChannel(); CommandSender sender = e.getSender(); String message = e.getMessage(); @@ -141,19 +144,24 @@ public class ChatProcessing { colormode = Color.Green; // If greentext, ignore channel or player colors - ArrayList formatters = addFormatters(colormode, e::shouldSendTo); - if (colormode == channel.Color().get() && mp != null && mp.RainbowPresserColorMode) { // Only overwrite channel color - final AtomicInteger rpc = new AtomicInteger(0); - formatters.add(ChatFormatter.builder().regex(WORD_PATTERN).color(colormode).onmatch((match, cf, s) -> { - cf.setColor(RainbowPresserColors[rpc.getAndUpdate(i -> ++i < RainbowPresserColors.length ? i : 0)]); - return match; - }).build()); - } - pingedconsole = false; // Will set it to true onmatch (static constructor) + ArrayList formatters; + if (component.allowFormatting().get()) { + formatters = addFormatters(colormode, e::shouldSendTo); + if (colormode == channel.Color().get() && mp != null && mp.RainbowPresserColorMode) { // Only overwrite channel color + final AtomicInteger rpc = new AtomicInteger(0); + formatters.add(ChatFormatter.builder().regex(WORD_PATTERN).color(colormode).onmatch((match, cf, s) -> { + cf.setColor(RainbowPresserColors[rpc.getAndUpdate(i -> ++i < RainbowPresserColors.length ? i : 0)]); + return match; + }).build()); + } + pingedconsole = false; // Will set it to true onmatch (static constructor) + } else + formatters = Lists.newArrayList(ChatFormatter.builder().regex(ENTIRE_MESSAGE_PATTERN) + .color(Color.White).priority(Priority.Low).build()); //This formatter is necessary TellrawPart json = createTellraw(sender, message, player, mp, e.getUser(), channelidentifier, e.getOrigin()); long combinetime = System.nanoTime(); - ChatFormatter.Combine(formatters, message, json); + ChatFormatter.Combine(formatters, message, json, component.getConfig()); combinetime = System.nanoTime() - combinetime; String jsonstr = toJson(json); if (jsonstr.length() >= 32767) { @@ -203,15 +211,15 @@ public class ChatProcessing { } static TellrawPart createTellraw(CommandSender sender, String message, @Nullable Player player, - @Nullable ChatPlayer mp, @Nullable ChromaGamerBase cg, final String channelidentifier, - String origin) { + @Nullable ChatPlayer mp, @Nullable ChromaGamerBase cg, final String channelidentifier, + String origin) { TellrawPart json = new TellrawPart(""); ChatOnlyComponent.tellrawCreate(mp, json); //TODO: Make nice API json.addExtra( new TellrawPart(channelidentifier) .setHoverEvent( TellrawEvent.create(TellrawEvent.HoverAction.SHOW_TEXT, - new TellrawPart((MCORIGIN.equals(origin) ? "" : "From " + origin + "n") + new TellrawPart((ChatUtils.MCORIGIN.equals(origin) ? "" : "From " + origin + "n") + "Copy message").setColor(Color.Blue))) .setClickEvent(TellrawEvent.create(TellrawEvent.ClickAction.SUGGEST_COMMAND, message))); if (PluginMain.permission.has(sender, "tbmc.badge.diamond")) @@ -237,7 +245,7 @@ public class ChatProcessing { } static String getChannelID(Channel channel, String origin) { - return ("[" + (MCORIGIN.equals(origin) ? "" : "§8" + origin.substring(0, 1) + "§r|") + channel.DisplayName().get()) + return ("[" + (ChatUtils.MCORIGIN.equals(origin) ? "" : "§8" + origin.substring(0, 1) + "§r|") + channel.DisplayName().get()) + "]"; } diff --git a/src/main/java/buttondevteam/chat/components/formatter/FormatterComponent.java b/src/main/java/buttondevteam/chat/components/formatter/FormatterComponent.java new file mode 100644 index 0000000..fadc86c --- /dev/null +++ b/src/main/java/buttondevteam/chat/components/formatter/FormatterComponent.java @@ -0,0 +1,39 @@ +package buttondevteam.chat.components.formatter; + +import buttondevteam.chat.PluginMain; +import buttondevteam.core.ComponentManager; +import buttondevteam.lib.TBMCChatEvent; +import buttondevteam.lib.architecture.Component; +import buttondevteam.lib.architecture.ConfigData; + +/** + * This component handles the custom processing of chat messages. If this component is disabled channels won't be supported either in Minecraft. + * If you only want to disable the formatting features, set allowFormatting to false. + */ +public class FormatterComponent extends Component { + ConfigData allowFormatting() { + return getConfig().getData("allowFormatting", true); + } + + @Override + protected void enable() { + + } + + @Override + protected void disable() { + + } + + /** + * Handles the chat if the component is enabled. + * + * @param event The chat event + * @return Whether the chat message shouldn't be sent for some reason + */ + public static boolean handleChat(TBMCChatEvent event) { + FormatterComponent component = ComponentManager.getIfEnabled(FormatterComponent.class); + if (component == null) return false; + return ChatProcessing.ProcessChat(event, component); + } +} diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/components/formatter/formatting/ChatFormatter.java similarity index 96% rename from src/main/java/buttondevteam/chat/formatting/ChatFormatter.java rename to src/main/java/buttondevteam/chat/components/formatter/formatting/ChatFormatter.java index 8c2d03e..dbd65ea 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/components/formatter/formatting/ChatFormatter.java @@ -1,7 +1,9 @@ -package buttondevteam.chat.formatting; +package buttondevteam.chat.components.formatter.formatting; -import buttondevteam.chat.ChatProcessing; import buttondevteam.chat.commands.ucmds.admin.DebugCommand; +import buttondevteam.chat.components.formatter.ChatProcessing; +import buttondevteam.lib.architecture.ConfigData; +import buttondevteam.lib.architecture.IHaveConfig; import buttondevteam.lib.chat.Color; import buttondevteam.lib.chat.Priority; import lombok.Builder; @@ -40,6 +42,11 @@ public final class ChatFormatter { @Builder.Default Type type = Type.Normal; String hoverText; + String name; + + private ConfigData enabled(IHaveConfig config) { + return config.getData(name + ".enabled", true); + } public enum Type { Normal, @@ -58,13 +65,15 @@ public final class ChatFormatter { R apply(T1 x1, T2 x2, T3 x3); } - public static void Combine(List formatters, String str, TellrawPart tp) { + public static void Combine(List formatters, String str, TellrawPart tp, IHaveConfig config) { /* * This method assumes that there is always a global formatter */ header("ChatFormatter.Combine begin"); ArrayList sections = new ArrayList<>(); + if (config != null) //null if testing + formatters.removeIf(cf -> !cf.enabled(config).get()); //Remove disabled formatters createSections(formatters, str, sections, true); header("Section creation (excluders done)"); diff --git a/src/main/java/buttondevteam/chat/formatting/FormattedSection.java b/src/main/java/buttondevteam/chat/components/formatter/formatting/FormattedSection.java similarity index 94% rename from src/main/java/buttondevteam/chat/formatting/FormattedSection.java rename to src/main/java/buttondevteam/chat/components/formatter/formatting/FormattedSection.java index 15101c4..c609e58 100644 --- a/src/main/java/buttondevteam/chat/formatting/FormattedSection.java +++ b/src/main/java/buttondevteam/chat/components/formatter/formatting/FormattedSection.java @@ -1,4 +1,4 @@ -package buttondevteam.chat.formatting; +package buttondevteam.chat.components.formatter.formatting; import java.util.ArrayList; import java.util.Collection; diff --git a/src/main/java/buttondevteam/chat/formatting/TellrawEvent.java b/src/main/java/buttondevteam/chat/components/formatter/formatting/TellrawEvent.java similarity index 93% rename from src/main/java/buttondevteam/chat/formatting/TellrawEvent.java rename to src/main/java/buttondevteam/chat/components/formatter/formatting/TellrawEvent.java index 8f43d63..4c532e5 100644 --- a/src/main/java/buttondevteam/chat/formatting/TellrawEvent.java +++ b/src/main/java/buttondevteam/chat/components/formatter/formatting/TellrawEvent.java @@ -1,76 +1,76 @@ -package buttondevteam.chat.formatting; - -import java.io.Serializable; - -import buttondevteam.lib.chat.TellrawSerializableEnum; - -public final class TellrawEvent implements Serializable { - private static final long serialVersionUID = -1681364161210561505L; - private transient boolean hoverEvent; - private T action; - private Object value; - - private TellrawEvent(T action, String value) { - this.hoverEvent = action instanceof HoverAction; - this.action = action; - this.value = value; - } - - private TellrawEvent(T action, TellrawPart value) { - this.hoverEvent = action instanceof HoverAction; - this.action = action; - this.value = value; - } - - public static TellrawEvent create(V action, String value) { - return new TellrawEvent<>(action, value); - } - - public static TellrawEvent create(V action, TellrawPart value) { - return new TellrawEvent<>(action, value); - } - - public boolean isHoverEvent() { - return hoverEvent; - } - - public T getAction() { - return action; - } - - public Object 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 { - } -} +package buttondevteam.chat.components.formatter.formatting; + +import buttondevteam.lib.chat.TellrawSerializableEnum; + +import java.io.Serializable; + +public final class TellrawEvent implements Serializable { + private static final long serialVersionUID = -1681364161210561505L; + private transient boolean hoverEvent; + private T action; + private Object value; + + private TellrawEvent(T action, String value) { + this.hoverEvent = action instanceof HoverAction; + this.action = action; + this.value = value; + } + + private TellrawEvent(T action, TellrawPart value) { + this.hoverEvent = action instanceof HoverAction; + this.action = action; + this.value = value; + } + + public static TellrawEvent create(V action, String value) { + return new TellrawEvent<>(action, value); + } + + public static TellrawEvent create(V action, TellrawPart value) { + return new TellrawEvent<>(action, value); + } + + public boolean isHoverEvent() { + return hoverEvent; + } + + public T getAction() { + return action; + } + + public Object 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/main/java/buttondevteam/chat/formatting/TellrawPart.java b/src/main/java/buttondevteam/chat/components/formatter/formatting/TellrawPart.java similarity index 92% rename from src/main/java/buttondevteam/chat/formatting/TellrawPart.java rename to src/main/java/buttondevteam/chat/components/formatter/formatting/TellrawPart.java index 0e7dc72..2425574 100644 --- a/src/main/java/buttondevteam/chat/formatting/TellrawPart.java +++ b/src/main/java/buttondevteam/chat/components/formatter/formatting/TellrawPart.java @@ -1,115 +1,115 @@ -package buttondevteam.chat.formatting; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; - -import buttondevteam.lib.chat.*; - -public final class TellrawPart implements Serializable { - private static final long serialVersionUID = 4125357644462144024L; - private Color color; - private boolean italic; - private boolean bold; - private boolean underlined; - private boolean strikethrough; - private boolean obfuscated; - private List extra = new ArrayList<>(); - private String text; - private TellrawEvent hoverEvent; - private TellrawEvent clickEvent; - - public TellrawPart(String text) { - this.text = text; - } - - public Color getColor() { - return color; - } - - public TellrawPart setColor(Color color) { - this.color = color; - return this; - } - - public boolean isItalic() { - return italic; - } - - public TellrawPart setItalic(boolean italic) { - this.italic = italic; - return this; - } - - public boolean isBold() { - return bold; - } - - public TellrawPart setBold(boolean bold) { - this.bold = bold; - return this; - } - - public boolean isUnderlined() { - return underlined; - } - - public TellrawPart setUnderlined(boolean underlined) { - this.underlined = underlined; - return this; - } - - public boolean isStrikethrough() { - return strikethrough; - } - - public TellrawPart setStrikethrough(boolean strikethrough) { - this.strikethrough = strikethrough; - return this; - } - - public boolean isObfuscated() { - return obfuscated; - } - - public TellrawPart setObfuscated(boolean obfuscated) { - this.obfuscated = obfuscated; - 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; - } -} +package buttondevteam.chat.components.formatter.formatting; + +import buttondevteam.lib.chat.Color; + +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 Color color; + private boolean italic; + private boolean bold; + private boolean underlined; + private boolean strikethrough; + private boolean obfuscated; + private List extra = new ArrayList<>(); + private String text; + private TellrawEvent hoverEvent; + private TellrawEvent clickEvent; + + public TellrawPart(String text) { + this.text = text; + } + + public Color getColor() { + return color; + } + + public TellrawPart setColor(Color color) { + this.color = color; + return this; + } + + public boolean isItalic() { + return italic; + } + + public TellrawPart setItalic(boolean italic) { + this.italic = italic; + return this; + } + + public boolean isBold() { + return bold; + } + + public TellrawPart setBold(boolean bold) { + this.bold = bold; + return this; + } + + public boolean isUnderlined() { + return underlined; + } + + public TellrawPart setUnderlined(boolean underlined) { + this.underlined = underlined; + return this; + } + + public boolean isStrikethrough() { + return strikethrough; + } + + public TellrawPart setStrikethrough(boolean strikethrough) { + this.strikethrough = strikethrough; + return this; + } + + public boolean isObfuscated() { + return obfuscated; + } + + public TellrawPart setObfuscated(boolean obfuscated) { + this.obfuscated = obfuscated; + 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/main/java/buttondevteam/chat/formatting/TellrawSerializer.java b/src/main/java/buttondevteam/chat/components/formatter/formatting/TellrawSerializer.java similarity index 90% rename from src/main/java/buttondevteam/chat/formatting/TellrawSerializer.java rename to src/main/java/buttondevteam/chat/components/formatter/formatting/TellrawSerializer.java index f8b591e..b1a36fd 100644 --- a/src/main/java/buttondevteam/chat/formatting/TellrawSerializer.java +++ b/src/main/java/buttondevteam/chat/components/formatter/formatting/TellrawSerializer.java @@ -1,59 +1,59 @@ -package buttondevteam.chat.formatting; - -import java.io.IOException; -import java.lang.reflect.Type; -import java.util.Collection; - -import com.google.gson.*; -import com.google.gson.stream.*; - -import buttondevteam.lib.chat.TellrawSerializableEnum; - -public abstract class TellrawSerializer { - public static class TwEnum extends TypeAdapter { - @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()); - } - } - - 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; - } - } - - public static class TwBool extends TypeAdapter { - @Override - public Boolean read(JsonReader reader) throws IOException { - throw new UnsupportedOperationException(); - } - - @Override - public void write(JsonWriter writer, Boolean val) throws IOException { - if (val) - writer.value(val); - else - writer.nullValue(); - } - } -} +package buttondevteam.chat.components.formatter.formatting; + +import buttondevteam.lib.chat.TellrawSerializableEnum; +import com.google.gson.*; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +import java.io.IOException; +import java.lang.reflect.Type; +import java.util.Collection; + +public abstract class TellrawSerializer { + public static class TwEnum extends TypeAdapter { + @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()); + } + } + + 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; + } + } + + public static class TwBool extends TypeAdapter { + @Override + public Boolean read(JsonReader reader) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void write(JsonWriter writer, Boolean val) throws IOException { + if (val) + writer.value(val); + else + writer.nullValue(); + } + } +} diff --git a/src/main/java/buttondevteam/chat/components/towny/TownyAnnouncer.java b/src/main/java/buttondevteam/chat/components/towny/TownyAnnouncer.java index e8cf4d6..ccfd298 100644 --- a/src/main/java/buttondevteam/chat/components/towny/TownyAnnouncer.java +++ b/src/main/java/buttondevteam/chat/components/towny/TownyAnnouncer.java @@ -1,6 +1,6 @@ package buttondevteam.chat.components.towny; -import buttondevteam.chat.ChatProcessing; +import buttondevteam.chat.ChatUtils; import buttondevteam.core.component.channel.Channel; import buttondevteam.lib.TBMCSystemChatEvent; import buttondevteam.lib.chat.TBMCChatAPI; @@ -34,18 +34,18 @@ public class TownyAnnouncer { if (townChannel == null) return; TBMCChatAPI.SendSystemMessage(townChannel, new Channel.RecipientTestResult(TownyComponent.getTownNationIndex(groupID, false), groupID), - message, target, ChatProcessing.MCORIGIN); + message, target, ChatUtils.MCORIGIN); break; case "Nation": if (nationChannel == null) return; TBMCChatAPI.SendSystemMessage(nationChannel, new Channel.RecipientTestResult(TownyComponent.getTownNationIndex(groupID, true), groupID), - message, target, ChatProcessing.MCORIGIN); + message, target, ChatUtils.MCORIGIN); break; case "Global": TBMCChatAPI.SendSystemMessage(Channel.GlobalChat, Channel.RecipientTestResult.ALL, - message, target, ChatProcessing.MCORIGIN); + message, target, ChatUtils.MCORIGIN); break; } } diff --git a/src/main/java/buttondevteam/chat/components/towny/TownyComponent.java b/src/main/java/buttondevteam/chat/components/towny/TownyComponent.java index dfdec58..a77bc83 100644 --- a/src/main/java/buttondevteam/chat/components/towny/TownyComponent.java +++ b/src/main/java/buttondevteam/chat/components/towny/TownyComponent.java @@ -2,7 +2,7 @@ package buttondevteam.chat.components.towny; import buttondevteam.chat.PluginMain; import buttondevteam.chat.VanillaUtils; -import buttondevteam.chat.formatting.TellrawPart; +import buttondevteam.chat.components.formatter.formatting.TellrawPart; import buttondevteam.core.component.channel.Channel; import buttondevteam.lib.architecture.Component; import buttondevteam.lib.chat.Color; diff --git a/src/main/java/buttondevteam/chat/listener/PlayerListener.java b/src/main/java/buttondevteam/chat/listener/PlayerListener.java index 54f7b4d..7fa3048 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerListener.java @@ -1,10 +1,11 @@ package buttondevteam.chat.listener; import buttondevteam.chat.ChatPlayer; -import buttondevteam.chat.ChatProcessing; +import buttondevteam.chat.ChatUtils; import buttondevteam.chat.PluginMain; import buttondevteam.chat.commands.ucmds.HistoryCommand; import buttondevteam.chat.components.flair.FlairComponent; +import buttondevteam.chat.components.formatter.FormatterComponent; import buttondevteam.chat.components.towncolors.TownColorComponent; import buttondevteam.core.ComponentManager; import buttondevteam.core.component.channel.Channel; @@ -51,7 +52,8 @@ public class PlayerListener implements Listener { if (event.isCancelled()) return; //The custom event is called in the core, but doesn't cancel the MC event - event.setCancelled(true); // The custom event should only be cancelled when muted or similar + if (ComponentManager.isEnabled(FormatterComponent.class)) //If not enabled, then let the other plugins deal with the message + event.setCancelled(true); // The custom event should only be cancelled when muted or similar } @EventHandler(priority = EventPriority.HIGHEST) @@ -94,7 +96,7 @@ public class PlayerListener implements Listener { Player player = Bukkit.getPlayer(message.substring(index + 1)); if (player != null && sender instanceof Player) player.sendMessage("§b" + ((Player) sender).getDisplayName() + " §bis in this world: " //TODO: Move to the Core - + ((Player) sender).getWorld().getName()); + + ((Player) sender).getWorld().getName()); } else if (cmd.equalsIgnoreCase("minecraft:me")) { if (!(sender instanceof Player) || !PluginMain.essentials.getUser((Player) sender).isMuted()) { String msg = message.substring(index + 1); @@ -130,7 +132,7 @@ public class PlayerListener implements Listener { String name = e.getLastToken(); for (Entry nicknamekv : nicknames.entrySet()) { if (nicknamekv.getKey().startsWith(name.toLowerCase())) - e.getTabCompletions().add(PluginMain.essentials.getUser(Bukkit.getPlayer(nicknamekv.getValue())).getNick(true)); //Tabcomplete with the correct case + e.getTabCompletions().add(PluginMain.essentials.getUser(Bukkit.getPlayer(nicknamekv.getValue())).getNick(true)); //Tabcomplete with the correct case } } @@ -153,7 +155,7 @@ public class PlayerListener implements Listener { if (flair.length() > 0) e.addInfo("/r/TheButton flair: " + flair); } - e.addInfo(String.format("Respect: %.2f", cp.getF())); + e.addInfo(String.format("Respect: %.2f", cp.getF())); } catch (Exception ex) { TBMCCoreAPI.SendException("Error while providing chat info for player " + e.getPlayer().getFileName(), ex); } @@ -165,25 +167,20 @@ public class PlayerListener implements Listener { if (e.isCancelled()) return; HistoryCommand.addChatMessage(e.getCm(), e.getChannel()); - e.setCancelled(ChatProcessing.ProcessChat(e)); + e.setCancelled(FormatterComponent.handleChat(e)); } catch (NoClassDefFoundError | Exception ex) { // Weird things can happen - val str = "§c!§r[" + e.getChannel().DisplayName().get() + "] <" - + ThorpeUtils.getDisplayName(e.getSender()) + "> " + e.getMessage(); - for (Player p : Bukkit.getOnlinePlayers()) - if (e.shouldSendTo(p)) - p.sendMessage(str); - Bukkit.getConsoleSender().sendMessage(str); + ChatUtils.sendChatMessage(e, s -> "§c!§r" + s); TBMCCoreAPI.SendException("An error occured while processing a chat message!", ex); } } @EventHandler public void onNickChange(NickChangeEvent e) { - String nick = e.getValue(); - if (nick == null) - nicknames.inverse().remove(e.getAffected().getBase().getUniqueId()); - else - nicknames.inverse().forcePut(e.getAffected().getBase().getUniqueId(), ChatColor.stripColor(nick).toLowerCase()); + String nick = e.getValue(); + if (nick == null) + nicknames.inverse().remove(e.getAffected().getBase().getUniqueId()); + else + nicknames.inverse().forcePut(e.getAffected().getBase().getUniqueId(), ChatColor.stripColor(nick).toLowerCase()); Bukkit.getScheduler().runTaskLater(PluginMain.Instance, () -> { TownColorComponent.updatePlayerColors(e.getAffected().getBase()); //Won't fire this event again diff --git a/src/test/java/buttondevteam/chat/ChatFormatIT.java b/src/test/java/buttondevteam/chat/components/formatter/ChatFormatIT.java similarity index 86% rename from src/test/java/buttondevteam/chat/ChatFormatIT.java rename to src/test/java/buttondevteam/chat/components/formatter/ChatFormatIT.java index d1ea14f..438155d 100644 --- a/src/test/java/buttondevteam/chat/ChatFormatIT.java +++ b/src/test/java/buttondevteam/chat/components/formatter/ChatFormatIT.java @@ -1,17 +1,21 @@ -package buttondevteam.chat; +package buttondevteam.chat.components.formatter; +import buttondevteam.chat.ChatUtils; +import buttondevteam.chat.ObjectTestRunner; import buttondevteam.chat.ObjectTestRunner.Objects; +import buttondevteam.chat.PluginMain; import buttondevteam.chat.commands.ucmds.admin.DebugCommand; -import buttondevteam.chat.formatting.ChatFormatter; -import buttondevteam.chat.formatting.TellrawEvent; -import buttondevteam.chat.formatting.TellrawEvent.ClickAction; -import buttondevteam.chat.formatting.TellrawEvent.HoverAction; -import buttondevteam.chat.formatting.TellrawPart; +import buttondevteam.chat.components.formatter.formatting.ChatFormatter; +import buttondevteam.chat.components.formatter.formatting.TellrawEvent; +import buttondevteam.chat.components.formatter.formatting.TellrawEvent.ClickAction; +import buttondevteam.chat.components.formatter.formatting.TellrawEvent.HoverAction; +import buttondevteam.chat.components.formatter.formatting.TellrawPart; import buttondevteam.core.TestPrepare; import buttondevteam.core.component.channel.Channel; import buttondevteam.lib.chat.Color; import net.milkbowl.vault.permission.Permission; import org.bukkit.command.CommandSender; +import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; @@ -19,8 +23,6 @@ import org.mockito.Mockito; import java.util.ArrayList; import java.util.List; -import static org.junit.Assert.assertEquals; - @RunWith(ObjectTestRunner.class) public class ChatFormatIT { @Objects @@ -92,15 +94,15 @@ public class ChatFormatIT { @Test public void testMessage() { ArrayList cfs = ChatProcessing.addFormatters(Color.White, p -> true); - final String chid = ChatProcessing.getChannelID(Channel.GlobalChat, ChatProcessing.MCORIGIN); - final TellrawPart tp = ChatProcessing.createTellraw(sender, message, null, null, null, chid, ChatProcessing.MCORIGIN); - ChatFormatter.Combine(cfs, message, tp); + final String chid = ChatProcessing.getChannelID(Channel.GlobalChat, ChatUtils.MCORIGIN); + final TellrawPart tp = ChatProcessing.createTellraw(sender, message, null, null, null, chid, ChatUtils.MCORIGIN); + ChatFormatter.Combine(cfs, message, tp, null); System.out.println("Testing: " + message); // System.out.println(ChatProcessing.toJson(tp)); - final TellrawPart expectedtp = ChatProcessing.createTellraw(sender, message, null, null, null, chid, ChatProcessing.MCORIGIN); + final TellrawPart expectedtp = ChatProcessing.createTellraw(sender, message, null, null, null, chid, ChatUtils.MCORIGIN); // System.out.println("Raw: " + ChatProcessing.toJson(expectedtp)); for (TellrawPart extra : extras) expectedtp.addExtra(extra); - assertEquals(ChatProcessing.toJson(expectedtp), ChatProcessing.toJson(tp)); + Assert.assertEquals(ChatProcessing.toJson(expectedtp), ChatProcessing.toJson(tp)); } }