From f34deb7abc6b31470f9ffa5942e5cf926c32a2df Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 5 Sep 2016 16:46:38 +0200 Subject: [PATCH] Probably fixed name mentioning --- .../thebuttonmcchat/ChatFormatter.java | 29 +++--- .../thebuttonmcchat/ChatFormatterBuilder.java | 8 +- .../thebuttonmcchat/ChatProcessing.java | 91 +++++++++---------- .../thebuttonmcchat/PlayerListener.java | 36 ++++---- 4 files changed, 83 insertions(+), 81 deletions(-) diff --git a/src/buttondevteam/thebuttonmcchat/ChatFormatter.java b/src/buttondevteam/thebuttonmcchat/ChatFormatter.java index 97fd7f5..b875482 100644 --- a/src/buttondevteam/thebuttonmcchat/ChatFormatter.java +++ b/src/buttondevteam/thebuttonmcchat/ChatFormatter.java @@ -2,7 +2,7 @@ package buttondevteam.thebuttonmcchat; import java.util.ArrayList; import java.util.List; -import java.util.function.Predicate; +import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -12,15 +12,15 @@ public final class ChatFormatter { private Pattern regex; private Format format; private Color color; - private Predicate onmatch; + private Function onmatch; private String openlink; private Priority priority; private String replacewith; private static final String[] RainbowPresserColors = new String[] { "red", "gold", "yellow", "green", "blue", - "dark_purple" }; // TODO + "dark_purple" }; // TODO: Move out to ChatProcessing - public ChatFormatter(Pattern regex, Format format, Color color, Predicate onmatch, String openlink, + public ChatFormatter(Pattern regex, Format format, Color color, Function onmatch, String openlink, Priority priority, String replacewith) { this.regex = regex; this.format = format; @@ -138,17 +138,16 @@ public final class ChatFormatter { 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 (formatter.color != null) - color = formatter.color; - if (formatter.format != null) - format = formatter.format; - if (formatter.openlink != null) - openlink = formatter.openlink; - if (formatter.replacewith != null) - replacewith = formatter.replacewith; - } else - DebugCommand.SendDebugMessage("Onmatch predicate returned false."); + if (formatter.onmatch != null) + originaltext = formatter.onmatch.apply(originaltext); + if (formatter.color != null) + color = formatter.color; + if (formatter.format != null) + format = formatter.format; + if (formatter.openlink != null) + openlink = formatter.openlink; + if (formatter.replacewith != null) + replacewith = formatter.replacewith; } finalstring.append(",{\"text\":\""); if (replacewith != null) diff --git a/src/buttondevteam/thebuttonmcchat/ChatFormatterBuilder.java b/src/buttondevteam/thebuttonmcchat/ChatFormatterBuilder.java index 36926f6..9fdd15d 100644 --- a/src/buttondevteam/thebuttonmcchat/ChatFormatterBuilder.java +++ b/src/buttondevteam/thebuttonmcchat/ChatFormatterBuilder.java @@ -1,6 +1,6 @@ package buttondevteam.thebuttonmcchat; -import java.util.function.Predicate; +import java.util.function.Function; import java.util.regex.Pattern; import buttondevteam.thebuttonmcchat.ChatFormatter.Color; @@ -11,7 +11,7 @@ public class ChatFormatterBuilder { private Pattern regex; private Format format; private Color color; - private Predicate onmatch; + private Function onmatch; private String openlink; private Priority priority; private String replacewith; @@ -47,11 +47,11 @@ public class ChatFormatterBuilder { return this; } - public Predicate getOnmatch() { + public Function getOnmatch() { return onmatch; } - public ChatFormatterBuilder setOnmatch(Predicate onmatch) { + public ChatFormatterBuilder setOnmatch(Function onmatch) { this.onmatch = onmatch; return this; } diff --git a/src/buttondevteam/thebuttonmcchat/ChatProcessing.java b/src/buttondevteam/thebuttonmcchat/ChatProcessing.java index 6c0844b..4871302 100644 --- a/src/buttondevteam/thebuttonmcchat/ChatProcessing.java +++ b/src/buttondevteam/thebuttonmcchat/ChatProcessing.java @@ -19,6 +19,13 @@ import buttondevteam.thebuttonmcchat.ChatFormatter.Priority; import buttondevteam.thebuttonmcchat.commands.UnlolCommand; public class ChatProcessing { + 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:/?=$\\-_.+!*'(),]+)"); + private static final Pattern ENTIRE_MESSAGE_PATTERN = Pattern.compile(".+"); + private static final Pattern UNDERLINED_PATTERN = Pattern.compile("(? 0) { - StringBuilder sb = new StringBuilder(); - sb.append("(?i)("); + StringBuilder namesb = new StringBuilder(); + namesb.append("(?i)("); for (Player p : PluginMain.GetPlayers()) - sb.append(p.getName()).append("|"); - sb.deleteCharAt(sb.length() - 1); - sb.append(")"); + namesb.append(p.getName()).append("|"); + namesb.deleteCharAt(namesb.length() - 1); + namesb.append(")"); + StringBuilder nicksb = new StringBuilder(); + nicksb.append("(?i)("); + for (Player p : PluginMain.GetPlayers()) + nicksb.append(PlayerListener.nicknames.inverse().get(p.getUniqueId())).append("|"); + nicksb.deleteCharAt(nicksb.length() - 1); + nicksb.append(")"); - formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(sb.toString())) + formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(namesb.toString())) .setColor(ChatFormatter.Color.Aqua).setOnmatch((String match) -> { Player p = Bukkit.getPlayer(match); if (p == null) { PluginMain.Instance.getLogger() .warning("Error: Can't find player " + match + " but it was reported as online."); - return false; + return "§c" + match + "§r"; } ChatPlayer mpp = ChatPlayer.GetFromPlayer(p); if (PlayerListener.NotificationSound == null) @@ -122,55 +132,44 @@ public class ChatProcessing { p.playSound(p.getLocation(), PlayerListener.NotificationSound, 1.0f, (float) PlayerListener.NotificationPitch); String color = String.format("§%x", (mpp.GetFlairColor() == 0x00 ? 0xb : mpp.GetFlairColor())); - return true; // TODO + return color + p.getName() + "§r"; }).setPriority(Priority.High).build()); - formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(sb.toString())) + formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(nicksb.toString())) .setColor(ChatFormatter.Color.Aqua).setOnmatch((String match) -> { - for (String n : PlayerListener.nicknames.keySet()) { - String nwithoutformatting = new String(n); - int index; - while ((index = nwithoutformatting.indexOf("§k")) != -1) - nwithoutformatting = nwithoutformatting - .replace("§k" + nwithoutformatting.charAt(index + 2), ""); // Support for one random char - while ((index = nwithoutformatting.indexOf('§')) != -1) - nwithoutformatting = nwithoutformatting - .replace("§" + nwithoutformatting.charAt(index + 1), ""); - if (!match.equalsIgnoreCase(nwithoutformatting)) - continue; // TODO - Player p = Bukkit.getPlayer(PlayerListener.nicknames.get(n)); + if (PlayerListener.nicknames.containsKey(match)) { + Player p = Bukkit.getPlayer(PlayerListener.nicknames.get(match)); if (p == null) { - PluginMain.Instance.getLogger().warning( - "Error: Can't find player " + match + " but it was reported as online."); - return false; + PluginMain.Instance.getLogger().warning("Error: Can't find player nicknamed " + match + + " but it was reported as online."); + return "§c" + match + "§r"; } - ChatPlayer mpp = ChatPlayer.GetFromPlayer(p); if (PlayerListener.NotificationSound == null) p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1.0f, 0.5f); // TODO: // Airhorn else p.playSound(p.getLocation(), PlayerListener.NotificationSound, 1.0f, (float) PlayerListener.NotificationPitch); - String color = String.format("§%x", - (mpp.GetFlairColor() == 0x00 ? 0xb : mpp.GetFlairColor())); + return PlayerListener.essentials.getUser(p).getNickname(); } - return true; // TODO + 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()); } pingedconsole = false; - formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile("(?i)" + Pattern.quote("@console"))) - .setColor(ChatFormatter.Color.Aqua).setOnmatch((String match) -> { + formatters.add(new ChatFormatterBuilder().setRegex(CONSOLE_PING_PATTERN).setColor(ChatFormatter.Color.Aqua) + .setOnmatch((String match) -> { if (!pingedconsole) { System.out.print("\007"); pingedconsole = true; } - return true; + return match; }).setPriority(Priority.High).build()); - formatters - .add(new ChatFormatterBuilder().setRegex(Pattern.compile("#(\\w+)")).setColor(ChatFormatter.Color.Blue) - .setOpenlink("https://twitter.com/hashtag/$1").setPriority(Priority.High).build()); + formatters.add(new ChatFormatterBuilder().setRegex(HASHTAG_PATTERN).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"), diff --git a/src/buttondevteam/thebuttonmcchat/PlayerListener.java b/src/buttondevteam/thebuttonmcchat/PlayerListener.java index 46b69a5..ea9b4de 100644 --- a/src/buttondevteam/thebuttonmcchat/PlayerListener.java +++ b/src/buttondevteam/thebuttonmcchat/PlayerListener.java @@ -1,7 +1,6 @@ package buttondevteam.thebuttonmcchat; import java.util.ArrayList; -import java.util.HashMap; import java.util.Random; import java.util.Timer; import java.util.TimerTask; @@ -44,6 +43,8 @@ import buttondevteam.bucket.core.TBMCPlayerSaveEvent; import buttondevteam.thebuttonmcchat.commands.ucmds.KittycannonCommand; import com.earth2me.essentials.Essentials; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import com.palmergames.bukkit.towny.object.Resident; import com.palmergames.bukkit.towny.object.Town; @@ -55,7 +56,10 @@ import com.vexsoftware.votifier.model.Vote; import com.vexsoftware.votifier.model.VotifierEvent; public class PlayerListener implements Listener { - public static HashMap nicknames = new HashMap<>(); + /** + * Does not contain format codes + */ + public static BiMap nicknames = HashBiMap.create(); public static boolean Enable = false; @@ -125,7 +129,17 @@ public class PlayerListener implements Listener { timer.schedule(tt, 15 * 1000); } - nicknames.put(essentials.getUser(p).getNickname(), p.getUniqueId()); + String nwithoutformatting = essentials.getUser(p).getNickname(); + int index; + while ((index = nwithoutformatting.indexOf("§k")) != -1) + nwithoutformatting = nwithoutformatting.replace("§k" + nwithoutformatting.charAt(index + 2), ""); // Support + // for + // one + // random + // char + while ((index = nwithoutformatting.indexOf('§')) != -1) + nwithoutformatting = nwithoutformatting.replace("§" + nwithoutformatting.charAt(index + 1), ""); + nicknames.put(nwithoutformatting, p.getUniqueId()); cp.RPMode = true; @@ -385,19 +399,9 @@ public class PlayerListener implements Listener { public void onTabComplete(PlayerChatTabCompleteEvent e) { String name = e.getLastToken(); for (String nickname : nicknames.keySet()) { - String nwithoutformatting = nickname; - int index; - while ((index = nwithoutformatting.indexOf("§k")) != -1) - nwithoutformatting = nwithoutformatting.replace("§k" + nwithoutformatting.charAt(index + 2), ""); // Support - // for - // one - // random - // char - while ((index = nwithoutformatting.indexOf('§')) != -1) - nwithoutformatting = nwithoutformatting.replace("§" + nwithoutformatting.charAt(index + 1), ""); - if (nwithoutformatting.startsWith(name) - && !nwithoutformatting.equals(Bukkit.getPlayer(nicknames.get(nickname)).getName())) - e.getTabCompletions().add(nwithoutformatting); + if (nickname.startsWith(name) + && !nickname.equals(Bukkit.getPlayer(nicknames.get(nickname)).getName())) + e.getTabCompletions().add(nickname); } }