commit 66d1c685bfdbf53c6547a53c0e455a91bc0c038e Author: ElgarL Date: Thu Oct 20 12:25:23 2011 +0000 diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..757c1f5 --- /dev/null +++ b/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..e31a149 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + TownyChat + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..fdeb044 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +#Mon Sep 26 22:06:26 BST 2011 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/bin/com/palmergames/bukkit/TownyChat/Chat.class b/bin/com/palmergames/bukkit/TownyChat/Chat.class new file mode 100644 index 0000000..ec81075 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/Chat.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$1.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$1.class new file mode 100644 index 0000000..2c66d4a Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$1.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$10.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$10.class new file mode 100644 index 0000000..416a8d4 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$10.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$11.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$11.class new file mode 100644 index 0000000..d68b1e9 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$11.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$12.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$12.class new file mode 100644 index 0000000..fc5ee8b Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$12.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$13.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$13.class new file mode 100644 index 0000000..5321a53 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$13.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$14.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$14.class new file mode 100644 index 0000000..5e90b0e Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$14.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$15.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$15.class new file mode 100644 index 0000000..673895a Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$15.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$16.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$16.class new file mode 100644 index 0000000..a93fea7 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$16.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$17.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$17.class new file mode 100644 index 0000000..43e20b0 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$17.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$18.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$18.class new file mode 100644 index 0000000..5308cf1 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$18.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$19.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$19.class new file mode 100644 index 0000000..afbf642 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$19.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$2.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$2.class new file mode 100644 index 0000000..3525f09 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$2.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$20.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$20.class new file mode 100644 index 0000000..a5db738 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$20.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$21.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$21.class new file mode 100644 index 0000000..75225c4 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$21.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$22.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$22.class new file mode 100644 index 0000000..4238113 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$22.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$23.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$23.class new file mode 100644 index 0000000..6e60ea6 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$23.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$24.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$24.class new file mode 100644 index 0000000..97d0774 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$24.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$25.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$25.class new file mode 100644 index 0000000..15c8902 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$25.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$3.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$3.class new file mode 100644 index 0000000..ae55cd1 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$3.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$4.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$4.class new file mode 100644 index 0000000..f8a271f Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$4.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$5.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$5.class new file mode 100644 index 0000000..42c2fd9 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$5.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$6.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$6.class new file mode 100644 index 0000000..91aadcf Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$6.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$7.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$7.class new file mode 100644 index 0000000..dcea5bb Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$7.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$8.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$8.class new file mode 100644 index 0000000..299ac6f Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$8.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$9.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$9.class new file mode 100644 index 0000000..80c0f5d Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter$9.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter.class new file mode 100644 index 0000000..516a5c0 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatFormatter.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/TownyChatReplacerCallable.class b/bin/com/palmergames/bukkit/TownyChat/TownyChatReplacerCallable.class new file mode 100644 index 0000000..270e9ee Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/TownyChatReplacerCallable.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/event/TownyChatEvent.class b/bin/com/palmergames/bukkit/TownyChat/event/TownyChatEvent.class new file mode 100644 index 0000000..4c72437 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/event/TownyChatEvent.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/event/TownyPlayerHighestListener.class b/bin/com/palmergames/bukkit/TownyChat/event/TownyPlayerHighestListener.class new file mode 100644 index 0000000..a8511db Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/event/TownyPlayerHighestListener.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/tasks/onLoadedTask.class b/bin/com/palmergames/bukkit/TownyChat/tasks/onLoadedTask.class new file mode 100644 index 0000000..6ee5778 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/tasks/onLoadedTask.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/util/ReplacerCallable.class b/bin/com/palmergames/bukkit/TownyChat/util/ReplacerCallable.class new file mode 100644 index 0000000..50ae1a5 Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/util/ReplacerCallable.class differ diff --git a/bin/com/palmergames/bukkit/TownyChat/util/StringReplaceManager.class b/bin/com/palmergames/bukkit/TownyChat/util/StringReplaceManager.class new file mode 100644 index 0000000..ba07f6d Binary files /dev/null and b/bin/com/palmergames/bukkit/TownyChat/util/StringReplaceManager.class differ diff --git a/bin/plugin.yml b/bin/plugin.yml new file mode 100644 index 0000000..bb2c981 --- /dev/null +++ b/bin/plugin.yml @@ -0,0 +1,11 @@ +name: TownyChat +main: com.palmergames.bukkit.TownyChat.Chat +version: 0.2 +author: ElgarL +website: http://code.google.com/a/eclipselabs.org/p/towny/ +description: > + TownyChat plugin which hooks Towny. + +softdepend: [Towny] + +commands: \ No newline at end of file diff --git a/src/com/palmergames/bukkit/TownyChat/Chat.java b/src/com/palmergames/bukkit/TownyChat/Chat.java new file mode 100644 index 0000000..c8fe0ce --- /dev/null +++ b/src/com/palmergames/bukkit/TownyChat/Chat.java @@ -0,0 +1,86 @@ +package com.palmergames.bukkit.TownyChat; + +import java.util.logging.Logger; + +import org.bukkit.event.Event; +import org.bukkit.event.Event.Priority; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.java.JavaPlugin; + +import com.palmergames.bukkit.TownyChat.event.TownyPlayerHighestListener; +import com.palmergames.bukkit.TownyChat.tasks.onLoadedTask; +import com.palmergames.bukkit.towny.Towny; + +/** + * TownyChat plugin to manage all Towny chat + * + * Website: http://code.google.com/a/eclipselabs.org/p/towny/ + * + * @author ElgarL + */ + +public class Chat extends JavaPlugin { + + private Logger logger = Logger.getLogger("com.palmergames.bukkit.TownyChat"); + private TownyPlayerHighestListener TownyPlayerListener; + + protected PluginManager pm; + private Towny towny = null; + + @Override + public void onEnable() { + + pm = getServer().getPluginManager(); + + checkPlugins(); + + /* + * This executes the task with a 1 tick delay avoiding the bukkit + * depends bug. + */ + if ((towny == null) || (getServer().getScheduler().scheduleSyncDelayedTask(this, new onLoadedTask(this), 1) == -1)) { + /* + * We either failed to find Towny or the Scheduler failed to + * register the task. + */ + logger.severe("Could not schedule onLoadedTask."); + logger.severe("disabling TownyChat"); + pm.disablePlugin(this); + } + + } + + @Override + public void onDisable() { + // reset any handles + towny = null; + pm = null; + logger = null; + } + + private void checkPlugins() { + Plugin test; + + test = pm.getPlugin("Towny"); + if (test != null && test instanceof Towny) + towny = (Towny) test; + + } + + public void registerEvents() { + TownyPlayerListener = new TownyPlayerHighestListener(towny); + + pm.registerEvent(Event.Type.PLAYER_CHAT, TownyPlayerListener, Priority.Highest, this); + pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, TownyPlayerListener, Priority.Highest, this); + } + + public Logger getLogger() { + return logger; + } + + public Towny getTowny() { + return towny; + } + +} \ No newline at end of file diff --git a/src/com/palmergames/bukkit/TownyChat/TownyChatFormatter.java b/src/com/palmergames/bukkit/TownyChat/TownyChatFormatter.java new file mode 100644 index 0000000..4e274e4 --- /dev/null +++ b/src/com/palmergames/bukkit/TownyChat/TownyChatFormatter.java @@ -0,0 +1,263 @@ +package com.palmergames.bukkit.TownyChat; + +import java.util.regex.Pattern; + +import com.palmergames.bukkit.towny.NotRegisteredException; +import com.palmergames.bukkit.towny.TownyFormatter; +import com.palmergames.bukkit.towny.TownySettings; +import com.palmergames.bukkit.towny.object.Nation; +import com.palmergames.bukkit.towny.object.Resident; +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.TownyUniverse; +import com.palmergames.bukkit.TownyChat.event.TownyChatEvent; +import com.palmergames.bukkit.TownyChat.util.StringReplaceManager; + +public class TownyChatFormatter { + private static StringReplaceManager replacer = new StringReplaceManager(); + + static { + replacer.registerFormatReplacement(Pattern.quote("{town}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return event.getResident().hasTown() ? event.getResident().getTown().getName() : ""; + } + }); + replacer.registerFormatReplacement(Pattern.quote("{townformatted}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return formatTownyTag(event.getResident(), false, true); + } + }); + replacer.registerFormatReplacement(Pattern.quote("{towntag}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return formatTownTag(event.getResident(), false, false); + } + }); + replacer.registerFormatReplacement(Pattern.quote("{towntagoverride}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return formatTownTag(event.getResident(), true, false); + } + }); + + replacer.registerFormatReplacement(Pattern.quote("{nation}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return event.getResident().hasNation() ? event.getResident().getTown().getNation().getName() : ""; + } + }); + replacer.registerFormatReplacement(Pattern.quote("{nationformatted}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return formatNationTag(event.getResident(), false, true); + } + }); + replacer.registerFormatReplacement(Pattern.quote("{nationtag}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return formatNationTag(event.getResident(), false, false); + } + }); + replacer.registerFormatReplacement(Pattern.quote("{nationtagoverride}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return formatNationTag(event.getResident(), true, false); + } + }); + + replacer.registerFormatReplacement(Pattern.quote("{townytag}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return formatTownyTag(event.getResident(), false, false); + } + }); + replacer.registerFormatReplacement(Pattern.quote("{townyformatted}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return formatTownyTag(event.getResident(), false, true); + } + }); + replacer.registerFormatReplacement(Pattern.quote("{townytagoverride}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return formatTownyTag(event.getResident(), true, false); + } + }); + + replacer.registerFormatReplacement(Pattern.quote("{title}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return event.getResident().hasTitle() ? event.getResident().getTitle() : ""; + } + }); + replacer.registerFormatReplacement(Pattern.quote("{surname}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return event.getResident().hasSurname() ? event.getResident().getSurname() : ""; + } + }); + replacer.registerFormatReplacement(Pattern.quote("{townynameprefix}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return TownyFormatter.getNamePrefix(event.getResident()); + } + }); + replacer.registerFormatReplacement(Pattern.quote("{townynamepostfix}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return TownyFormatter.getNamePostfix(event.getResident()); + } + }); + replacer.registerFormatReplacement(Pattern.quote("{townyprefix}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return event.getResident().hasTitle() ? event.getResident().getTitle() : TownyFormatter.getNamePrefix(event.getResident()); + } + }); + replacer.registerFormatReplacement(Pattern.quote("{townypostfix}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return event.getResident().hasSurname() ? event.getResident().getSurname() : TownyFormatter.getNamePostfix(event.getResident()); + } + }); + + replacer.registerFormatReplacement(Pattern.quote("{townycolor}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return event.getResident().isMayor() ? (event.getResident().isKing() ? TownySettings.getKingColour() : TownySettings.getMayorColour()) : ""; + } + }); + replacer.registerFormatReplacement(Pattern.quote("{group}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return TownyUniverse.getPermissionSource().getPlayerGroup(event.getEvent().getPlayer()); + } + }); + replacer.registerFormatReplacement(Pattern.quote("{permprefix}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return TownyUniverse.getPermissionSource().getPrefixSuffix(event.getResident(), "prefix"); + } + }); + replacer.registerFormatReplacement(Pattern.quote("{permsuffix}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return TownyUniverse.getPermissionSource().getPrefixSuffix(event.getResident(), "suffix"); + } + }); + + replacer.registerFormatReplacement(Pattern.quote("{playername}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return event.getEvent().getPlayer().getName(); + } + }); + replacer.registerFormatReplacement(Pattern.quote("{modplayername}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return event.getEvent().getPlayer().getDisplayName(); + } + }); + + replacer.registerFormatReplacement(Pattern.quote("{channelTag}"), new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return formatTownyTag(event.getResident(), false, false); + } + }); + + // Replace colours last ({msg} is replaced last as it can't be regex parsed). + replacer.registerFormatReplacement("&{1}[0-9A-Fa-f]{1}", new TownyChatReplacerCallable() { + @Override + public String call(String match, TownyChatEvent event) throws Exception { + return "\u00A7" + match.charAt(1); + } + }); + + } + + public static String getChatFormat(TownyChatEvent event) { + // Replace the {msg} here so it's not regex parsed. + return replacer.replaceAll(event.getFormat(), event).replace("{msg}", event.getMessage()); + } + + /** + * @param resident + * @param override use full names if no tag is present + * @param full Only use full names (no tags). + * @return + */ + public static String formatTownyTag(Resident resident, Boolean override, Boolean full) { + try { + if (resident.hasTown()) { + Town town = resident.getTown(); + String townTag = town.getTag(); + Nation nation = null; + String nationTag = null; + if (resident.hasNation()) { + nation = town.getNation(); + nationTag = nation.getTag(); + } + + String nTag = "", tTag = ""; + + if (full) { + nationTag = ""; + townTag = ""; + } + // Load town tags/names + if (townTag != null && !townTag.isEmpty()) + tTag = townTag; + else if (override || full) + tTag = town.getName(); + + // Load the nation tags/names + if ((nationTag != null) && !nationTag.isEmpty()) + nTag = nationTag; + else if (resident.hasNation() && (override || full)) + nTag = nation.getName(); + + // Output depending on what tags are present + if ((tTag != "") && (nTag != "")) + return String.format(TownySettings.getChatTownNationTagFormat(), nTag, tTag); + + if (nTag != "") + return String.format(TownySettings.getChatNationTagFormat(), nTag); + + if (tTag != "") + return String.format(TownySettings.getChatTownTagFormat(), tTag); + + } + } catch (NotRegisteredException e) { + // no town or nation + } + return ""; + } + + public static String formatTownTag(Resident resident, Boolean override, Boolean full) { + try { + if (resident.hasTown()) + if (resident.getTown().hasTag()) + return String.format(TownySettings.getChatTownTagFormat(), resident.getTown().getTag()); + else if (override) + return String.format(TownySettings.getChatTownTagFormat(), resident.getTown().getName()); + + } catch (NotRegisteredException e) { + } + return ""; + } + + public static String formatNationTag(Resident resident, Boolean override, Boolean full) { + try { + if (resident.hasNation()) + if (resident.getTown().getNation().hasTag()) + return String.format(TownySettings.getChatNationTagFormat(), resident.getTown().getNation().getTag()); + else if (override) + return String.format(TownySettings.getChatNationTagFormat(), resident.getTown().getNation().getName()); + + } catch (NotRegisteredException e) { + } + return ""; + } +} diff --git a/src/com/palmergames/bukkit/TownyChat/TownyChatReplacerCallable.java b/src/com/palmergames/bukkit/TownyChat/TownyChatReplacerCallable.java new file mode 100644 index 0000000..f63c3be --- /dev/null +++ b/src/com/palmergames/bukkit/TownyChat/TownyChatReplacerCallable.java @@ -0,0 +1,12 @@ +package com.palmergames.bukkit.TownyChat; + +import com.palmergames.bukkit.TownyChat.event.TownyChatEvent; +import com.palmergames.bukkit.TownyChat.util.ReplacerCallable; + +public abstract class TownyChatReplacerCallable implements ReplacerCallable { + public TownyChatReplacerCallable() { + + } + + public abstract String call(String match, TownyChatEvent event) throws Exception; +} diff --git a/src/com/palmergames/bukkit/TownyChat/event/TownyChatEvent.java b/src/com/palmergames/bukkit/TownyChat/event/TownyChatEvent.java new file mode 100644 index 0000000..56ed1cf --- /dev/null +++ b/src/com/palmergames/bukkit/TownyChat/event/TownyChatEvent.java @@ -0,0 +1,56 @@ +package com.palmergames.bukkit.TownyChat.event; + +import org.bukkit.event.player.PlayerChatEvent; + +import com.palmergames.bukkit.towny.object.Resident; + +public class TownyChatEvent { + private PlayerChatEvent event; + private Resident resident; + + public TownyChatEvent(PlayerChatEvent event, Resident resident) { + this.event = event; + this.resident = resident; + } + + /** + * Get the resident associated with the chat event's talking player. + * @return resident associated with the chat event's talking player + */ + public Resident getResident() { + return resident; + } + + /** + * + * @return the PlayerChatEvent + */ + public PlayerChatEvent getEvent() { + return event; + } + + + /** + * Convenience method for setting the chat event's format. + * @param format + */ + public void setFormat(String format) { + event.setFormat(format); + } + + /** + * Convenience method for getting the chat event's format + * @return the chat event's format + */ + public String getFormat() { + return event.getFormat(); + } + + /** + * Convenience method for getting the chat event's message + * @return the chat event's message + */ + public String getMessage() { + return event.getMessage(); + } +} diff --git a/src/com/palmergames/bukkit/TownyChat/event/TownyPlayerHighestListener.java b/src/com/palmergames/bukkit/TownyChat/event/TownyPlayerHighestListener.java new file mode 100644 index 0000000..e11b280 --- /dev/null +++ b/src/com/palmergames/bukkit/TownyChat/event/TownyPlayerHighestListener.java @@ -0,0 +1,159 @@ +package com.palmergames.bukkit.TownyChat.event; + + +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerChatEvent; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.event.player.PlayerListener; + +import com.palmergames.bukkit.towny.NotRegisteredException; +import com.palmergames.bukkit.towny.Towny; +import com.palmergames.bukkit.towny.TownyMessaging; +import com.palmergames.bukkit.towny.TownySettings; +import com.palmergames.bukkit.TownyChat.event.TownyChatEvent; +import com.palmergames.bukkit.TownyChat.TownyChatFormatter; +import com.palmergames.bukkit.towny.object.Nation; +import com.palmergames.bukkit.towny.object.Resident; +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.TownyUniverse; +import com.palmergames.util.StringMgmt; + +public class TownyPlayerHighestListener extends PlayerListener { + private final Towny plugin; + + public TownyPlayerHighestListener(Towny instance) { + this.plugin = instance; + } + + @Override + public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { + + Player player = event.getPlayer(); + + // Test if this player is registered with Towny. + try { + plugin.getTownyUniverse().getResident(player.getName()); + } catch (NotRegisteredException e) { + return; + } + String split[] = event.getMessage().split("\\ "); + + if (split.length > 1) { + String command = split[0].trim().toLowerCase(); + String message = StringMgmt.join(StringMgmt.remFirstArg(split), " "); + + if (TownySettings.chatChannelExists(command)) { + event.setMessage(message); + + if (!plugin.isPermissions() || (plugin.isPermissions() && TownyUniverse.getPermissionSource().hasPermission(player, TownySettings.getChatChannelPermission(command)))) { + // Deal with special cases + if (command.equalsIgnoreCase("/tc")) { + + // Town Chat + parseTownChatCommand(event, command, player); + event.setCancelled(true); + + } else if (command.equalsIgnoreCase("/nc")) { + + // Nation Chat + parseNationChatCommand(event, command, player); + event.setCancelled(true); + + } else { + + // Custom channel Chat + parseDefaultChannelChatCommand(event, command, player); + event.setCancelled(true); + } + } else { + TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_err_command_disable")); + event.setCancelled(true); + } + + }// channel doesn't exist + + } + } + + @Override + public void onPlayerChat(PlayerChatEvent event) { + + Player player = event.getPlayer(); + Resident resident; + try { + resident = plugin.getTownyUniverse().getResident(player.getName()); + } catch (NotRegisteredException e) { + return; + } + + if (plugin.hasPlayerMode(player, "tc")) { + // Town Chat + parseTownChatCommand(event, "/tc", player); + } else if (plugin.hasPlayerMode(player, "nc")) { + // Nation chat + parseNationChatCommand(event, "/nc", player); + } else { + // All chat modes are disabled, or this is open chat. + if (TownySettings.isUsingModifyChat()) { + event.setFormat(TownySettings.getModifyChatFormat()); + } + + TownyChatEvent chatEvent = new TownyChatEvent(event, resident); + event.setFormat(TownyChatFormatter.getChatFormat(chatEvent)); + return; + } + event.setCancelled(true); + + } + + private void parseTownChatCommand(PlayerChatEvent event, String command, Player player) { + try { + Resident resident = plugin.getTownyUniverse().getResident(player.getName()); + Town town = resident.getTown(); + + event.setFormat(TownySettings.getChatTownChannelFormat().replace("{channelTag}", TownySettings.getChatChannelName(command)).replace("{msgcolour}", TownySettings.getChatChannelColour(command))); + + TownyChatEvent chatEvent = new TownyChatEvent(event, resident); + event.setFormat(TownyChatFormatter.getChatFormat(chatEvent)); + + TownyMessaging.sendTownMessage(town, chatEvent.getFormat()); + } catch (NotRegisteredException x) { + TownyMessaging.sendErrorMsg(player, x.getError()); + } + } + + private void parseNationChatCommand(PlayerChatEvent event, String command, Player player) { + try { + Resident resident = plugin.getTownyUniverse().getResident(player.getName()); + Nation nation = resident.getTown().getNation(); + + event.setFormat(TownySettings.getChatNationChannelFormat().replace("{channelTag}", TownySettings.getChatChannelName(command)).replace("{msgcolour}", TownySettings.getChatChannelColour(command))); + + TownyChatEvent chatEvent = new TownyChatEvent(event, resident); + event.setFormat(TownyChatFormatter.getChatFormat(chatEvent)); + TownyMessaging.sendNationMessage(nation, chatEvent.getFormat()); + } catch (NotRegisteredException x) { + TownyMessaging.sendErrorMsg(player, x.getError()); + } + } + + private void parseDefaultChannelChatCommand(PlayerChatEvent event, String command, Player player) { + try { + Resident resident = plugin.getTownyUniverse().getResident(player.getName()); + + event.setFormat(TownySettings.getChatDefaultChannelFormat().replace("{channelTag}", TownySettings.getChatChannelName(command)).replace("{msgcolour}", TownySettings.getChatChannelColour(command))); + + TownyChatEvent chatEvent = new TownyChatEvent(event, resident); + event.setFormat(TownyChatFormatter.getChatFormat(chatEvent)); + + for (Player test : plugin.getTownyUniverse().getOnlinePlayers()) { + if (!plugin.isPermissions() || (plugin.isPermissions() && TownyUniverse.getPermissionSource().hasPermission(test, TownySettings.getChatChannelPermission(command)))) + TownyMessaging.sendMessage(test, chatEvent.getFormat()); + } + + // TownyMessaging.sendNationMessage(nation, chatEvent.getFormat()); + } catch (NotRegisteredException x) { + TownyMessaging.sendErrorMsg(player, x.getError()); + } + } +} diff --git a/src/com/palmergames/bukkit/TownyChat/tasks/onLoadedTask.java b/src/com/palmergames/bukkit/TownyChat/tasks/onLoadedTask.java new file mode 100644 index 0000000..5f3fc3d --- /dev/null +++ b/src/com/palmergames/bukkit/TownyChat/tasks/onLoadedTask.java @@ -0,0 +1,31 @@ +package com.palmergames.bukkit.TownyChat.tasks; + +import com.palmergames.bukkit.TownyChat.Chat; +import com.palmergames.bukkit.towny.Towny; + +/* + * + * Created by ElgarL + * + */ + +public class onLoadedTask implements Runnable { + + Chat plugin; + Towny towny; + + public onLoadedTask(Chat plugin) { + super(); + this.plugin = plugin; + this.towny = plugin.getTowny(); + } + + @Override + public void run() { + + plugin.getLogger().info("-******* TownyChat enabled *******-"); + plugin.registerEvents(); + + } + +} \ No newline at end of file diff --git a/src/com/palmergames/bukkit/TownyChat/util/ReplacerCallable.java b/src/com/palmergames/bukkit/TownyChat/util/ReplacerCallable.java new file mode 100644 index 0000000..0972dd3 --- /dev/null +++ b/src/com/palmergames/bukkit/TownyChat/util/ReplacerCallable.java @@ -0,0 +1,11 @@ +package com.palmergames.bukkit.TownyChat.util; + +/** + * A Callable used for regex matching. + * Used in conjunction with StringReplaceManager. + * + * @author Chris H (Shade) + */ +public interface ReplacerCallable { + public String call(String match, E e) throws Exception; +} diff --git a/src/com/palmergames/bukkit/TownyChat/util/StringReplaceManager.java b/src/com/palmergames/bukkit/TownyChat/util/StringReplaceManager.java new file mode 100644 index 0000000..2de4fd8 --- /dev/null +++ b/src/com/palmergames/bukkit/TownyChat/util/StringReplaceManager.java @@ -0,0 +1,107 @@ +package com.palmergames.bukkit.TownyChat.util; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + + +/** + * Register keyword regex's to be dynamically replaced. + * + * Credits to: + * - http://stackoverflow.com/questions/1326682/java-replacing-multiple-different-substring-in-a-string-at-once-or-in-the-most-e/1326962#1326962 + * + * @author Chris H (Shade) + * @version 1.0 + */ +public class StringReplaceManager { + private List replacementPatterns = new ArrayList(); + private List replacements = new ArrayList(); + + public boolean registerFormatReplacement(String regexKey) { + return registerFormatReplacement(regexKey, null); + } + + public boolean registerFormatReplacement(String regexKey, Object replacement) { + Pattern pattern = Pattern.compile(regexKey); + replacementPatterns.add(pattern); + replacements.add(replacement); + return true; + } + + @SuppressWarnings("unchecked") + public String replaceAll(String format, E e) { + String out = format; + for (int i = 0; i < replacementPatterns.size(); i++) { + Matcher matcher = replacementPatterns.get(i).matcher(out); + Object replacer = replacements.get(i); + StringBuffer sb = new StringBuffer(); + + while (matcher.find()) { + String match = matcher.group(); + if (replacer == null) { + matcher.appendReplacement(sb, e.toString()); + } else if (replacer instanceof String) { + matcher.appendReplacement(sb, replacer.toString()); + } else { + try { + String replacement = ((ReplacerCallable)replacer).call(match, e); + matcher.appendReplacement(sb, replacement); + } catch (Exception exception) { + continue; + } + } + + } + matcher.appendTail(sb); + out = sb.toString(); + } + return out; + } + + public static void main(String[] args) { + String regex = "\\+{1}[a-zA-Z]+"; + String s = "+meh asdf+lo"; + /* + * TestChatEvent is a simple string holder. TestReplacerCallable will return the string held in TestChatEvent. + * + { + StringReplaceManager replacer = new StringReplaceManager(); + replacer.registerFormatReplacement(regex, new TestReplacerCallable()); + System.out.println(replacer.replaceAll(s, new TestChatEvent("!"))); + } + */ + + { + StringReplaceManager replacer = new StringReplaceManager(); + replacer.registerFormatReplacement(regex, "1"); + System.out.println(replacer.replaceAll(s, "2")); + } + + { + StringReplaceManager replacer = new StringReplaceManager(); + replacer.registerFormatReplacement(regex); + System.out.println(replacer.replaceAll(s, "2")); + } + + int times = 1000; + { + long t = System.currentTimeMillis(); + for (int i = 0; i < times; i++) { + s.replaceAll(regex, "2"); + } + System.out.println("Runtime: " + ((System.currentTimeMillis() - t) / ((double)times))); + } + + { + long t = System.currentTimeMillis(); + StringReplaceManager replacer = new StringReplaceManager(); + replacer.registerFormatReplacement(regex); + for (int i = 0; i < times; i++) { + replacer.replaceAll(s, "2"); + } + System.out.println("Precompiled: " + ((System.currentTimeMillis() - t) / ((double)times))); + } + } +} \ No newline at end of file diff --git a/src/plugin.yml b/src/plugin.yml new file mode 100644 index 0000000..bb2c981 --- /dev/null +++ b/src/plugin.yml @@ -0,0 +1,11 @@ +name: TownyChat +main: com.palmergames.bukkit.TownyChat.Chat +version: 0.2 +author: ElgarL +website: http://code.google.com/a/eclipselabs.org/p/towny/ +description: > + TownyChat plugin which hooks Towny. + +softdepend: [Towny] + +commands: \ No newline at end of file