mirror of
https://github.com/darksaid98/TownyChat.git
synced 2026-12-02 14:38:57 +00:00
This commit is contained in:
commit
66d1c685bf
46 files changed
+785
No files matched your search
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="lib" path="../../lib/bukkit-0.0.1-SNAPSHOT.jar"/>
|
||||
<classpathentry kind="lib" path="../../lib/craftbukkit-0.0.1-SNAPSHOT.jar"/>
|
||||
<classpathentry kind="lib" path="../../lib/Towny.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>TownyChat</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -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
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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:
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<TownyChatEvent> replacer = new StringReplaceManager<TownyChatEvent>();
|
||||
|
||||
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 "";
|
||||
}
|
||||
}
|
||||
@@ -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<TownyChatEvent> {
|
||||
public TownyChatReplacerCallable() {
|
||||
|
||||
}
|
||||
|
||||
public abstract String call(String match, TownyChatEvent event) throws Exception;
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<E> {
|
||||
public String call(String match, E e) throws Exception;
|
||||
}
|
||||
@@ -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<E> {
|
||||
private List<Pattern> replacementPatterns = new ArrayList<Pattern>();
|
||||
private List<Object> replacements = new ArrayList<Object>();
|
||||
|
||||
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<E>)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<TestChatEvent> replacer = new StringReplaceManager<TestChatEvent>();
|
||||
replacer.registerFormatReplacement(regex, new TestReplacerCallable());
|
||||
System.out.println(replacer.replaceAll(s, new TestChatEvent("!")));
|
||||
}
|
||||
*/
|
||||
|
||||
{
|
||||
StringReplaceManager<String> replacer = new StringReplaceManager<String>();
|
||||
replacer.registerFormatReplacement(regex, "1");
|
||||
System.out.println(replacer.replaceAll(s, "2"));
|
||||
}
|
||||
|
||||
{
|
||||
StringReplaceManager<String> replacer = new StringReplaceManager<String>();
|
||||
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<String> replacer = new StringReplaceManager<String>();
|
||||
replacer.registerFormatReplacement(regex);
|
||||
for (int i = 0; i < times; i++) {
|
||||
replacer.replaceAll(s, "2");
|
||||
}
|
||||
System.out.println("Precompiled: " + ((System.currentTimeMillis() - t) / ((double)times)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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:
|
||||
Reference in new issue
Block a user