From f39ed23699d855ab07591433b5fe021feff3a973 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 18 Dec 2016 13:33:04 +0100 Subject: [PATCH] Started formatter and escape logic --- .../buttondevteam/chat/ChatProcessing.java | 69 ++++++++++--------- .../chat/formatting/ChatFormatter.java | 15 ++++ 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 3d03c9c..3bdbd5b 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -2,6 +2,7 @@ package buttondevteam.chat; import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.regex.Pattern; import org.bukkit.Bukkit; @@ -46,6 +47,39 @@ public class ChatProcessing { "dark_purple" }; // TODO private static boolean pingedconsole = false; + private static ArrayList commonFormatters = new ArrayList<>(); + + public static final ChatFormatter ESCAPE_FORMATTER = new ChatFormatterBuilder().setRegex(ESCAPE_PATTERN) + .setRemoveCharPos((short) 0).build(); + + private ChatProcessing() { + } + + static { + commonFormatters.add(new ChatFormatterBuilder().setRegex(BOLD_PATTERN).setFormat(Format.Bold) + .setRemoveCharCount((short) 2).build()); + commonFormatters.add(new ChatFormatterBuilder().setRegex(ITALIC_PATTERN).setFormat(Format.Italic) + .setRemoveCharCount((short) 1).build()); + commonFormatters.add(new ChatFormatterBuilder().setRegex(UNDERLINED_PATTERN).setFormat(Format.Underlined) + .setRemoveCharCount((short) 1).build()); + commonFormatters.add(ESCAPE_FORMATTER); + // URLs + Rainbow text + commonFormatters.add(new ChatFormatterBuilder().setRegex(URL_PATTERN).setFormat(Format.Underlined) + .setOpenlink("$1").build()); + commonFormatters.add(new ChatFormatterBuilder().setRegex(NULL_MENTION_PATTERN).setColor(Color.DarkRed).build()); // Properly added a bug as a feature + commonFormatters.add(new ChatFormatterBuilder().setRegex(CONSOLE_PING_PATTERN).setColor(Color.Aqua) + .setOnmatch((String match) -> { + if (!pingedconsole) { + System.out.print("\007"); + pingedconsole = true; // Will set it to false in ProcessChat + } + return match; + }).setPriority(Priority.High).build()); + + commonFormatters.add(new ChatFormatterBuilder().setRegex(HASHTAG_PATTERN).setColor(Color.Blue) + .setOpenlink("https://twitter.com/hashtag/$1").setPriority(Priority.High).build()); + } + // Returns e.setCancelled public static boolean ProcessChat(Channel channel, CommandSender sender, String message) { long processstart = System.nanoTime(); @@ -84,7 +118,8 @@ public class ChatProcessing { } Channel currentchannel = channel; - ArrayList formatters = new ArrayList(); + @SuppressWarnings("unchecked") + ArrayList formatters = (ArrayList) commonFormatters.clone(); Color colormode = currentchannel.color; if (mp != null && mp.OtherColorMode != null) @@ -102,17 +137,6 @@ public class ChatProcessing { String suggestmsg = formattedmessage; - formatters.add(new ChatFormatterBuilder().setRegex(BOLD_PATTERN).setFormat(Format.Bold) - .setRemoveCharCount((short) 2).build()); - formatters.add(new ChatFormatterBuilder().setRegex(ITALIC_PATTERN).setFormat(Format.Italic) - .setRemoveCharCount((short) 1).build()); - formatters.add(new ChatFormatterBuilder().setRegex(UNDERLINED_PATTERN).setFormat(Format.Underlined) - .setRemoveCharCount((short) 1).build()); - formatters.add(new ChatFormatterBuilder().setRegex(ESCAPE_PATTERN).setRemoveCharPos((short) 0).build()); - - // URLs + Rainbow text - formatters.add(new ChatFormatterBuilder().setRegex(URL_PATTERN).setFormat(Format.Underlined).setOpenlink("$1") - .build()); if (PluginMain.GetPlayers().size() > 0) { StringBuilder namesb = new StringBuilder(); namesb.append("(?i)("); @@ -130,8 +154,6 @@ public class ChatProcessing { nicksb.deleteCharAt(nicksb.length() - 1); nicksb.append(")"); - formatters.add(new ChatFormatterBuilder().setRegex(NULL_MENTION_PATTERN).setColor(Color.DarkRed).build()); // Properly added a bug as a feature - formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(namesb.toString())).setColor(Color.Aqua) .setOnmatch((String match) -> { Player p = Bukkit.getPlayer(match); @@ -172,24 +194,7 @@ public class ChatProcessing { }).setPriority(Priority.High).build()); } - pingedconsole = false; - formatters.add(new ChatFormatterBuilder().setRegex(CONSOLE_PING_PATTERN).setColor(Color.Aqua) - .setOnmatch((String match) -> { - if (!pingedconsole) { - System.out.print("\007"); - pingedconsole = true; - } - return match; - }).setPriority(Priority.High).build()); - - formatters.add(new ChatFormatterBuilder().setRegex(HASHTAG_PATTERN).setColor(Color.Blue) - .setOpenlink("https://twitter.com/hashtag/$1").setPriority(Priority.High).build()); - - /* - * if (!hadurls) { if (formattedmessage.matches("(?i).*" + Pattern.quote("@console") + ".*")) { formattedmessage = formattedmessage.replaceAll( "(?i)" + Pattern.quote("@console"), - * "§b@console§r"); formattedmessage = formattedmessage .replaceAll( "(?i)" + Pattern.quote("@console"), String.format( - * "\",\"color\":\"%s\"},{\"text\":\"§b@console§r\",\"color\":\"blue\"},{\"text\":\"" , colormode)); System.out.println("\007"); } } - */ + pingedconsole = false; // Will set it to true onmatch (static constructor) TellrawPart json = new TellrawPart(""); if (mp != null && mp.ChatOnly) { diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index 7e52bc6..37d2196 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -2,11 +2,14 @@ package buttondevteam.chat.formatting; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; +import buttondevteam.chat.ChatProcessing; import buttondevteam.chat.commands.ucmds.admin.DebugCommand; import buttondevteam.lib.chat.*; @@ -62,8 +65,20 @@ public final class ChatFormatter { return Integer.compare(s1.Start, s2.Start); }); List combined = new ArrayList<>(); + Map nextSection = new HashMap<>(); + boolean escaped = false; for (int i = 0; i < sections.size(); i++) { // Set ending to -1 until closed with another 1 long "section" - only do this if IsRange is true + final FormattedSection section = sections.get(i); + if (!section.IsRange) { + escaped = section.Formatters.contains(ChatProcessing.ESCAPE_FORMATTER); + continue; // TODO: Escape \ and check and fix escape logic + } + if (nextSection.containsKey(section.Formatters.get(0)) && !escaped) { + FormattedSection s = nextSection.remove(section.Formatters.get(0)); + s.End = section.Start; + combined.add(s); + } } for (int i = 0; i < sections.size(); i++) { FormattedSection section = sections.get(i);