From 2594721152fb02db6259d687eea7581b18d8fc6f Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 30 Dec 2016 21:22:34 +0100 Subject: [PATCH] Fixes! Escape logic again... - Made the escape chars disappear - Fixed empty nickname list matching empty strings --- .../buttondevteam/chat/ChatProcessing.java | 57 ++++++++++--------- .../chat/formatting/ChatFormatter.java | 12 ++-- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 14a1933..c795b7c 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -135,6 +135,7 @@ public class ChatProcessing { namesb.append(")"); StringBuilder nicksb = new StringBuilder(); nicksb.append("(?i)("); + boolean addNickFormatter = false; { final int size = Bukkit.getOnlinePlayers().size(); int index = 0; @@ -144,6 +145,7 @@ public class ChatProcessing { nicksb.append(nick); if (index < size - 1) { nicksb.append("|"); + addNickFormatter = true; } } index++; @@ -169,26 +171,27 @@ public class ChatProcessing { return color + p.getName() + "§r"; }).setPriority(Priority.High).build()); - formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(nicksb.toString())).setColor(Color.Aqua) - .setOnmatch((String match) -> { - 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 nicknamed " + match + " but was reported as online."); - return "§c" + match + "§r"; + if (addNickFormatter) + formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(nicksb.toString())) + .setColor(Color.Aqua).setOnmatch((String match) -> { + 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 nicknamed " + + match + " but was reported as online."); + return "§c" + match + "§r"; + } + if (PlayerListener.NotificationSound == null) + p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1.0f, 0.5f); + else + p.playSound(p.getLocation(), PlayerListener.NotificationSound, 1.0f, + (float) PlayerListener.NotificationPitch); + return PluginMain.essentials.getUser(p).getNickname(); } - if (PlayerListener.NotificationSound == null) - p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1.0f, 0.5f); - else - p.playSound(p.getLocation(), PlayerListener.NotificationSound, 1.0f, - (float) PlayerListener.NotificationPitch); - return PluginMain.essentials.getUser(p).getNickname(); - } - 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()); + 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; // Will set it to true onmatch (static constructor) @@ -198,15 +201,16 @@ public class ChatProcessing { json.addExtra(new TellrawPart("[C]").setHoverEvent( TellrawEvent.create(TellrawEvent.HoverAC, TellrawEvent.HoverAction.SHOW_TEXT, "Chat only"))); } - final String channelidentifier = ("[" + (sender instanceof IDiscordSender ? "d|" : "") + currentchannel.DisplayName) - + "]" + (mp != null && !mp.RPMode ? "[OOC]" : ""); + final String channelidentifier = ("[" + (sender instanceof IDiscordSender ? "d|" : "") + + currentchannel.DisplayName) + "]" + (mp != null && !mp.RPMode ? "[OOC]" : ""); json.addExtra( - new TellrawPart(channelidentifier).setHoverEvent( + new TellrawPart(channelidentifier) + .setHoverEvent( TellrawEvent.create(TellrawEvent.HoverAC, 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, suggestmsg))); + .setClickEvent(TellrawEvent.create(TellrawEvent.ClickAC, + TellrawEvent.ClickAction.SUGGEST_COMMAND, suggestmsg))); json.addExtra(new TellrawPart(" <")); json.addExtra( new TellrawPart( @@ -381,9 +385,8 @@ public class ChatProcessing { player.sendMessage("§cAn error occured while sending the message."); return true; } - PluginMain.Instance.getServer().getConsoleSender() - .sendMessage(String.format("%s <%s> %s", channelidentifier, - (player != null ? player.getDisplayName() : sender.getName()), message)); + PluginMain.Instance.getServer().getConsoleSender().sendMessage(String.format("%s <%s> %s", channelidentifier, + (player != null ? player.getDisplayName() : sender.getName()), message)); DebugCommand.SendDebugMessage( "-- Full ChatProcessing time: " + (System.nanoTime() - processstart) / 1000000f + " ms"); DebugCommand.SendDebugMessage("-- ChatFormatter.Combine time: " + combinetime / 1000000f + " ms"); diff --git a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java index ac6772f..7baf4ae 100644 --- a/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java +++ b/src/main/java/buttondevteam/chat/formatting/ChatFormatter.java @@ -72,8 +72,10 @@ public final class ChatFormatter { final FormattedSection section = sections.get(i); if (!section.IsRange) { escaped = section.Formatters.contains(ChatProcessing.ESCAPE_FORMATTER) && !escaped; // Enable escaping on first \, disable on second - if (!escaped) // Don't add the escape character - combined.add(section); + if (escaped) // Don't add the escape character + section.RemCharFromStart = 1; + combined.add(section); + DebugCommand.SendDebugMessage("Added " + (!escaped ? "not " : "") + "escaped section: " + section); continue; } // TODO: Actually combine overlapping sections if (!escaped) { @@ -87,8 +89,10 @@ public final class ChatFormatter { DebugCommand.SendDebugMessage("Adding next section: " + section); nextSection.put(section.Formatters.get(0), section); } - } else + } else { + DebugCommand.SendDebugMessage("Skipping section: " + section); escaped = false; // Reset escaping if applied, like if we're at the '*' in '\*' + } } sections = combined; boolean cont = true; @@ -225,6 +229,6 @@ public final class ChatFormatter { @Override public String toString() { return new StringBuilder("F(").append(color).append(", ").append(format).append(", ").append(openlink) - .append(", ").append(priority).append(")").toString(); + .append(", ").append(priority).append(", ").append(regex).append(")").toString(); } }