summaryrefslogtreecommitdiffstats
path: root/EssentialsChat
diff options
context:
space:
mode:
authorKHobbits <rob@khobbits.co.uk>2012-01-24 22:08:20 +0000
committerKHobbits <rob@khobbits.co.uk>2012-01-24 22:08:20 +0000
commitbdc695c85b97a31694c75716f33566b171a9559f (patch)
tree24bd4dda334bbbe38344beb9efb5e2d08275fa98 /EssentialsChat
parent8c54f370922b4d5f13741ee6b06ecc307e2b6e7f (diff)
downloadEssentials-bdc695c85b97a31694c75716f33566b171a9559f.tar
Essentials-bdc695c85b97a31694c75716f33566b171a9559f.tar.gz
Essentials-bdc695c85b97a31694c75716f33566b171a9559f.tar.lz
Essentials-bdc695c85b97a31694c75716f33566b171a9559f.tar.xz
Essentials-bdc695c85b97a31694c75716f33566b171a9559f.zip
First sweep over Essentials Chat, to implement 2.8 changes.
Formatting cache still needs reimplemented.
Diffstat (limited to 'EssentialsChat')
-rw-r--r--EssentialsChat/src/com/earth2me/essentials/chat/ChatStore.java2
-rw-r--r--EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java19
-rw-r--r--EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java141
-rw-r--r--EssentialsChat/src/com/earth2me/essentials/chat/EssentialsLocalChatEvent.java16
-rw-r--r--EssentialsChat/src/com/earth2me/essentials/chat/EssentialsLocalChatEventListener.java21
-rw-r--r--EssentialsChat/src/com/earth2me/essentials/chat/listenerlevel/EssentialsChatPlayerListenerHighest.java18
-rw-r--r--EssentialsChat/src/com/earth2me/essentials/chat/listenerlevel/EssentialsChatPlayerListenerLowest.java20
-rw-r--r--EssentialsChat/src/com/earth2me/essentials/chat/listenerlevel/EssentialsChatPlayerListenerNormal.java17
8 files changed, 127 insertions, 127 deletions
diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/ChatStore.java b/EssentialsChat/src/com/earth2me/essentials/chat/ChatStore.java
index bd6a9adab..b2a429e14 100644
--- a/EssentialsChat/src/com/earth2me/essentials/chat/ChatStore.java
+++ b/EssentialsChat/src/com/earth2me/essentials/chat/ChatStore.java
@@ -12,7 +12,7 @@ public class ChatStore
private final transient Trade charge;
private long radius;
- ChatStore(final IEssentials ess, final IUser user, final String type)
+ public ChatStore(final IEssentials ess, final IUser user, final String type)
{
this.user = user;
this.type = type;
diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java
index 3035a75f0..6e361beca 100644
--- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java
+++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChat.java
@@ -9,8 +9,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
-import org.bukkit.event.Event.Priority;
-import org.bukkit.event.Event.Type;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
@@ -35,17 +33,18 @@ public class EssentialsChat extends JavaPlugin
return;
}
- final Map<PlayerChatEvent, String> charges = new HashMap<PlayerChatEvent, String>();
+ final Map<PlayerChatEvent, ChatStore> chatStore = new HashMap<PlayerChatEvent, ChatStore>();
- final EssentialsChatPlayerListenerLowest playerListenerLowest = new EssentialsChatPlayerListenerLowest(getServer(), ess);
- final EssentialsChatPlayerListenerNormal playerListenerNormal = new EssentialsChatPlayerListenerNormal(getServer(), ess, charges);
- final EssentialsChatPlayerListenerHighest playerListenerHighest = new EssentialsChatPlayerListenerHighest(getServer(), ess, charges);
+ final EssentialsChatPlayerListenerLowest playerListenerLowest = new EssentialsChatPlayerListenerLowest(getServer(), ess, chatStore);
+ final EssentialsChatPlayerListenerNormal playerListenerNormal = new EssentialsChatPlayerListenerNormal(getServer(), ess, chatStore);
+ final EssentialsChatPlayerListenerHighest playerListenerHighest = new EssentialsChatPlayerListenerHighest(getServer(), ess, chatStore);
+ pluginManager.registerEvents(playerListenerLowest, this);
+ pluginManager.registerEvents(playerListenerNormal, this);
+ pluginManager.registerEvents(playerListenerHighest, this);
+
final EssentialsLocalChatEventListener localChatListener = new EssentialsLocalChatEventListener(getServer(), ess);
- pluginManager.registerEvent(Type.PLAYER_CHAT, playerListenerLowest, Priority.Lowest, this);
- pluginManager.registerEvent(Type.PLAYER_CHAT, playerListenerNormal, Priority.Normal, this);
- pluginManager.registerEvent(Type.PLAYER_CHAT, playerListenerHighest, Priority.Highest, this);
- pluginManager.registerEvent(Type.CUSTOM_EVENT, localChatListener, Priority.Highest, this);
+ pluginManager.registerEvents(localChatListener, this);
LOGGER.info(_("loadinfo", this.getDescription().getName(), this.getDescription().getVersion(), "essentials team"));
}
diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java
index d55cfe9cb..6ab845b72 100644
--- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java
+++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java
@@ -3,7 +3,9 @@ package com.earth2me.essentials.chat;
import com.earth2me.essentials.ChargeException;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.Trade;
+import com.earth2me.essentials.Util;
import com.earth2me.essentials.api.IEssentials;
+import com.earth2me.essentials.api.IGroups;
import com.earth2me.essentials.api.ISettings;
import com.earth2me.essentials.api.IUser;
import java.util.Locale;
@@ -12,119 +14,98 @@ import java.util.logging.Logger;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
+import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChatEvent;
-import org.bukkit.event.player.PlayerListener;
//TODO: Translate the local/spy tags
-public abstract class EssentialsChatPlayer extends PlayerListener
+public abstract class EssentialsChatPlayer implements Listener
{
protected transient IEssentials ess;
- protected final static Logger logger = Logger.getLogger("Minecraft");
- protected final transient Server server;
+ protected final static Logger LOGGER = Logger.getLogger("Minecraft");
+ protected final transient Server server;
+ protected final transient Map<PlayerChatEvent, ChatStore> chatStorage;
- public EssentialsChatPlayer(final Server server, final IEssentials ess)
+ public EssentialsChatPlayer(final Server server,
+ final IEssentials ess,
+ final Map<PlayerChatEvent, ChatStore> chatStorage)
{
this.ess = ess;
this.server = server;
+ this.chatStorage = chatStorage;
}
- @Override
public void onPlayerChat(final PlayerChatEvent event)
{
}
- public boolean isAborted(final PlayerChatEvent event)
+ public ChatStore getChatStore(final PlayerChatEvent event)
{
- return isAborted(event, "chat");
+ return chatStorage.get(event);
}
- public boolean isAborted(final PlayerChatEvent event, final String command)
+ public void setChatStore(final PlayerChatEvent event, final ChatStore chatStore)
{
- if (event.isCancelled())
- {
- return true;
- }
+ chatStorage.put(event, chatStore);
+ }
- final IUser user = ess.getUser(event.getPlayer());
- if (!isAffordableFor(user, command))
- {
- event.setCancelled(true);
- return true;
- }
- return false;
+ public ChatStore delChatStore(final PlayerChatEvent event)
+ {
+ return chatStorage.remove(event);
}
-
- protected void chargeChat (final PlayerChatEvent event, final Map<PlayerChatEvent, String> charges) {
-
- final IUser user = ess.getUser(event.getPlayer());
-
- String charge = charges.remove(event);
- if (charge == null)
- {
- charge = "chat";
- }
+ protected void chargeChat(final PlayerChatEvent event, final ChatStore chatStore)
+ {
try
{
- charge(user, charge);
+ charge(chatStore.getUser(), chatStore.getCharge());
}
catch (ChargeException e)
{
- ess.getCommandHandler().showCommandError(user, charge, e);
+ ess.getCommandHandler().showCommandError(chatStore.getUser(), chatStore.getLongType(), e);
event.setCancelled(true);
}
}
- protected void charge(final CommandSender sender, final String command) throws ChargeException
+ protected void charge(final CommandSender sender, final Trade charge) throws ChargeException
{
if (sender instanceof Player)
{
- final Trade charge = new Trade(command, ess);
charge.charge(ess.getUser((Player)sender));
}
}
- protected boolean isAffordableFor(final CommandSender sender, final String command)
+ protected void formatChat(final PlayerChatEvent event, final ChatStore chatStore)
{
- if (sender instanceof Player)
+ final IUser user = chatStore.getUser();
+ if (user.isAuthorized("essentials.chat.color"))
{
- try
- {
- final Trade charge = new Trade(command, ess);
- charge.isAffordableFor(ess.getUser((Player)sender));
- }
- catch (ChargeException e)
- {
- return false;
- }
+ event.setMessage(Util.stripColor(event.getMessage()));
}
- else
- {
- return false;
+ String group = user.getGroup();
+ String world = user.getWorld().getName();
+
+ IGroups groupSettings = ess.getGroups();
+ groupSettings.acquireReadLock();
+ try
+ {
+ event.setFormat(groupSettings.getChatFormat(user).format(new Object[] {group, world, world.substring(0, 1).toUpperCase(Locale.ENGLISH)}));
}
-
- return true;
- }
-
- protected void formatChat(final PlayerChatEvent event)
- {
- final IUser user = ess.getUser(event.getPlayer());
- if (user.isAuthorized("essentials.chat.color"))
+ finally
{
- event.setMessage(event.getMessage().replaceAll("&([0-9a-f])", "\u00a7$1"));
+ groupSettings.unlock();
}
- String format = ess.getGroups().getChatFormat(user);
- event.setFormat(format.replace('&', '\u00a7').replace("\u00a7\u00a7", "&").replace("{DISPLAYNAME}", "%1$s").replace("{GROUP}", user.getGroup()).replace("{MESSAGE}", "%2$s").replace("{WORLDNAME}", user.getWorld().getName()).replace("{SHORTWORLDNAME}", user.getWorld().getName().substring(0, 1).toUpperCase(Locale.ENGLISH)));
+
}
-
+
+ //TODO: Flesh this out - '?' trigger is too easily accidentally triggered
protected String getChatType(final String message)
{
switch (message.charAt(0))
{
case '!':
return "shout";
- case '?':
- return "question";
+ //case '?':
+ //return "question";
//case '@':
// return "admin";
default:
@@ -132,50 +113,48 @@ public abstract class EssentialsChatPlayer extends PlayerListener
}
}
- protected void handleLocalChat(final Map<PlayerChatEvent, String> charges, final PlayerChatEvent event)
+ protected void handleLocalChat(final PlayerChatEvent event, final ChatStore chatStore)
{
long radius = 0;
ISettings settings = ess.getSettings();
settings.acquireReadLock();
- try {
+ try
+ {
radius = settings.getData().getChat().getLocalRadius();
- } finally {
+ }
+ finally
+ {
settings.unlock();
}
radius *= radius;
+ chatStore.setRadius(radius);
- final IUser user = ess.getUser(event.getPlayer());
- final String chatType = getChatType(event.getMessage());
- final StringBuilder command = new StringBuilder();
- command.append("chat");
+ final IUser user = chatStore.getUser();
- if (event.getMessage().length() > 0 && chatType.length() > 0)
+ if (event.getMessage().length() > 1 && chatStore.getType().length() > 0)
{
- command.append("-").append(chatType);
final StringBuilder permission = new StringBuilder();
- permission.append("essentials.chat.").append(chatType);
-
- final StringBuilder format = new StringBuilder();
- format.append(chatType).append("Format");
-
- final StringBuilder errorMsg = new StringBuilder();
- errorMsg.append("notAllowedTo").append(chatType.substring(0, 1).toUpperCase(Locale.ENGLISH)).append(chatType.substring(1));
+ permission.append("essentials.chat.").append(chatStore.getType());
if (user.isAuthorized(permission.toString()))
{
+ final StringBuilder format = new StringBuilder();
+ format.append(chatStore.getType()).append("Format");
event.setMessage(event.getMessage().substring(1));
event.setFormat(_(format.toString(), event.getFormat()));
- charges.put(event, command.toString());
return;
}
+ final StringBuilder errorMsg = new StringBuilder();
+ errorMsg.append("notAllowedTo").append(chatStore.getType().substring(0, 1).toUpperCase(Locale.ENGLISH)).append(chatStore.getType().substring(1));
+
user.sendMessage(_(errorMsg.toString()));
event.setCancelled(true);
return;
}
-
+
event.setCancelled(true);
- final EssentialsLocalChatEvent localChat = new EssentialsLocalChatEvent(event, radius);
+ final EssentialsLocalChatEvent localChat = new EssentialsLocalChatEvent(event, chatStore);
ess.getServer().getPluginManager().callEvent(localChat);
}
}
diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsLocalChatEvent.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsLocalChatEvent.java
index fe605b89a..04f70aff7 100644
--- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsLocalChatEvent.java
+++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsLocalChatEvent.java
@@ -3,6 +3,7 @@ package com.earth2me.essentials.chat;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerChatEvent;
@@ -14,6 +15,7 @@ public class EssentialsLocalChatEvent extends Event implements Cancellable
private long radius;
private boolean cancelled = false;
private PlayerChatEvent parentEvent = null;
+ private static final HandlerList handlers = new HandlerList();
public EssentialsLocalChatEvent(final Player player, final String message, final String format, final long radius)
{
@@ -24,9 +26,9 @@ public class EssentialsLocalChatEvent extends Event implements Cancellable
this.radius = radius;
}
- public EssentialsLocalChatEvent(final PlayerChatEvent event, final long radius)
+ public EssentialsLocalChatEvent(final PlayerChatEvent event, final ChatStore chatStore)
{
- this(event.getPlayer(), event.getMessage(), event.getFormat(), radius);
+ this(event.getPlayer(), event.getMessage(), event.getFormat(), chatStore.getRadius());
this.parentEvent = event;
}
@@ -97,4 +99,14 @@ public class EssentialsLocalChatEvent extends Event implements Cancellable
{
return parentEvent;
}
+
+ public HandlerList getHandlers()
+ {
+ return handlers;
+ }
+
+ public static HandlerList getHandlerList()
+ {
+ return handlers;
+ }
} \ No newline at end of file
diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsLocalChatEventListener.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsLocalChatEventListener.java
index 135bfa81f..ef88472cf 100644
--- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsLocalChatEventListener.java
+++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsLocalChatEventListener.java
@@ -1,16 +1,17 @@
package com.earth2me.essentials.chat;
+import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.api.IEssentials;
import com.earth2me.essentials.api.IUser;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Player;
-import org.bukkit.event.CustomEventListener;
import org.bukkit.event.Event;
+import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
-public class EssentialsLocalChatEventListener extends CustomEventListener implements Listener {
+public class EssentialsLocalChatEventListener implements Listener {
protected transient IEssentials ess;
@@ -21,7 +22,8 @@ public class EssentialsLocalChatEventListener extends CustomEventListener implem
this.ess = ess;
this.server = server;
}
-
+
+ @EventHandler
public void onLocalChat(final EssentialsLocalChatEvent event) {
final Player sender = event.getPlayer();
if (event.getRadius() < 1)
@@ -34,7 +36,7 @@ public class EssentialsLocalChatEventListener extends CustomEventListener implem
for (Player onlinePlayer : server.getOnlinePlayers())
{
- String type = "[L]";
+ String type = _("chatTypeLocal");
final IUser user = ess.getUser(onlinePlayer);
//TODO: remove reference to op
if (user.isIgnoringPlayer(sender.getName()) && !sender.isOp())
@@ -54,7 +56,7 @@ public class EssentialsLocalChatEventListener extends CustomEventListener implem
{
if (user.isAuthorized("essentials.chat.spy"))
{
- type = type.concat("[Spy]");
+ type = type.concat(_("chatTypeSpy"));
}
else
{
@@ -66,12 +68,5 @@ public class EssentialsLocalChatEventListener extends CustomEventListener implem
final String message = String.format(event.getFormat(), type.concat(sender.getDisplayName()), event.getMessage());
user.sendMessage(message);
}
- }
-
- @Override
- public void onCustomEvent(final Event event) {
- if (event instanceof EssentialsLocalChatEvent) {
- onLocalChat((EssentialsLocalChatEvent) event);
- }
- }
+ }
} \ No newline at end of file
diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/listenerlevel/EssentialsChatPlayerListenerHighest.java b/EssentialsChat/src/com/earth2me/essentials/chat/listenerlevel/EssentialsChatPlayerListenerHighest.java
index 7868f2295..292ce3959 100644
--- a/EssentialsChat/src/com/earth2me/essentials/chat/listenerlevel/EssentialsChatPlayerListenerHighest.java
+++ b/EssentialsChat/src/com/earth2me/essentials/chat/listenerlevel/EssentialsChatPlayerListenerHighest.java
@@ -1,32 +1,34 @@
package com.earth2me.essentials.chat.listenerlevel;
import com.earth2me.essentials.api.IEssentials;
+import com.earth2me.essentials.chat.ChatStore;
import com.earth2me.essentials.chat.EssentialsChatPlayer;
import java.util.Map;
import org.bukkit.Server;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerChatEvent;
public class EssentialsChatPlayerListenerHighest extends EssentialsChatPlayer
{
- private final transient Map<PlayerChatEvent, String> charges;
-
public EssentialsChatPlayerListenerHighest(final Server server,
final IEssentials ess,
- final Map<PlayerChatEvent, String> charges)
+ final Map<PlayerChatEvent, ChatStore> chatStorage)
{
- super(server, ess);
- this.charges = charges;
+ super(server, ess, chatStorage);
}
+ @EventHandler(priority = EventPriority.HIGHEST)
@Override
public void onPlayerChat(final PlayerChatEvent event)
{
- if (isAborted(event))
+ final ChatStore chatStore = delChatStore(event);
+ if (event.isCancelled())
{
return;
}
-
- chargeChat(event, charges);
+
+ chargeChat(event, chatStore);
}
}
diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/listenerlevel/EssentialsChatPlayerListenerLowest.java b/EssentialsChat/src/com/earth2me/essentials/chat/listenerlevel/EssentialsChatPlayerListenerLowest.java
index 37394c3e6..58edc2b0f 100644
--- a/EssentialsChat/src/com/earth2me/essentials/chat/listenerlevel/EssentialsChatPlayerListenerLowest.java
+++ b/EssentialsChat/src/com/earth2me/essentials/chat/listenerlevel/EssentialsChatPlayerListenerLowest.java
@@ -1,26 +1,38 @@
package com.earth2me.essentials.chat.listenerlevel;
import com.earth2me.essentials.api.IEssentials;
+import com.earth2me.essentials.api.IUser;
+import com.earth2me.essentials.chat.ChatStore;
import com.earth2me.essentials.chat.EssentialsChatPlayer;
+import java.util.Map;
import org.bukkit.Server;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerChatEvent;
public class EssentialsChatPlayerListenerLowest extends EssentialsChatPlayer
{
- public EssentialsChatPlayerListenerLowest(final Server server, final IEssentials ess)
+ public EssentialsChatPlayerListenerLowest(final Server server,
+ final IEssentials ess,
+ final Map<PlayerChatEvent, ChatStore> chatStorage)
{
- super(server, ess);
+ super(server, ess, chatStorage);
}
+ @EventHandler(priority = EventPriority.LOWEST)
@Override
public void onPlayerChat(final PlayerChatEvent event)
{
- if (isAborted(event))
+ if (event.isCancelled())
{
return;
}
- formatChat(event);
+ final IUser user = ess.getUser(event.getPlayer());
+ final ChatStore chatStore = new ChatStore(ess, user, getChatType(event.getMessage()));
+ setChatStore(event, chatStore);
+
+ formatChat(event, chatStore);
}
} \ No newline at end of file
diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/listenerlevel/EssentialsChatPlayerListenerNormal.java b/EssentialsChat/src/com/earth2me/essentials/chat/listenerlevel/EssentialsChatPlayerListenerNormal.java
index c1a9968ef..c456b700d 100644
--- a/EssentialsChat/src/com/earth2me/essentials/chat/listenerlevel/EssentialsChatPlayerListenerNormal.java
+++ b/EssentialsChat/src/com/earth2me/essentials/chat/listenerlevel/EssentialsChatPlayerListenerNormal.java
@@ -1,32 +1,33 @@
package com.earth2me.essentials.chat.listenerlevel;
import com.earth2me.essentials.api.IEssentials;
+import com.earth2me.essentials.chat.ChatStore;
import com.earth2me.essentials.chat.EssentialsChatPlayer;
import java.util.Map;
import org.bukkit.Server;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerChatEvent;
public class EssentialsChatPlayerListenerNormal extends EssentialsChatPlayer
{
- private final transient Map<PlayerChatEvent, String> charges;
-
public EssentialsChatPlayerListenerNormal(final Server server,
final IEssentials ess,
- final Map<PlayerChatEvent, String> charges)
+ final Map<PlayerChatEvent, ChatStore> chatStorage)
{
- super(server, ess);
- this.charges = charges;
+ super(server, ess, chatStorage);
}
+ @EventHandler(priority = EventPriority.NORMAL)
@Override
public void onPlayerChat(final PlayerChatEvent event)
{
- if (isAborted(event))
+ if (event.isCancelled())
{
return;
}
-
- handleLocalChat(charges, event);
+ final ChatStore chatStore = getChatStore(event);
+ handleLocalChat(event, chatStore);
}
}