From 91c22e650b6339a64075b94828f8ccb978f81a35 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 25 Aug 2017 00:56:47 +0200 Subject: [PATCH] Fixed ann cmds, chatformat Lombok, testing fixing Fixed announce commands Using Lombok for ChatFormat In the process of fixing the tests Plus #52, #53 --- pom.xml | 4 + .../buttondevteam/chat/ChatProcessing.java | 70 ++++----- .../chat/commands/ucmds/IgnoreCommand.java | 3 +- .../commands/ucmds/announce/AddCommand.java | 2 +- .../commands/ucmds/announce/EditCommand.java | 8 +- .../ucmds/announce/SetTimeCommand.java | 2 +- .../chat/formatting/ChatFormatter.java | 78 ++++----- .../chat/formatting/ChatFormatterBuilder.java | 148 ------------------ 8 files changed, 88 insertions(+), 227 deletions(-) delete mode 100644 src/main/java/buttondevteam/chat/formatting/ChatFormatterBuilder.java diff --git a/pom.xml b/pom.xml index 58c30ba..bed1af4 100644 --- a/pom.xml +++ b/pom.xml @@ -92,12 +92,16 @@ integration-test verify + test true + buttondevteam diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 7a5f232..6001f54 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -40,37 +40,38 @@ public class ChatProcessing { private static final Pattern UNDERLINED_PATTERN = Pattern.compile("\\_"); private static final Pattern ITALIC_PATTERN = Pattern.compile("\\*"); private static final Pattern BOLD_PATTERN = Pattern.compile("\\*\\*"); + private static final Pattern CODE_PATTERN = Pattern.compile("`"); + private static final Pattern MASKED_LINK_PATTERN = Pattern.compile("\\[([^\\[\\]])\\]\\(([^\\(\\)])\\)"); private static final Color[] RainbowPresserColors = new Color[] { Color.Red, Color.Gold, Color.Yellow, Color.Green, Color.Blue, Color.DarkPurple }; private static boolean pingedconsole = false; - public static final ChatFormatter ESCAPE_FORMATTER = new ChatFormatterBuilder().setRegex(ESCAPE_PATTERN).build(); + public static final ChatFormatter ESCAPE_FORMATTER = ChatFormatter.builder().regex(ESCAPE_PATTERN).build(); private static ArrayList commonFormatters = Lists.newArrayList( - new ChatFormatterBuilder().setRegex(BOLD_PATTERN).setBold(true).setRemoveCharCount((short) 2).setRange(true) - .setPriority(Priority.High).build(), - new ChatFormatterBuilder().setRegex(ITALIC_PATTERN).setItalic(true).setRemoveCharCount((short) 1) - .setRange(true).build(), - new ChatFormatterBuilder().setRegex(UNDERLINED_PATTERN).setUnderlined(true).setRemoveCharCount((short) 1) - .setRange(true).build(), - ESCAPE_FORMATTER, - 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) -> + ChatFormatter.builder().regex(BOLD_PATTERN).bold(true).removeCharCount((short) 2).range(true) + .priority(Priority.High).build(), + ChatFormatter.builder().regex(ITALIC_PATTERN).italic(true).removeCharCount((short) 1).range(true).build(), + ChatFormatter.builder().regex(UNDERLINED_PATTERN).underlined(true).removeCharCount((short) 1).range(true) + .build(), + ESCAPE_FORMATTER, ChatFormatter.builder().regex(URL_PATTERN).underlined(true).openlink("$1").build(), + ChatFormatter.builder().regex(NULL_MENTION_PATTERN).color(Color.DarkRed).build(), // Properly added a bug as a feature + ChatFormatter.builder().regex(CONSOLE_PING_PATTERN).color(Color.Aqua).onmatch((match, builder) -> { + if (!pingedconsole) { + System.out.print("\007"); + pingedconsole = true; // Will set it to false in ProcessChat + } + return match; + }).priority(Priority.High).build(), - { - if (!pingedconsole) { - System.out.print("\007"); - pingedconsole = true; // Will set it to false in ProcessChat - } - return match; - }).setPriority(Priority.High).build(), - - new ChatFormatterBuilder().setRegex(HASHTAG_PATTERN).setColor(Color.Blue) - .setOpenlink("https://twitter.com/hashtag/$1").setPriority(Priority.High).build(), - new ChatFormatterBuilder().setRegex(CYAN_PATTERN).setColor(Color.Aqua).build() // #55 - ); + ChatFormatter.builder().regex(HASHTAG_PATTERN).color(Color.Blue).openlink("https://twitter.com/hashtag/$1") + .priority(Priority.High).build(), + ChatFormatter.builder().regex(CYAN_PATTERN).color(Color.Aqua).build(), // #55 + ChatFormatter.builder().regex(CODE_PATTERN).color(Color.DarkGray).removeCharCount((short) 1).range(true) + .build(), + ChatFormatter.builder().regex(MASKED_LINK_PATTERN).underlined(true).onmatch((match, builder) -> { + return match; // TODO! + }).build()); private static Gson gson = new GsonBuilder() .registerTypeHierarchyAdapter(TellrawSerializableEnum.class, new TellrawSerializer.TwEnum()) .registerTypeHierarchyAdapter(Collection.class, new TellrawSerializer.TwCollection()) @@ -108,9 +109,8 @@ public class ChatProcessing { ArrayList formatters = addFormatters(colormode); if (colormode == channel.color && mp != null && mp.RainbowPresserColorMode) { // Only overwrite channel color final AtomicInteger rpc = new AtomicInteger(0); - formatters.add(new ChatFormatterBuilder().setColor(colormode).setOnmatch((match, builder) -> { - builder.setColor( - RainbowPresserColors[rpc.getAndUpdate(i -> ++i < RainbowPresserColors.length ? i : 0)]); + formatters.add(ChatFormatter.builder().color(colormode).onmatch((match, cf) -> { + cf.setColor(RainbowPresserColors[rpc.getAndUpdate(i -> ++i < RainbowPresserColors.length ? i : 0)]); return match; }).build()); } @@ -245,8 +245,8 @@ public class ChatProcessing { @SuppressWarnings("unchecked") ArrayList formatters = (ArrayList) commonFormatters.clone(); - formatters.add(new ChatFormatterBuilder().setRegex(ENTIRE_MESSAGE_PATTERN).setColor(colormode) - .setPriority(Priority.Low).build()); + formatters.add( + ChatFormatter.builder().regex(ENTIRE_MESSAGE_PATTERN).color(colormode).priority(Priority.Low).build()); if (Bukkit.getOnlinePlayers().size() > 0) { StringBuilder namesb = new StringBuilder("(?i)("); @@ -271,8 +271,8 @@ public class ChatProcessing { } nicksb.append(")"); - formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(namesb.toString())).setColor(Color.Aqua) - .setOnmatch((match, builder) -> { + formatters.add(ChatFormatter.builder().regex(Pattern.compile(namesb.toString())).color(Color.Aqua) + .onmatch((match, builder) -> { Player p = Bukkit.getPlayer(match); if (p == null) { PluginMain.Instance.getLogger() @@ -287,11 +287,11 @@ public class ChatProcessing { (float) PlayerListener.NotificationPitch); String color = String.format("§%x", (mpp.GetFlairColor() == 0x00 ? 0xb : mpp.GetFlairColor())); return color + p.getName() + "§r"; - }).setPriority(Priority.High).build()); + }).priority(Priority.High).build()); if (addNickFormatter) - formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(nicksb.toString())) - .setColor(Color.Aqua).setOnmatch((match, builder) -> { + formatters.add(ChatFormatter.builder().regex((Pattern.compile(nicksb.toString()))).color(Color.Aqua) + .onmatch((match, builder) -> { if (PlayerListener.nicknames.containsKey(match)) { Player p = Bukkit.getPlayer(PlayerListener.nicknames.get(match)); if (p == null) { @@ -309,7 +309,7 @@ public class ChatProcessing { Bukkit.getServer().getLogger().warning("Player nicknamed " + match + " not found in nickname map but was reported as online."); return "§c" + match + "§r"; - }).setPriority(Priority.High).build()); + }).priority(Priority.High).build()); } return formatters; } diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/IgnoreCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/IgnoreCommand.java index 9e333db..4ea2973 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/IgnoreCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/IgnoreCommand.java @@ -16,8 +16,7 @@ public final class IgnoreCommand extends UCommandBase { @Override public String[] GetHelpText(String alias) { return new String[] { "§6---- Ignore flair ----", - "Stop the \"write your name in the thread\" message from showing up", - "Use /u ignore if you commented from multiple accounts" }; + "Stop the \"write your name in the thread\" message from showing up" }; } @Override diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/announce/AddCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/announce/AddCommand.java index be359b2..d86fcf2 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/announce/AddCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/announce/AddCommand.java @@ -23,7 +23,7 @@ public class AddCommand extends AnnounceCommandBase { return false; } StringBuilder sb = new StringBuilder(); - for (int i = 2; i < args.length; i++) { + for (int i = 0; i < args.length; i++) { sb.append(args[i]); if (i != args.length - 1) sb.append(" "); diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/announce/EditCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/announce/EditCommand.java index 42b3ada..0bb0c32 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/announce/EditCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/announce/EditCommand.java @@ -21,22 +21,22 @@ public class EditCommand extends AnnounceCommandBase { sender.sendMessage("§cError: This command can only be used from a command block. You can use add and remove, though it's not recommended."); return true; } - if (args.length < 4) { + if (args.length < 1) { return false; } StringBuilder sb1 = new StringBuilder(); - for (int i1 = 3; i1 < args.length; i1++) { + for (int i1 = 1; i1 < args.length; i1++) { sb1.append(args[i1]); if (i1 != args.length - 1) sb1.append(" "); } String finalmessage1 = sb1.toString().replace('&', '§'); - int index = Integer.parseInt(args[2]); + int index = Integer.parseInt(args[0]); if (index > 100) return false; while (PluginMain.AnnounceMessages.size() <= index) PluginMain.AnnounceMessages.add(""); - PluginMain.AnnounceMessages.set(Integer.parseInt(args[2]), + PluginMain.AnnounceMessages.set(Integer.parseInt(args[0]), finalmessage1); sender.sendMessage("Announcement edited."); return true; diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/announce/SetTimeCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/announce/SetTimeCommand.java index fe488d6..4e36a0b 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/announce/SetTimeCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/announce/SetTimeCommand.java @@ -16,7 +16,7 @@ public class SetTimeCommand extends AnnounceCommandBase { @Override public boolean OnCommand(CommandSender sender, String alias, String[] args) { - if (args.length < 3) { + if (args.length < 1) { return false; } try { diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index e655e1f..4924ff5 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -4,12 +4,17 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Predicate; 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.*; +import lombok.Builder; +import lombok.Data; /** * A {@link ChatFormatter} shows what formatting to use based on regular expressions. {@link ChatFormatter#Combine(List, String, TellrawPart)} is used to turn it into a {@link TellrawPart}, combining @@ -19,12 +24,24 @@ import buttondevteam.lib.chat.*; * @author NorbiPeti * */ +@Data +@Builder public final class ChatFormatter { - private ChatFormatterBuilder builder; - - public ChatFormatter(ChatFormatterBuilder builder) { - this.builder = builder; - } + Pattern regex; + boolean italic; + boolean bold; + boolean underlined; + boolean strikethrough; + boolean obfuscated; + Color color; + BiFunction onmatch; + String openlink; + @Builder.Default + Priority priority = Priority.Normal; + @Builder.Default + short removeCharCount = 0; + @Builder.Default + boolean range = false; public static void Combine(List formatters, String str, TellrawPart tp) { /* @@ -33,7 +50,7 @@ public final class ChatFormatter { header("ChatFormatter.Combine begin"); ArrayList sections = new ArrayList(); for (ChatFormatter formatter : formatters) { - Matcher matcher = formatter.builder.regex.matcher(str); + Matcher matcher = formatter.regex.matcher(str); while (matcher.find()) { DebugCommand.SendDebugMessage("Found match from " + matcher.start() + " to " + (matcher.end() - 1)); DebugCommand.SendDebugMessage("With formatter:" + formatter); @@ -44,13 +61,13 @@ public final class ChatFormatter { if (groups.size() > 0) DebugCommand.SendDebugMessage("First group: " + groups.get(0)); FormattedSection section = new FormattedSection(formatter, matcher.start(), matcher.end() - 1, groups, - formatter.builder.removecharcount, formatter.builder.removecharcount, formatter.builder.range); + formatter.removeCharCount, formatter.removeCharCount, formatter.range); sections.add(section); } } sections.sort((s1, s2) -> s1.Start == s2.Start - ? s1.End == s2.End ? Integer.compare(s2.Formatters.get(0).builder.priority.GetValue(), - s1.Formatters.get(0).builder.priority.GetValue()) : Integer.compare(s2.End, s1.End) + ? s1.End == s2.End ? Integer.compare(s2.Formatters.get(0).priority.GetValue(), + s1.Formatters.get(0).priority.GetValue()) : Integer.compare(s2.End, s1.End) : Integer.compare(s1.Start, s2.Start)); header("Range section conversion"); @@ -243,11 +260,10 @@ public final class ChatFormatter { if (found) { i = 1; found = false; - sections.sort((s1, - s2) -> s1.Start == s2.Start ? s1.End == s2.End - ? Integer.compare(s2.Formatters.get(0).builder.priority.GetValue(), - s1.Formatters.get(0).builder.priority.GetValue()) - : Integer.compare(s2.End, s1.End) : Integer.compare(s1.Start, s2.Start)); + sections.sort((s1, s2) -> s1.Start == s2.Start + ? s1.End == s2.End ? Integer.compare(s2.Formatters.get(0).priority.GetValue(), + s1.Formatters.get(0).priority.GetValue()) : Integer.compare(s2.End, s1.End) + : Integer.compare(s1.Start, s2.Start)); } else cont = false; } @@ -266,25 +282,25 @@ public final class ChatFormatter { Color color = null; boolean bold = false, italic = false, underlined = false, strikethrough = false, obfuscated = false; String openlink = null; - section.Formatters.sort((cf2, cf1) -> cf1.builder.priority.compareTo(cf2.builder.priority)); + section.Formatters.sort((cf2, cf1) -> cf1.priority.compareTo(cf2.priority)); for (ChatFormatter formatter : section.Formatters) { DebugCommand.SendDebugMessage("Applying formatter: " + formatter); - if (formatter.builder.onmatch != null) - originaltext = formatter.builder.onmatch.apply(originaltext, formatter.builder); - if (formatter.builder.color != null) - color = formatter.builder.color; - if (formatter.builder.bold) + if (formatter.onmatch != null) + originaltext = formatter.onmatch.apply(originaltext, formatter); + if (formatter.color != null) + color = formatter.color; + if (formatter.bold) bold = true; - if (formatter.builder.italic) + if (formatter.italic) italic = true; - if (formatter.builder.underlined) + if (formatter.underlined) underlined = true; - if (formatter.builder.strikethrough) + if (formatter.strikethrough) strikethrough = true; - if (formatter.builder.obfuscated) + if (formatter.obfuscated) obfuscated = true; - if (formatter.builder.openlink != null) - openlink = formatter.builder.openlink; + if (formatter.openlink != null) + openlink = formatter.openlink; } TellrawPart newtp = new TellrawPart(""); newtp.setText(originaltext); @@ -306,16 +322,6 @@ public final class ChatFormatter { header("ChatFormatter.Combine done"); } - @Override - public String toString() { - return new StringBuilder("F(").append(builder.color).append(", ") - .append((builder.bold ? "bold" : "") + (builder.italic ? "italic" : "") - + (builder.underlined ? "underlined" : "") + (builder.strikethrough ? "strikethrough" : "") - + (builder.obfuscated ? "obfuscated" : "")) - .append(", ").append(builder.openlink).append(", ").append(builder.priority).append(", ") - .append(builder.regex).append(")").toString(); // TODO: Lombok - } - /** * * @param str diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatterBuilder.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatterBuilder.java deleted file mode 100644 index 4772180..0000000 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatterBuilder.java +++ /dev/null @@ -1,148 +0,0 @@ -package buttondevteam.chat.formatting; - -import java.io.Serializable; -import java.util.function.BiFunction; -import java.util.regex.Pattern; - -import buttondevteam.lib.chat.*; -import lombok.SneakyThrows; - -public class ChatFormatterBuilder implements Serializable { - private static final long serialVersionUID = -6115913400749778686L; - Pattern regex; - boolean italic; - boolean bold; - boolean underlined; - boolean strikethrough; - boolean obfuscated; - Color color; - BiFunction onmatch; - String openlink; - Priority priority = Priority.Normal; - short removecharcount = 0; - boolean range = false; - - /** - * The returned object is backed by this builder. All changes made to this object affets the returned one. - */ - @SneakyThrows - public ChatFormatter build() { - return new ChatFormatter(this); - } - - public Pattern getRegex() { - return regex; - } - - public ChatFormatterBuilder setRegex(Pattern regex) { - this.regex = regex; - return this; - } - - public boolean isItalic() { - return italic; - } - - public ChatFormatterBuilder setItalic(boolean italic) { - this.italic = italic; - return this; - } - - public boolean isBold() { - return bold; - } - - public ChatFormatterBuilder setBold(boolean bold) { - this.bold = bold; - return this; - } - - public boolean isUnderlined() { - return underlined; - } - - public ChatFormatterBuilder setUnderlined(boolean underlined) { - this.underlined = underlined; - return this; - } - - public boolean isStrikethrough() { - return strikethrough; - } - - public ChatFormatterBuilder setStrikethrough(boolean strikethrough) { - this.strikethrough = strikethrough; - return this; - } - - public boolean isObfuscated() { - return obfuscated; - } - - public ChatFormatterBuilder setObfuscated(boolean obfuscated) { - this.obfuscated = obfuscated; - return this; - } - - public Color getColor() { - return color; - } - - public ChatFormatterBuilder setColor(Color color) { - this.color = color; - return this; - } - - public BiFunction getOnmatch() { - return onmatch; - } - - /** - * Making any changes here using the builder will not affect the previous matches with the current design - */ - public ChatFormatterBuilder setOnmatch(BiFunction 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 == null ? Priority.Normal : priority; - return this; - } - - public short getRemoveCharCount() { - return removecharcount; - } - - /** - * 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 boolean isRange() { - return range; - } - - public ChatFormatterBuilder setRange(boolean range) { - this.range = range; - return this; - } -}