From 437d9e8f985435ceb8dd9f7bc68626d072996be7 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 5 Aug 2017 01:23:37 +0200 Subject: [PATCH] Removed RC messages, fixed URLs probably #63 Added URL test Smaller refactors --- .../buttondevteam/chat/ChatProcessing.java | 78 ++++++++++--------- .../java/buttondevteam/chat/PluginMain.java | 2 +- .../chat/formatting/ChatFormatter.java | 20 ++--- .../chat/formatting/TellrawEvent.java | 19 ++--- .../buttondevteam/chat/ChatFormatTest.java | 12 ++- 5 files changed, 71 insertions(+), 60 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index f67e427..4b40d66 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -52,8 +52,7 @@ public class ChatProcessing { new ChatFormatterBuilder().setRegex(UNDERLINED_PATTERN).setUnderlined(true).setRemoveCharCount((short) 1) .setRange(true).build(), ESCAPE_FORMATTER, - new ChatFormatterBuilder().setRegex(URL_PATTERN).setUnderlined(true).setOpenlink("$1").setRange(true) - .build(), + new ChatFormatterBuilder().setRegex(URL_PATTERN).setUnderlined(true).setOpenlink("$1").build(), new ChatFormatterBuilder().setRegex(NULL_MENTION_PATTERN).setColor(Color.DarkRed).build(), // Properly added a bug as a feature new ChatFormatterBuilder().setRegex(CONSOLE_PING_PATTERN).setColor(Color.Aqua) .setOnmatch((match, builder) -> @@ -162,53 +161,60 @@ public class ChatProcessing { final String channelidentifier) { TellrawPart json = new TellrawPart(""); 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("[C]") + .setHoverEvent(TellrawEvent.create(TellrawEvent.HoverAction.SHOW_TEXT, "Chat only"))); } json.addExtra( new TellrawPart(channelidentifier) .setHoverEvent( - TellrawEvent.create(TellrawEvent.HoverAC, TellrawEvent.HoverAction.SHOW_TEXT, + TellrawEvent.create(TellrawEvent.HoverAction.SHOW_TEXT, new TellrawPart((sender instanceof IDiscordSender ? "From Discord\n" : "") + "Copy message").setColor(Color.Blue))) - .setClickEvent(TellrawEvent.create(TellrawEvent.ClickAC, - TellrawEvent.ClickAction.SUGGEST_COMMAND, message))); + .setClickEvent(TellrawEvent.create(TellrawEvent.ClickAction.SUGGEST_COMMAND, message))); 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("Flair: %s", - (mp != null ? mp.GetFormattedFlair() : "-")))) - .addExtra(new TellrawPart( - String.format("\nPlayername: %s\n", + (player != null ? player.getDisplayName() + : sender.getName())) + .setHoverEvent( + TellrawEvent + .create(TellrawEvent.HoverAction.SHOW_TEXT, + new TellrawPart("") + .addExtra(new TellrawPart(String.format( + "Flair: %s", + (mp != null ? mp.GetFormattedFlair() + : "-")))) + .addExtra(new TellrawPart(String.format( + "\nPlayername: %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().get() + .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().get() / (double) mp.FDeaths().get()) - : "Infinite"), - (mp != null && mp.UserName().get() != null - && !mp.UserName().get().isEmpty() - ? "\nUserName: " - + mp.UserName().get() - : ""), - (mp != null && mp.PlayerName().get() - .equals("\nAlpha_Bacca44") - ? "\nDeaths: " - + PlayerListener.AlphaDeaths - : "")))) - .addExtra(new TellrawPart("\nFor more, do /u info " - + sender.getName()))))); + : "Infinite"), + (mp != null + && mp.UserName().get() != null + && !mp.UserName().get() + .isEmpty() + ? "\nUserName: " + + mp.UserName() + .get() + : ""), + (mp != null && mp.PlayerName().get() + .equals("\nAlpha_Bacca44") + ? "\nDeaths: " + + PlayerListener.AlphaDeaths + : "")))) + .addExtra(new TellrawPart( + "\nFor more, do /u info " + + sender.getName()))))); json.addExtra(new TellrawPart("> ")); return json; } diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index 5d0c496..7780e2c 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -98,7 +98,7 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. setupEconomy(); setupPermissions(); - new Thread(() -> FlairGetterThreadMethod()).start(); + new Thread(this::FlairGetterThreadMethod).start(); new Thread(new AnnouncerThread()).start(); } diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index 68ff611..e655e1f 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -177,23 +177,23 @@ public final class ChatFormatter { } thirdFormattedSection.Start = origend + 1; thirdFormattedSection.End = origend2; - System.out.println("RC start"); + DebugCommand.SendDebugMessage("RC start"); for (short ii = 0; ii < 3; ii += 2) // Only check first and third section for (short iii = 0; iii < 2; iii++) { final int startorend = iii == 0 ? section.Start : section.End; if (rc[ii][iii][0] <= startorend && rc[ii][iii][1] >= startorend) { final String startorendText = iii == 0 ? "Start" : "End"; - System.out.println("rc[" + ii + "][" + iii + "][0] <= section." + startorendText + " && rc[" - + ii + "][" + iii + "][1] >= section." + startorendText); - System.out.println(rc[ii][iii][0] + " <= " + startorend + " && " + rc[ii][iii][1] + " >= " - + startorend); + DebugCommand.SendDebugMessage("rc[" + ii + "][" + iii + "][0] <= section." + startorendText + + " && rc[" + ii + "][" + iii + "][1] >= section." + startorendText); + DebugCommand.SendDebugMessage(rc[ii][iii][0] + " <= " + startorend + " && " + rc[ii][iii][1] + + " >= " + startorend); rc[1][iii] = new int[] { startorend, rc[ii][iii][1] }; rc[ii][iii][1] = startorend - 1; - System.out.println("rc[1][" + iii + "]: " + rc[1][iii][0] + " " + rc[1][iii][1]); - System.out.println("rc[" + ii + "][" + iii + "][1]: " + rc[ii][iii][1]); + DebugCommand.SendDebugMessage("rc[1][" + iii + "]: " + rc[1][iii][0] + " " + rc[1][iii][1]); + DebugCommand.SendDebugMessage("rc[" + ii + "][" + iii + "][1]: " + rc[ii][iii][1]); } } - System.out.println("RC done"); + DebugCommand.SendDebugMessage("RC done"); Function getRemCharStart = arr -> arr[1] - arr[0] < 0 ? 0 : arr[1] - arr[0]; firstSection.RemCharFromStart = (short) (int) getRemCharStart.apply(rc[0][0]); firstSection.RemCharFromEnd = (short) (int) getRemCharStart.apply(rc[0][1]); @@ -296,9 +296,9 @@ public final class ChatFormatter { newtp.setStrikethrough(strikethrough); newtp.setObfuscated(obfuscated); if (openlink != null && openlink.length() > 0) { - newtp.setClickEvent(TellrawEvent.create(TellrawEvent.ClickAC, TellrawEvent.ClickAction.OPEN_URL, + newtp.setClickEvent(TellrawEvent.create(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, + .setHoverEvent(TellrawEvent.create(TellrawEvent.HoverAction.SHOW_TEXT, new TellrawPart("Click to open").setColor(Color.Blue))); } tp.addExtra(newtp); diff --git a/src/main/java/buttondevteam/chat/formatting/TellrawEvent.java b/src/main/java/buttondevteam/chat/formatting/TellrawEvent.java index 4e18dc0..8f43d63 100644 --- a/src/main/java/buttondevteam/chat/formatting/TellrawEvent.java +++ b/src/main/java/buttondevteam/chat/formatting/TellrawEvent.java @@ -10,27 +10,24 @@ public final class TellrawEvent implements Serial private T action; private Object value; - private TellrawEvent(Class cl, T action, String value) { - this.hoverEvent = HoverAction.class.equals(cl); + private TellrawEvent(T action, String value) { + this.hoverEvent = action instanceof HoverAction; this.action = action; this.value = value; } - private TellrawEvent(Class cl, T action, TellrawPart value) { - this.hoverEvent = HoverAction.class.equals(cl); + private TellrawEvent(T action, TellrawPart value) { + this.hoverEvent = action instanceof HoverAction; this.action = action; this.value = value; } - public static final Class HoverAC = HoverAction.class; - public static final Class ClickAC = ClickAction.class; - - public static TellrawEvent create(Class cl, V action, String value) { - return new TellrawEvent<>(cl, action, value); + public static TellrawEvent create(V action, String value) { + return new TellrawEvent<>(action, value); } - public static TellrawEvent create(Class cl, V action, TellrawPart value) { - return new TellrawEvent<>(cl, action, value); + public static TellrawEvent create(V action, TellrawPart value) { + return new TellrawEvent<>(action, value); } public boolean isHoverEvent() { diff --git a/src/test/java/buttondevteam/chat/ChatFormatTest.java b/src/test/java/buttondevteam/chat/ChatFormatTest.java index d0c2c23..9e97bf0 100644 --- a/src/test/java/buttondevteam/chat/ChatFormatTest.java +++ b/src/test/java/buttondevteam/chat/ChatFormatTest.java @@ -11,6 +11,9 @@ import org.mockito.Mockito; import buttondevteam.chat.ObjectTestRunner.Objects; 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.core.TestPrepare; import buttondevteam.lib.chat.Channel; @@ -38,6 +41,11 @@ public class ChatFormatTest extends TestCase { list.add(new ChatFormatTest(sender, "¯\\\\\\_(ツ)\\_/¯", new TellrawPart("¯").setColor(Color.White), new TellrawPart("\\").setColor(Color.White), new TellrawPart("_(ツ)").setColor(Color.White), new TellrawPart("_/¯").setColor(Color.White))); + list.add(new ChatFormatTest(sender, "https://google.hu/", + new TellrawPart("https://google.hu/").setColor(Color.White).setUnderlined(true) + .setHoverEvent(TellrawEvent.create(HoverAction.SHOW_TEXT, + new TellrawPart("Click to open").setColor(Color.Blue))) + .setClickEvent(TellrawEvent.create(ClickAction.OPEN_URL, "https://google.hu/")))); return list; } @@ -46,10 +54,10 @@ public class ChatFormatTest extends TestCase { private final String message; private final TellrawPart[] extras; - public ChatFormatTest(CommandSender sender, String message, TellrawPart... extras) { + public ChatFormatTest(CommandSender sender, String message, TellrawPart... expectedextras) { this.sender = sender; this.message = message; - this.extras = extras; + this.extras = expectedextras; } @Test