From cee4918526dc8c434988c9f824b537319f8649bf Mon Sep 17 00:00:00 2001 From: cnaude Date: Sat, 28 Feb 2015 23:02:39 -0700 Subject: [PATCH] Add replace-first-occurrences --- .../java/com/cnaude/purpleirc/PurpleBot.java | 40 +++++++++++-------- .../purpleirc/Utilities/ChatTokenizer.java | 14 +------ 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/cnaude/purpleirc/PurpleBot.java b/src/main/java/com/cnaude/purpleirc/PurpleBot.java index 205fad0..d937c61 100644 --- a/src/main/java/com/cnaude/purpleirc/PurpleBot.java +++ b/src/main/java/com/cnaude/purpleirc/PurpleBot.java @@ -50,6 +50,7 @@ import java.text.Collator; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Iterator; import java.util.List; import me.botsko.prism.actionlibs.QueryParameters; import me.botsko.prism.events.BlockStateChange; @@ -140,7 +141,7 @@ public final class PurpleBot { public CaseInsensitiveMap> muteList; public CaseInsensitiveMap> enabledMessages; public CaseInsensitiveMap userPrefixes; - public CaseInsensitiveMap> firstOccurenceReplacements; + public CaseInsensitiveMap> firstOccurrenceReplacements; public String defaultCustomPrefix; public CaseInsensitiveMap>> commandMap; public CaseInsensitiveMap>> extraCommandMap; @@ -177,7 +178,7 @@ public final class PurpleBot { this.extraCommandMap = new CaseInsensitiveMap<>(); this.joinNoticeCooldownMap = new CaseInsensitiveMap<>(); this.enabledMessages = new CaseInsensitiveMap<>(); - this.firstOccurenceReplacements = new CaseInsensitiveMap<>(); + this.firstOccurrenceReplacements = new CaseInsensitiveMap<>(); this.userPrefixes = new CaseInsensitiveMap<>(); this.muteList = new CaseInsensitiveMap<>(); this.worldList = new CaseInsensitiveMap<>(); @@ -671,7 +672,7 @@ public final class PurpleBot { muteList.clear(); enabledMessages.clear(); userPrefixes.clear(); - firstOccurenceReplacements.clear(); + firstOccurrenceReplacements.clear(); worldList.clear(); commandMap.clear(); extraCommandMap.clear(); @@ -681,7 +682,7 @@ public final class PurpleBot { channelCmdNotifyMode = config.getString("command-notify.mode", "msg"); plugin.logDebug(" channelCmdNotifyMode => " + channelCmdNotifyMode); - + for (String s : config.getStringList("custom-prefixes")) { String pair[] = s.split(" ", 2); if (pair.length > 0) { @@ -690,14 +691,14 @@ public final class PurpleBot { plugin.logDebug("CustomPrefix: " + pair[0] + " => " + token); } } - defaultCustomPrefix = ChatColor.translateAlternateColorCodes('&',config.getString("custom-prefix-default", "[IRC]")); - + defaultCustomPrefix = ChatColor.translateAlternateColorCodes('&', config.getString("custom-prefix-default", "[IRC]")); + for (String s : config.getStringList("replace-first-occurrences")) { String pair[] = s.split(" ", 3); if (pair.length > 2) { CaseInsensitiveMap rfo = new CaseInsensitiveMap<>(); rfo.put(pair[1], pair[2]); - firstOccurenceReplacements.put(pair[0], rfo); + firstOccurrenceReplacements.put(pair[0], rfo); plugin.logDebug("ReplaceFirstOccurence: " + pair[0] + " => " + pair[1] + " => " + pair[2]); } } @@ -843,7 +844,7 @@ public final class PurpleBot { enabledMessages.put(channelName, c); if (enabledMessages.isEmpty()) { plugin.logInfo("There are no enabled messages!"); - } + } // build valid world list Collection w = new ArrayList<>(); @@ -2283,14 +2284,6 @@ public final class PurpleBot { } return message; } - - protected String replaceFirstOccurences(String message) { - String newMessage = message; - - - - return newMessage; - } // Broadcast chat messages from IRC /** @@ -2305,6 +2298,21 @@ public final class PurpleBot { public void broadcastChat(User user, org.pircbotx.Channel channel, String target, String message, boolean override, boolean ctcpResponse) { boolean messageSent = false; String myChannel = channel.getName(); + + /* + First occurrence replacements + */ + if (!firstOccurrenceReplacements.isEmpty()) { + for (String key : firstOccurrenceReplacements.keySet()) { + if (user.getNick().equalsIgnoreCase(key) || checkUserMask(user, key)) { + CaseInsensitiveMap cm = firstOccurrenceReplacements.get(key); + for (Object obj : cm.keySet()) { + message = message.replaceFirst((String)obj, ChatColor.translateAlternateColorCodes('&',(String)cm.get(obj))); + } + } + } + } + /* Send messages to Dynmap if enabled */ diff --git a/src/main/java/com/cnaude/purpleirc/Utilities/ChatTokenizer.java b/src/main/java/com/cnaude/purpleirc/Utilities/ChatTokenizer.java index 0956e83..9ba2600 100644 --- a/src/main/java/com/cnaude/purpleirc/Utilities/ChatTokenizer.java +++ b/src/main/java/com/cnaude/purpleirc/Utilities/ChatTokenizer.java @@ -78,20 +78,10 @@ public class ChatTokenizer { plugin.logDebug("customPrefix before: " + customPrefix); if (!ircBot.userPrefixes.isEmpty()) { for (String key : ircBot.userPrefixes.keySet()) { - if (ircBot.userPrefixes.containsKey(user.getNick())) { - plugin.logDebug("Comparing [" + key + "] to [" + user.getNick() + "] MATCH!"); + if (ircBot.userPrefixes.containsKey(user.getNick()) || ircBot.checkUserMask(user, key)) { customPrefix = ircBot.userPrefixes.get(key); break; - } else { - plugin.logDebug("Comparing [" + key + "] to [" + user.getNick() + "] NOPE!"); - } - if (ircBot.checkUserMask(user, key)) { - customPrefix = ircBot.userPrefixes.get(key); - plugin.logDebug("Comparing [" + key + "] to [" + user.getHostmask() + "] MATCH"); - break; - } else { - plugin.logDebug("Comparing [" + key + "] to [" + user.getHostmask() + "] NOPE!"); - } + } } } plugin.logDebug("customPrefix after: " + customPrefix);