From 42784ff3a51ca5882c4637fc22ea00cd654d1bd6 Mon Sep 17 00:00:00 2001 From: cnaude Date: Mon, 5 May 2014 22:17:53 -0700 Subject: [PATCH] Strip IRC background colors. --- dependency-reduced-pom.xml | 2 +- pom.xml | 2 +- .../java/com/cnaude/purpleirc/PurpleIRC.java | 4 +++- .../purpleirc/Utilities/ColorConverter.java | 19 ++++++++++++++++--- src/main/resources/config.yml | 2 ++ 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index 1f519eb..f8f75ee 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -4,7 +4,7 @@ com.cnaude PurpleIRC PurpleIRC - 2.1.13 + 2.1.14-SNAPSHOT A CraftBukkit plugin for bridging game chat with IRC. Connect to any number of IRC servers and channels simultaneously. http://dev.bukkit.org/server-mods/purpleirc/ diff --git a/pom.xml b/pom.xml index 6bbf6d0..02eb071 100644 --- a/pom.xml +++ b/pom.xml @@ -339,7 +339,7 @@ - + diff --git a/src/main/java/com/cnaude/purpleirc/PurpleIRC.java b/src/main/java/com/cnaude/purpleirc/PurpleIRC.java index a9da751..1154b01 100644 --- a/src/main/java/com/cnaude/purpleirc/PurpleIRC.java +++ b/src/main/java/com/cnaude/purpleirc/PurpleIRC.java @@ -107,6 +107,7 @@ public class PurpleIRC extends JavaPlugin { private boolean debugEnabled; private boolean stripGameColors; private boolean stripIRCColors; + private boolean stripIRCBackgroundColors; private boolean customTabList; private boolean listSortByName; public boolean exactNickMatch; @@ -515,8 +516,9 @@ private void loadConfig() { logDebug("Debug enabled"); stripGameColors = getConfig().getBoolean("strip-game-colors", false); stripIRCColors = getConfig().getBoolean("strip-irc-colors", false); + stripIRCBackgroundColors = getConfig().getBoolean("strip-irc-bg-colors", true); exactNickMatch = getConfig().getBoolean("nick-exact-match", true); - colorConverter = new ColorConverter(this, stripGameColors, stripIRCColors); + colorConverter = new ColorConverter(this, stripGameColors, stripIRCColors, stripIRCBackgroundColors); logDebug("strip-game-colors: " + stripGameColors); logDebug("strip-irc-colors: " + stripIRCColors); diff --git a/src/main/java/com/cnaude/purpleirc/Utilities/ColorConverter.java b/src/main/java/com/cnaude/purpleirc/Utilities/ColorConverter.java index 8a6cb9f..6dbfd98 100644 --- a/src/main/java/com/cnaude/purpleirc/Utilities/ColorConverter.java +++ b/src/main/java/com/cnaude/purpleirc/Utilities/ColorConverter.java @@ -10,6 +10,8 @@ import com.cnaude.purpleirc.PurpleIRC; import java.util.EnumMap; import java.util.HashMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.bukkit.ChatColor; import org.pircbotx.Colors; @@ -22,8 +24,10 @@ public class ColorConverter { PurpleIRC plugin; private final boolean stripGameColors; private final boolean stripIRCColors; + private final boolean stripIRCBackgroundColors; private final EnumMap ircColorMap = new EnumMap(ChatColor.class); private final HashMap gameColorMap = new HashMap(); + private final Pattern pattern; /** * @@ -31,11 +35,13 @@ public class ColorConverter { * @param stripGameColors * @param stripIRCColors */ - public ColorConverter(PurpleIRC plugin, boolean stripGameColors, boolean stripIRCColors) { + public ColorConverter(PurpleIRC plugin, boolean stripGameColors, boolean stripIRCColors, boolean stripIRCBackgroundColors) { this.stripGameColors = stripGameColors; this.stripIRCColors = stripIRCColors; + this.stripIRCBackgroundColors = stripIRCBackgroundColors; this.plugin = plugin; buildDefaultColorMaps(); + this.pattern = Pattern.compile("((\\u0003\\d+),\\d+)"); } /** @@ -62,6 +68,13 @@ public String gameColorsToIrc(String message) { * @return */ public String ircColorsToGame(String message) { + if (stripIRCBackgroundColors) { + Matcher m = pattern.matcher(message); + while (m.find()) { + plugin.logDebug("Stripping background colors: " + m.group(1) + "=>" + m.group(2)); + message = message.replace(m.group(1), m.group(2)); + } + } if (stripIRCColors) { return Colors.removeFormattingAndColors(message); } else { @@ -99,11 +112,11 @@ public void addGameColorMap(String ircColor, String gameColor) { plugin.logDebug("addGameColorMap: " + ircColor + " => " + gameColor); gameColorMap.put(getIrcColor(ircColor), chatColor); } - + private String getIrcColor(String ircColor) { String s = ""; try { - s = (String)Colors.class.getField(ircColor.toUpperCase()).get(null); + s = (String) Colors.class.getField(ircColor.toUpperCase()).get(null); } catch (NoSuchFieldException ex) { plugin.logError(ex.getMessage()); } catch (SecurityException ex) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index afbb86b..57a124f 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -154,6 +154,8 @@ list-sort-by-name: true strip-game-colors: false # Prevent IRC colors from appearing in game strip-irc-colors: false +# Strip IRC background colors (recommend leaving this as true) +strip-irc-bg-colors: true # Add IRC names to in-game tab list custom-tab-list: false # Nothing to see here