From 5cdbe6116618f52353b4b73665e219d0347a5de3 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 4 Sep 2016 21:07:09 +0200 Subject: [PATCH] FIXED #39! Mostly. --- .../thebuttonmcchat/ChatFormatter.java | 79 +++++++---------- .../thebuttonmcchat/ChatFormatterBuilder.java | 85 +++++++++++++++++++ .../thebuttonmcchat/ChatProcessing.java | 73 +++++++--------- .../thebuttonmcchat/PlayerListener.java | 3 - .../thebuttonmcchat/PluginMain.java | 1 - .../commands/ucmds/admin/UpdatePlugin.java | 9 -- 6 files changed, 149 insertions(+), 101 deletions(-) create mode 100644 src/buttondevteam/thebuttonmcchat/ChatFormatterBuilder.java diff --git a/src/buttondevteam/thebuttonmcchat/ChatFormatter.java b/src/buttondevteam/thebuttonmcchat/ChatFormatter.java index ccb843b..97fd7f5 100644 --- a/src/buttondevteam/thebuttonmcchat/ChatFormatter.java +++ b/src/buttondevteam/thebuttonmcchat/ChatFormatter.java @@ -15,41 +15,20 @@ public final class ChatFormatter { private Predicate onmatch; private String openlink; private Priority priority; + private String replacewith; private static final String[] RainbowPresserColors = new String[] { "red", "gold", "yellow", "green", "blue", - "dark_purple" }; + "dark_purple" }; // TODO - public ChatFormatter(Pattern regex, Format format) { + public ChatFormatter(Pattern regex, Format format, Color color, Predicate onmatch, String openlink, + Priority priority, String replacewith) { this.regex = regex; this.format = format; - this.priority = Priority.High; - } - - public ChatFormatter(Pattern regex, Format format, String openlink) { - this.regex = regex; - this.format = format; - this.openlink = openlink; - this.priority = Priority.High; - } - - public ChatFormatter(Pattern regex, Color color, String openlink, Priority priority) { - this.regex = regex; - this.color = color; - this.openlink = openlink; - this.priority = priority; - } - - public ChatFormatter(Pattern regex, Color color, Priority priority) { - this.regex = regex; - this.color = color; - this.priority = priority; - } - - public ChatFormatter(Pattern regex, Color color, Predicate onmatch, Priority priority) { - this.regex = regex; this.color = color; this.onmatch = onmatch; - this.priority = priority; + this.openlink = openlink; + this.priority = Priority.High; + this.replacewith = replacewith; } public static String Combine(List formatters, String str) { @@ -67,8 +46,7 @@ public final class ChatFormatter { groups.add(matcher.group(i + 1)); if (groups.size() > 0) DebugCommand.SendDebugMessage("First group: " + groups.get(0)); - FormattedSection section = new FormattedSection(formatter, matcher.start(), matcher.end() - 1, - groups); + FormattedSection section = new FormattedSection(formatter, matcher.start(), matcher.end() - 1, groups); sections.add(section); } } @@ -96,8 +74,8 @@ public final class ChatFormatter { origend = origend2; origend2 = tmp; } - FormattedSection section = new FormattedSection( - sections.get(i - 1).Formatters, sections.get(i).Start, origend, sections.get(i - 1).Matches); + FormattedSection section = new FormattedSection(sections.get(i - 1).Formatters, sections.get(i).Start, + origend, sections.get(i - 1).Matches); section.Formatters.addAll(sections.get(i).Formatters); section.Matches.addAll(sections.get(i).Matches); sections.add(i, section); @@ -124,10 +102,12 @@ public final class ChatFormatter { sections.remove(i); found = true; } - if (i < sections.size() && sections.get(i).End < sections.get(i).Start) { - DebugCommand.SendDebugMessage("Removing section: " + sections.get(i)); - sections.remove(i); - found = true; + for (int j = i - 1; j <= i + 1; j++) { + if (j < sections.size() && sections.get(j).End < sections.get(j).Start) { + DebugCommand.SendDebugMessage("Removing section: " + sections.get(j)); + sections.remove(j); + found = true; + } } i = nextindex - 1; i++; @@ -151,27 +131,31 @@ public final class ChatFormatter { DebugCommand.SendDebugMessage("Applying section: " + section); String originaltext = str.substring(section.Start, section.End + 1); DebugCommand.SendDebugMessage("Originaltext: " + originaltext); - finalstring.append(",{\"text\":\""); // TODO: Bool replace - Replace with match $1? - finalstring.append(originaltext); - finalstring.append("\""); Color color = null; Format format = null; String openlink = null; - Priority priority = null; + String replacewith = null; + section.Formatters.sort((cf1, cf2) -> cf1.priority.compareTo(cf2.priority)); for (ChatFormatter formatter : section.Formatters) { DebugCommand.SendDebugMessage("Applying formatter: " + formatter); if (formatter.onmatch == null || formatter.onmatch.test(originaltext)) { - if (priority == null || priority.GetValue() < formatter.priority.GetValue()) { + if (formatter.color != null) color = formatter.color; - format = formatter.format; // TODO: Don't overwrite - // parts, and work until all - // of them are combined + if (formatter.format != null) + format = formatter.format; + if (formatter.openlink != null) openlink = formatter.openlink; - priority = formatter.priority; - } + if (formatter.replacewith != null) + replacewith = formatter.replacewith; } else DebugCommand.SendDebugMessage("Onmatch predicate returned false."); } + finalstring.append(",{\"text\":\""); + if (replacewith != null) + finalstring.append(replacewith.replace("$1", section.Matches.get(0))); + else + finalstring.append(originaltext); + finalstring.append("\""); if (color != null) { finalstring.append(",\"color\":\""); finalstring.append(color.name); @@ -190,7 +174,8 @@ public final class ChatFormatter { finalstring.append("}"); } DebugCommand.SendDebugMessage("Finalstring: " + finalstring); - return finalstring.toString(); // TODO + return finalstring.toString(); + } @Override diff --git a/src/buttondevteam/thebuttonmcchat/ChatFormatterBuilder.java b/src/buttondevteam/thebuttonmcchat/ChatFormatterBuilder.java new file mode 100644 index 0000000..36926f6 --- /dev/null +++ b/src/buttondevteam/thebuttonmcchat/ChatFormatterBuilder.java @@ -0,0 +1,85 @@ +package buttondevteam.thebuttonmcchat; + +import java.util.function.Predicate; +import java.util.regex.Pattern; + +import buttondevteam.thebuttonmcchat.ChatFormatter.Color; +import buttondevteam.thebuttonmcchat.ChatFormatter.Format; +import buttondevteam.thebuttonmcchat.ChatFormatter.Priority; + +public class ChatFormatterBuilder { + private Pattern regex; + private Format format; + private Color color; + private Predicate onmatch; + private String openlink; + private Priority priority; + private String replacewith; + + public ChatFormatter build() { + return new ChatFormatter(regex, format, color, onmatch, openlink, priority, replacewith); + } + + public Pattern getRegex() { + return regex; + } + + public ChatFormatterBuilder setRegex(Pattern regex) { + this.regex = regex; + return this; + } + + public Format getFormat() { + return format; + } + + public ChatFormatterBuilder setFormat(Format format) { + this.format = format; + return this; + } + + public Color getColor() { + return color; + } + + public ChatFormatterBuilder setColor(Color color) { + this.color = color; + return this; + } + + public Predicate getOnmatch() { + return onmatch; + } + + public ChatFormatterBuilder setOnmatch(Predicate onmatch) { + this.onmatch = onmatch; + return this; + } + + public String getOpenlink() { + return openlink; + } + + public ChatFormatterBuilder setOpenlink(String openlink) { + this.openlink = openlink; + return this; + } + + public Priority getPriority() { + return priority; + } + + public ChatFormatterBuilder setPriority(Priority priority) { + this.priority = priority; + return this; + } + + public String getReplacewith() { + return replacewith; + } + + public ChatFormatterBuilder setReplacewith(String replacewith) { + this.replacewith = replacewith; + return this; + } +} diff --git a/src/buttondevteam/thebuttonmcchat/ChatProcessing.java b/src/buttondevteam/thebuttonmcchat/ChatProcessing.java index 1579ba6..1b50221 100644 --- a/src/buttondevteam/thebuttonmcchat/ChatProcessing.java +++ b/src/buttondevteam/thebuttonmcchat/ChatProcessing.java @@ -69,7 +69,8 @@ public class ChatProcessing { colormode = ChatFormatter.Color.Green; // If greentext, ignore channel or player colors - formatters.add(new ChatFormatter(Pattern.compile(".+"), colormode, "", Priority.Low)); + formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(".+")).setColor(colormode) + .setPriority(Priority.Low).build()); String formattedmessage = message; formattedmessage = formattedmessage.replace("\\", "\\\\"); @@ -78,16 +79,19 @@ public class ChatProcessing { String suggestmsg = formattedmessage; - formatters.add(new ChatFormatter(Pattern.compile("(? { + formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(sb.toString())) + .setColor(ChatFormatter.Color.Aqua).setOnmatch((String match) -> { Player p = Bukkit.getPlayer(match); if (p == null) { PluginMain.Instance.getLogger() @@ -119,10 +123,10 @@ public class ChatProcessing { (float) PlayerListener.NotificationPitch); String color = String.format("§%x", (mpp.GetFlairColor() == 0x00 ? 0xb : mpp.GetFlairColor())); return true; // TODO - }, Priority.High)); + }).setPriority(Priority.High).build()); - formatters - .add(new ChatFormatter(Pattern.compile(sb.toString()), ChatFormatter.Color.Aqua, (String match) -> { + formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(sb.toString())) + .setColor(ChatFormatter.Color.Aqua).setOnmatch((String match) -> { for (String n : PlayerListener.nicknames.keySet()) { String nwithoutformatting = new String(n); int index; @@ -155,32 +159,27 @@ public class ChatProcessing { (mpp.GetFlairColor() == 0x00 ? 0xb : mpp.GetFlairColor())); } return true; // TODO - }, Priority.High)); + }).setPriority(Priority.High).build()); } pingedconsole = false; - formatters.add(new ChatFormatter(Pattern.compile("(?i)" + Pattern.quote("@console")), ChatFormatter.Color.Aqua, - (String match) -> { + formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile("(?i)" + Pattern.quote("@console"))) + .setColor(ChatFormatter.Color.Aqua).setOnmatch((String match) -> { if (!pingedconsole) { System.out.print("\007"); pingedconsole = true; } return true; - }, Priority.High)); + }).setPriority(Priority.High).build()); - formatters.add(new ChatFormatter(Pattern.compile("#(\\w+)"), ChatFormatter.Color.Blue, - "https://twitter.com/hashtag/$1", Priority.High)); + formatters + .add(new ChatFormatterBuilder().setRegex(Pattern.compile("#(\\w+)")).setColor(ChatFormatter.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"); } } + * 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"); } } */ StringBuilder json = new StringBuilder(); @@ -207,24 +206,16 @@ public class ChatProcessing { json.append("{\"text\":\"> \",\"color\":\"white\"}"); /* - * int index = -1; ArrayList list = new ArrayList(); - * 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, + * int index = -1; ArrayList list = new ArrayList(); 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(String.format("{\"text\":\"%s\",\"color\":\"%s\"}]", ChatFormatter.Combine(formatters, formattedmessage), colormode)); */ json.append(ChatFormatter.Combine(formatters, formattedmessage)); json.append("]"); diff --git a/src/buttondevteam/thebuttonmcchat/PlayerListener.java b/src/buttondevteam/thebuttonmcchat/PlayerListener.java index 4cf4d09..46b69a5 100644 --- a/src/buttondevteam/thebuttonmcchat/PlayerListener.java +++ b/src/buttondevteam/thebuttonmcchat/PlayerListener.java @@ -24,7 +24,6 @@ import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerChatTabCompleteEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerPickupItemEvent; import org.bukkit.event.player.PlayerQuitEvent; @@ -35,8 +34,6 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; -import org.bukkit.util.Vector; - import au.com.mineauz.minigames.MinigamePlayer; import au.com.mineauz.minigames.Minigames; import buttondevteam.bucket.core.TBMCPlayer; diff --git a/src/buttondevteam/thebuttonmcchat/PluginMain.java b/src/buttondevteam/thebuttonmcchat/PluginMain.java index 337370b..93f1f4d 100644 --- a/src/buttondevteam/thebuttonmcchat/PluginMain.java +++ b/src/buttondevteam/thebuttonmcchat/PluginMain.java @@ -4,7 +4,6 @@ import net.milkbowl.vault.chat.Chat; import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.permission.Permission; -import org.apache.commons.io.IOUtils; import org.bukkit.Bukkit; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.configuration.InvalidConfigurationException; diff --git a/src/buttondevteam/thebuttonmcchat/commands/ucmds/admin/UpdatePlugin.java b/src/buttondevteam/thebuttonmcchat/commands/ucmds/admin/UpdatePlugin.java index c0ce9c3..c707a7c 100644 --- a/src/buttondevteam/thebuttonmcchat/commands/ucmds/admin/UpdatePlugin.java +++ b/src/buttondevteam/thebuttonmcchat/commands/ucmds/admin/UpdatePlugin.java @@ -1,16 +1,7 @@ package buttondevteam.thebuttonmcchat.commands.ucmds.admin; -import java.io.File; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; - -import org.apache.commons.io.FileUtils; import org.bukkit.command.CommandSender; -import org.bukkit.plugin.Plugin; - import buttondevteam.bucket.core.TBMCCoreAPI; -import buttondevteam.thebuttonmcchat.PluginMain; public class UpdatePlugin extends AdminCommandBase {