From 91c22e650b6339a64075b94828f8ccb978f81a35 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 25 Aug 2017 00:56:47 +0200 Subject: [PATCH 1/3] 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; - } -} From 2ab6a5f4eb41c4a86442ffa754437e8e0f112209 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 27 Aug 2017 23:35:46 +0200 Subject: [PATCH 2/3] Nickname impr., Towny spy, RP channel - Added nickname cache update on nick change for mentions - Added support for Towny spy (again) - Removed RPMode and added RP channel --- .../java/buttondevteam/chat/ChatPlayer.java | 1 - .../buttondevteam/chat/ChatProcessing.java | 17 ++++++--- .../java/buttondevteam/chat/PluginMain.java | 19 ++++++++-- .../chat/commands/OOCCommand.java | 36 ------------------- .../listener/PlayerJoinLeaveListener.java | 2 -- .../chat/listener/PlayerListener.java | 6 ++++ .../java/buttondevteam/chat/ChatFormatIT.java | 2 +- 7 files changed, 36 insertions(+), 47 deletions(-) delete mode 100644 src/main/java/buttondevteam/chat/commands/OOCCommand.java diff --git a/src/main/java/buttondevteam/chat/ChatPlayer.java b/src/main/java/buttondevteam/chat/ChatPlayer.java index 3eb9338..25bc99d 100644 --- a/src/main/java/buttondevteam/chat/ChatPlayer.java +++ b/src/main/java/buttondevteam/chat/ChatPlayer.java @@ -42,7 +42,6 @@ public class ChatPlayer extends TBMCPlayerBase { return data(false); } - public boolean RPMode = true; public Location SavedLocation; public boolean Working; // public int Tables = 10; diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 6001f54..7c5e262 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.concurrent.atomic.AtomicInteger; import java.util.regex.Pattern; @@ -115,7 +116,7 @@ public class ChatProcessing { }).build()); } pingedconsole = false; // Will set it to true onmatch (static constructor) - final String channelidentifier = getChannelID(channel, sender, mp); + final String channelidentifier = getChannelID(channel, sender); TellrawPart json = createTellraw(sender, message, player, mp, channelidentifier); long combinetime = System.nanoTime(); @@ -148,10 +149,18 @@ public class ChatProcessing { score = e.getMCScore(sender); if (score < 0) // Never send messages to score below 0 sender.sendMessage("§cYou don't have permission to send this message or something went wrong"); - else + else { PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, String.format("tellraw @a[score_%s=%d,score_%s_min=%d] %s", channel.ID, score, channel.ID, score, jsonstr)); + if (e.getChannel().ID.equals(PluginMain.TownChat.ID) + || e.getChannel().ID.equals(PluginMain.NationChat.ID)) { + ((List) json.getExtra()).add(0, new TellrawPart("[SPY]")); + jsonstr = toJson(json); + Bukkit.getServer().dispatchCommand(PluginMain.Console, String.format( + "tellraw @a[score_%s=1000,score_%s_min=1000 %s", channel.ID, channel.ID, jsonstr)); + } + } } else PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, String.format("tellraw @a %s", jsonstr)); @@ -235,9 +244,9 @@ public class ChatProcessing { return json; } - static String getChannelID(Channel channel, CommandSender sender, ChatPlayer mp) { + static String getChannelID(Channel channel, CommandSender sender) { final String channelidentifier = ("[" + (sender instanceof IDiscordSender ? "d|" : "") + channel.DisplayName) - + "]" + (mp != null && !mp.RPMode ? "[OOC]" : ""); + + "]"; return channelidentifier; } diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index f54690c..3149890 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -23,7 +23,6 @@ import buttondevteam.lib.chat.Color; import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.chat.Channel.RecipientTestResult; import buttondevteam.lib.player.TBMCPlayerBase; - import com.earth2me.essentials.Essentials; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -63,6 +62,7 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. public static Channel TownChat; public static Channel NationChat; + public static Channel RPChannel; /** *

