From cb911250284e94761149a6585e5e030e84c06d58 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 24 Sep 2016 13:18:09 +0200 Subject: [PATCH] Started fixing #46 --- .../buttondevteam/chat/ChatProcessing.java | 17 ++++------ .../java/buttondevteam/chat/PluginMain.java | 15 --------- .../chat/formatting/ChatFormatter.java | 24 ++++++++------ .../chat/formatting/ChatFormatterBuilder.java | 32 +++++++++++++++---- 4 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 2561282..a8ada45 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -27,14 +27,10 @@ import buttondevteam.chat.formatting.TellrawPart; import buttondevteam.chat.formatting.TellrawSerializableEnum; import buttondevteam.chat.formatting.TellrawSerializer; import buttondevteam.chat.formatting.ChatFormatter.Color; -import buttondevteam.chat.formatting.ChatFormatter.Format; import buttondevteam.chat.formatting.ChatFormatter.Priority; -import buttondevteam.chat.formatting.TellrawEvent.ClickAction; -import buttondevteam.chat.formatting.TellrawEvent.HoverAction; -import buttondevteam.chat.formatting.TellrawSerializer.TwCollection; -import buttondevteam.chat.formatting.TellrawSerializer.TwEnum; public class ChatProcessing { + private static final Pattern ESCAPE_PATTERN = Pattern.compile("\\\\([\\*\\_\\\\])"); private static final Pattern CONSOLE_PING_PATTERN = Pattern.compile("(?i)" + Pattern.quote("@console")); private static final Pattern HASHTAG_PATTERN = Pattern.compile("#(\\w+)"); private static final Pattern URL_PATTERN = Pattern.compile("(http[\\w:/?=$\\-_.+!*'(),]+)"); @@ -103,17 +99,16 @@ public class ChatProcessing { String suggestmsg = formattedmessage; formatters.add(new ChatFormatterBuilder().setRegex(BOLD_PATTERN).setFormat(ChatFormatter.Format.Bold) - .setReplacewith("$1").build()); + .setRemoveCharCount((short) 2).build()); formatters.add(new ChatFormatterBuilder().setRegex(ITALIC_PATTERN).setFormat(ChatFormatter.Format.Italic) - .setReplacewith("$1").build()); + .setRemoveCharCount((short) 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()); + .setFormat(ChatFormatter.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(ChatFormatter.Format.Underlined) - .setReplacewith("$1").build()); + .setOpenlink("$1").build()); /* * formattedmessage = formattedmessage .replace( item, String.format( * "\",\"color\":\"%s\"},{\"text\":\"%s\",\"color\":\"%s\",\"underlined\":\"true\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"%s\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Open URL\",\"color\":\"blue\"}]}}},{\"text\":\"" diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index 93cdf1b..29804a8 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -28,10 +28,8 @@ import com.palmergames.bukkit.towny.object.TownyUniverse; import java.io.*; import java.lang.String; -import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; -import java.net.URLClassLoader; import java.net.URLConnection; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -340,19 +338,6 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. } } - private void addClassPath(final URL url) throws IOException { - final URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader(); - final Class sysclass = URLClassLoader.class; - try { - final Method method = sysclass.getDeclaredMethod("addURL", new Class[] { URL.class }); - method.setAccessible(true); - method.invoke(sysloader, new Object[] { url }); - } catch (final Throwable t) { - t.printStackTrace(); - throw new IOException("Error adding " + url + " to system classloader"); - } - } - public static Permission permission = null; public static Economy economy = null; public static Chat chat = null; diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index 1f1d5b8..8899eae 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -15,17 +15,19 @@ public final class ChatFormatter { private Function onmatch; private String openlink; private Priority priority; - private String replacewith; + private short removecharcount = 0; + private short removecharpos = -1; public ChatFormatter(Pattern regex, Format format, Color color, Function onmatch, String openlink, - Priority priority, String replacewith) { + Priority priority, short removecharcount, short removecharpos) { this.regex = regex; this.format = format; this.color = color; this.onmatch = onmatch; this.openlink = openlink; this.priority = Priority.High; - this.replacewith = replacewith; + this.removecharcount = removecharcount; + this.removecharpos = removecharpos; } public static void Combine(List formatters, String str, TellrawPart tp) { @@ -130,7 +132,7 @@ public final class ChatFormatter { Color color = null; Format format = null; String openlink = null; - String replacewith = null; + List removecharpositions = new ArrayList(); section.Formatters.sort((cf2, cf1) -> cf1.priority.compareTo(cf2.priority)); for (ChatFormatter formatter : section.Formatters) { DebugCommand.SendDebugMessage("Applying formatter: " + formatter); @@ -142,14 +144,16 @@ public final class ChatFormatter { format = formatter.format; if (formatter.openlink != null) openlink = formatter.openlink; - if (formatter.replacewith != null) - replacewith = formatter.replacewith; + if (formatter.removecharcount != 0) { + removecharpositions.add(section.Start + formatter.removecharcount); // TODO + removecharpositions.add(section.End - formatter.removecharcount); + } + if (formatter.removecharpos != -1) + removecharpositions.add((int) formatter.removecharpos); } TellrawPart newtp = new TellrawPart(""); - if (replacewith != null) // TODO: The ranges may change when formattings are nested, and this can't handle that - newtp.setText(replacewith.replace("$1", section.Matches.get(0))); - else - newtp.setText(originaltext); + StringBuilder origtextsb = new StringBuilder(originaltext); + newtp.setText(origtextsb.toString()); if (color != null) newtp.setColor(color); if (format != null) diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatterBuilder.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatterBuilder.java index f400cb1..5558f04 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatterBuilder.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatterBuilder.java @@ -14,10 +14,11 @@ public class ChatFormatterBuilder { private Function onmatch; private String openlink; private Priority priority; - private String replacewith; + private short removecharcount = 0; + private short removecharpos = -1; public ChatFormatter build() { - return new ChatFormatter(regex, format, color, onmatch, openlink, priority, replacewith); + return new ChatFormatter(regex, format, color, onmatch, openlink, priority, removecharcount, removecharpos); } public Pattern getRegex() { @@ -74,12 +75,31 @@ public class ChatFormatterBuilder { return this; } - public String getReplacewith() { - return replacewith; + public short getRemoveCharCount() { + return removecharcount; } - public ChatFormatterBuilder setReplacewith(String replacewith) { - this.replacewith = replacewith; + /** + * Sets the amount of characters to be removed from the start and the end of the match. + * + * @return This instance + */ + public ChatFormatterBuilder setRemoveCharCount(short removecharcount) { + this.removecharcount = removecharcount; + return this; + } + + public short getRemoveCharPos() { + return removecharpos; + } + + /** + * Sets the position where a single character should be removed. Setting -1 will disable it. + * + * @return This instance + */ + public ChatFormatterBuilder setRemoveCharPos(short removecharpos) { + this.removecharpos = removecharpos; return this; } }