@@ -93,6 +93,11 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. TownChat = new Channel("§3TC§f", Color.DarkAqua, "tc", s -> checkTownNationChat(s, false))); TBMCChatAPI.RegisterChatChannel( NationChat = new Channel("§6NC§f", Color.Gold, "nc", s -> checkTownNationChat(s, true))); + TBMCChatAPI.RegisterChatChannel(RPChannel = new Channel("§7RP§f", Color.Gray, "rp", Channel.noScoreResult(s -> { + if (s instanceof ConsoleCommandSender) + return true; + return true; // TODO: Allow hiding it + }, "You need to show the RP chat in order to speak in it."))); setupChat(); setupEconomy(); @@ -324,10 +329,18 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. private static RecipientTestResult checkTownNationChat(CommandSender sender, boolean nationchat) { if (!(sender instanceof Player)) return new RecipientTestResult("§cYou are not a player!"); + Resident resident = PluginMain.TU.getResidentMap().get(sender.getName().toLowerCase()); + RecipientTestResult result = checkTownNationChatInternal(sender, nationchat, resident); + if (result.errormessage != null && resident != null && resident.getModes().contains("spy")) // Only use spy if they wouldn't see it + result = new RecipientTestResult(1000); // There won't be more than a thousand towns/nations probably + return result; + } + + private static RecipientTestResult checkTownNationChatInternal(CommandSender sender, boolean nationchat, + Resident resident) { try { - Resident resident = PluginMain.TU.getResidentMap().get(sender.getName().toLowerCase()); if (resident != null && resident.getModes().contains("spy")) - return null; + return new RecipientTestResult(1000); // There won't be more than a thousand towns/nations probably /* * p.sendMessage(String.format("[SPY-%s] - %s: %s", channel.DisplayName, ((Player) sender).getDisplayName(), message)); */ diff --git a/src/main/java/buttondevteam/chat/commands/OOCCommand.java b/src/main/java/buttondevteam/chat/commands/OOCCommand.java deleted file mode 100644 index c85151f..0000000 --- a/src/main/java/buttondevteam/chat/commands/OOCCommand.java +++ /dev/null @@ -1,36 +0,0 @@ -package buttondevteam.chat.commands; - -import org.bukkit.entity.Player; - -import buttondevteam.chat.ChatPlayer; -import buttondevteam.lib.chat.CommandClass; -import buttondevteam.lib.chat.PlayerCommandBase; -import buttondevteam.lib.player.TBMCPlayer; - -@CommandClass(modOnly = false) -public final class OOCCommand extends PlayerCommandBase { - - @Override - public String[] GetHelpText(String alias) { - return new String[] { "§6---- Out-of-character message ----", - "This command will put a [OCC] tag before your message indicating that you are talking out of character", - "Usage: /" + alias + " " }; - } - - @Override - public boolean OnCommand(Player player, String alias, String[] args) { - if (args.length == 0) { - return false; - } else { - final ChatPlayer cp = TBMCPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class); - cp.RPMode = false; - String message = ""; - for (String arg : args) - message += arg + " "; - player.chat(message.substring(0, message.length() - 1)); - cp.RPMode = true; - } - return true; - } - -} diff --git a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java index 5e05bee..aa96a2b 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java @@ -86,8 +86,6 @@ public class PlayerJoinLeaveListener implements Listener { nwithoutformatting = p.getName(); PlayerListener.nicknames.put(nwithoutformatting, p.getUniqueId()); - cp.RPMode = true; - cp.FlairUpdate(); if (cp.ChatOnly || p.getGameMode().equals(GameMode.SPECTATOR)) { diff --git a/src/main/java/buttondevteam/chat/listener/PlayerListener.java b/src/main/java/buttondevteam/chat/listener/PlayerListener.java index 521eb54..e218c34 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerListener.java @@ -21,6 +21,7 @@ import buttondevteam.lib.chat.ChatChannelRegisterEvent; import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayerGetInfoEvent; +import net.ess3.api.events.NickChangeEvent; import buttondevteam.lib.player.ChromaGamerBase.InfoTarget; import com.google.common.collect.BiMap; @@ -292,4 +293,9 @@ public class PlayerListener implements Listener { if (e.getChannel().filteranderrormsg != null && PluginMain.SB.getObjective(e.getChannel().ID) == null) // Not global chat and doesn't exist yet PluginMain.SB.registerNewObjective(e.getChannel().ID, "dummy"); } + + @EventHandler + public void onNickChange(NickChangeEvent e) { + nicknames.inverse().put(e.getAffected().getBase().getUniqueId(), e.getValue()); + } } diff --git a/src/test/java/buttondevteam/chat/ChatFormatIT.java b/src/test/java/buttondevteam/chat/ChatFormatIT.java index 19c8f59..fc8124b 100644 --- a/src/test/java/buttondevteam/chat/ChatFormatIT.java +++ b/src/test/java/buttondevteam/chat/ChatFormatIT.java @@ -63,7 +63,7 @@ public class ChatFormatIT extends TestCase { @Test public void testMessage() { ArrayList cfs = ChatProcessing.addFormatters(Color.White); - final String chid = ChatProcessing.getChannelID(Channel.GlobalChat, sender, null); + final String chid = ChatProcessing.getChannelID(Channel.GlobalChat, sender); final TellrawPart tp = ChatProcessing.createTellraw(sender, message, null, null, chid); ChatFormatter.Combine(cfs, message, tp); System.out.println("Testing: " + message); From 7174a977283ff474255f3a0f19461721c197682f Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 28 Aug 2017 15:23:06 +0200 Subject: [PATCH 3/3] Fixes, implemented ChatRooms, less code for preprocess --- .../buttondevteam/chat/ChatProcessing.java | 2 +- .../java/buttondevteam/chat/PluginMain.java | 2 - .../chat/listener/PlayerListener.java | 150 ++++++++---------- 3 files changed, 67 insertions(+), 87 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 7c5e262..3d2c2c2 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -158,7 +158,7 @@ public class ChatProcessing { ((List) json.getExtra()).add(0, new TellrawPart("[SPY]")); jsonstr = toJson(json); Bukkit.getServer().dispatchCommand(PluginMain.Console, String.format( - "tellraw @a[score_%s=1000,score_%s_min=1000 %s", channel.ID, channel.ID, jsonstr)); + "tellraw @a[score_%s=1000,score_%s_min=1000] %s", channel.ID, channel.ID, jsonstr)); } } } else diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index 3149890..a381f5d 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -339,8 +339,6 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. private static RecipientTestResult checkTownNationChatInternal(CommandSender sender, boolean nationchat, Resident resident) { try { - if (resident != null && resident.getModes().contains("spy")) - return new RecipientTestResult(1000); // There won't be more than a thousand towns/nations probably /* * p.sendMessage(String.format("[SPY-%s] - %s: %s", channel.DisplayName, ((Player) sender).getDisplayName(), message)); */ diff --git a/src/main/java/buttondevteam/chat/listener/PlayerListener.java b/src/main/java/buttondevteam/chat/listener/PlayerListener.java index e218c34..0e8bb55 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerListener.java @@ -2,9 +2,12 @@ package buttondevteam.chat.listener; import java.util.*; import java.util.Map.Entry; +import java.util.function.Consumer; +import java.util.function.Supplier; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; import org.bukkit.event.*; import org.bukkit.event.entity.PlayerDeathEvent; @@ -18,6 +21,7 @@ import buttondevteam.lib.TBMCChatEvent; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.chat.Channel; import buttondevteam.lib.chat.ChatChannelRegisterEvent; +import buttondevteam.lib.chat.ChatRoom; import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayerGetInfoEvent; @@ -58,43 +62,67 @@ public class PlayerListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST) public void PlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { - if (event.getMessage().length() < 2) - return; - int index = event.getMessage().indexOf(" "); - ChatPlayer mp = TBMCPlayer.getPlayer(event.getPlayer().getUniqueId(), ChatPlayer.class); + if (!event.isCancelled()) + event.setCancelled(onCommandPreprocess(event.getPlayer(), event.getMessage())); + } + + private boolean onCommandPreprocess(CommandSender sender, String message) { + if (message.length() < 2) + return false; + int index = message.indexOf(" "); + ChatPlayer mp; + if (sender instanceof Player) + mp = TBMCPlayer.getPlayer(((Player) sender).getUniqueId(), ChatPlayer.class); + else + mp = null; String cmd = ""; - if (index == -1) { // Only the command is run - cmd = event.getMessage().substring(1); + if (index == -1 && (sender instanceof Player || sender instanceof ConsoleCommandSender)) { // Only the command is run + // ^^ We can only store player or console channels - Directly sending to channels would still work if they had an event + cmd = sender instanceof ConsoleCommandSender ? message : message.substring(1); for (Channel channel : Channel.getChannels()) { if (cmd.equalsIgnoreCase(channel.ID)) { - if (mp.CurrentChannel.equals(channel)) - mp.CurrentChannel = Channel.GlobalChat; - else - mp.CurrentChannel = channel; - event.getPlayer().sendMessage("§6You are now talking in: §b" + mp.CurrentChannel.DisplayName); - event.setCancelled(true); - break; + Supplier getch = () -> sender instanceof Player ? mp.CurrentChannel : ConsoleChannel; + Consumer setch = ch -> { + if (sender instanceof Player) + mp.CurrentChannel = ch; + else + ConsoleChannel = ch; + }; + Channel oldch = getch.get(); + if (oldch instanceof ChatRoom) + ((ChatRoom) oldch).leaveRoom(sender); + if (oldch.equals(channel)) + setch.accept(Channel.GlobalChat); + else { + setch.accept(channel); + if (channel instanceof ChatRoom) + ((ChatRoom) channel).joinRoom(sender); + } + sender.sendMessage("§6You are now talking in: §b" + getch.get().DisplayName); + return true; } } } else { // We have arguments - cmd = event.getMessage().substring(1, index); + cmd = sender instanceof ConsoleCommandSender ? message.substring(0, index) : message.substring(1, index); if (cmd.equalsIgnoreCase("tpahere")) { - Player player = Bukkit.getPlayer(event.getMessage().substring(index + 1)); - if (player != null) - player.sendMessage("§b" + event.getPlayer().getDisplayName() + " §bis in this world: " - + event.getPlayer().getWorld().getName()); + Player player = Bukkit.getPlayer(message.substring(index + 1)); + if (player != null && sender instanceof Player) + player.sendMessage("§b" + ((Player) sender).getDisplayName() + " §bis in this world: " + + ((Player) sender).getWorld().getName()); } else if (cmd.equalsIgnoreCase("minecraft:me")) { - if (!PluginMain.essentials.getUser(event.getPlayer()).isMuted()) { - event.setCancelled(true); - String message = event.getMessage().substring(index + 1); - Bukkit.broadcastMessage(String.format("* %s %s", event.getPlayer().getDisplayName(), message)); + if (!(sender instanceof Player) || !PluginMain.essentials.getUser((Player) sender).isMuted()) { + String msg = message.substring(index + 1); + Bukkit.broadcastMessage(String.format("* %s %s", ((Player) sender).getDisplayName(), msg)); + return true; + } else { + sender.sendMessage("§cCan't use /minecraft:me while muted."); + return true; } } else for (Channel channel : Channel.getChannels()) { if (cmd.equalsIgnoreCase(channel.ID)) { - event.setCancelled(true); - TBMCChatAPI.SendChatMessage(channel, event.getPlayer(), - event.getMessage().substring(index + 1)); + TBMCChatAPI.SendChatMessage(channel, sender, message.substring(index + 1)); + return true; } } // TODO: Target selectors @@ -103,24 +131,24 @@ public class PlayerListener implements Listener { if (cmd.toLowerCase().startsWith("un")) { for (HelpTopic ht : PluginMain.Instance.getServer().getHelpMap().getHelpTopics()) { if (ht.getName().equalsIgnoreCase("/" + cmd)) - return; + return false; } - if (PluginMain.permission.has(event.getPlayer(), "tbmc.admin")) { + if (PluginMain.permission.has(sender, "tbmc.admin")) { String s = cmd.substring(2); Player target = null; - target = Bukkit.getPlayer(event.getMessage().substring(index + 1)); + target = Bukkit.getPlayer(message.substring(index + 1)); if (target == null) { - event.getPlayer().sendMessage("§cError: Player not found. (/un" + s + " )"); - event.setCancelled(true); - } - if (target != null) { - target.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 10 * 20, 5, false, false)); - Bukkit.broadcastMessage( - event.getPlayer().getDisplayName() + " un" + s + "'d " + target.getDisplayName()); - event.setCancelled(true); + sender.sendMessage("§cError: Player not found. (/un" + s + " )"); + return true; } + target.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 10 * 20, 5, false, false)); + Bukkit.broadcastMessage( + (sender instanceof Player ? ((Player) sender).getDisplayName() : sender.getName()) + " un" + s + + "'d " + target.getDisplayName()); + return true; } } + return false; } @EventHandler @@ -204,54 +232,8 @@ public class PlayerListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST) public void onConsoleCommand(ServerCommandEvent event) { - if (event.getCommand().length() < 2) - return; - int index = event.getCommand().indexOf(" "); - String cmd = ""; - if (index == -1) { - cmd = event.getCommand(); - for (Channel channel : Channel.getChannels()) { - if (cmd.equalsIgnoreCase(channel.ID)) { - if (ConsoleChannel.equals(channel)) - ConsoleChannel = Channel.GlobalChat; - else - ConsoleChannel = channel; - event.getSender().sendMessage("§6You are now talking in: §b" + ConsoleChannel.DisplayName); - event.setCommand("dontrunthiscmd"); - break; - } - } - } else { - cmd = event.getCommand().substring(0, index); - for (Channel channel : Channel.getChannels()) { - if (cmd.equalsIgnoreCase(channel.ID)) { - Channel c = ConsoleChannel; - ConsoleChannel = channel; - TBMCChatAPI.SendChatMessage(PlayerListener.ConsoleChannel, Bukkit.getConsoleSender(), - event.getCommand().substring(index + 1)); - ConsoleChannel = c; - event.setCommand("dontrunthiscmd"); - } - } - } - if (cmd.toLowerCase().startsWith("un")) { - for (HelpTopic ht : PluginMain.Instance.getServer().getHelpMap().getHelpTopics()) { - if (ht.getName().equalsIgnoreCase("/" + cmd)) - return; - } - String s = cmd.substring(2); - Player target = null; - target = Bukkit.getPlayer(event.getCommand().substring(index + 1)); - if (target == null) { - event.getSender().sendMessage("§cError: Player not found. (/un" + s + " )"); - event.setCommand("dontrunthiscmd"); - } - if (target != null) { - target.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 10 * 20, 5, false, false)); - Bukkit.broadcastMessage(event.getSender().getName() + " un" + s + "'d " + target.getDisplayName()); - event.setCommand("dontrunthiscmd"); - } - } + if (onCommandPreprocess(event.getSender(), event.getCommand())) + event.setCommand("dontrunthiscmd"); } @EventHandler