Reduce duplicate calls in EssentialsChat

Should cut the time down a little.
This commit is contained in:
KHobbits 2012-01-18 22:53:39 +00:00
parent 947bee98a0
commit cd8cc63113
6 changed files with 133 additions and 103 deletions

View File

@ -0,0 +1,51 @@
package com.earth2me.essentials.chat;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User;
public class ChatStore
{
private final User user;
private final String type;
private final Trade charge;
private long radius;
ChatStore(final IEssentials ess, final User user, final String type)
{
this.user = user;
this.type = type;
this.charge = new Trade(getLongType(), ess);
}
public User getUser()
{
return user;
}
public Trade getCharge()
{
return charge;
}
public String getType()
{
return type;
}
public String getLongType()
{
return type.length() > 0 ? "chat" : "chat-" + type;
}
public long getRadius()
{
return radius;
}
public void setRadius(long radius)
{
this.radius = radius;
}
}

View File

@ -19,7 +19,6 @@ public class EssentialsChat extends JavaPlugin
private static final Logger LOGGER = Logger.getLogger("Minecraft"); private static final Logger LOGGER = Logger.getLogger("Minecraft");
private transient Map<String, IEssentialsChatListener> chatListener; private transient Map<String, IEssentialsChatListener> chatListener;
@Override @Override
public void onEnable() public void onEnable()
{ {
@ -36,12 +35,12 @@ public class EssentialsChat extends JavaPlugin
} }
chatListener = new ConcurrentSkipListMap<String, IEssentialsChatListener>(); chatListener = new ConcurrentSkipListMap<String, IEssentialsChatListener>();
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, chatListener); final EssentialsChatPlayerListenerLowest playerListenerLowest = new EssentialsChatPlayerListenerLowest(getServer(), ess, chatListener, chatStore);
final EssentialsChatPlayerListenerNormal playerListenerNormal = new EssentialsChatPlayerListenerNormal(getServer(), ess, chatListener, charges); final EssentialsChatPlayerListenerNormal playerListenerNormal = new EssentialsChatPlayerListenerNormal(getServer(), ess, chatListener, chatStore);
final EssentialsChatPlayerListenerHighest playerListenerHighest = new EssentialsChatPlayerListenerHighest(getServer(), ess, chatListener, charges); final EssentialsChatPlayerListenerHighest playerListenerHighest = new EssentialsChatPlayerListenerHighest(getServer(), ess, chatListener, chatStore);
pluginManager.registerEvent(Type.PLAYER_CHAT, playerListenerLowest, Priority.Lowest, this); pluginManager.registerEvent(Type.PLAYER_CHAT, playerListenerLowest, Priority.Lowest, this);
pluginManager.registerEvent(Type.PLAYER_CHAT, playerListenerNormal, Priority.Normal, this); pluginManager.registerEvent(Type.PLAYER_CHAT, playerListenerNormal, Priority.Normal, this);
pluginManager.registerEvent(Type.PLAYER_CHAT, playerListenerHighest, Priority.Highest, this); pluginManager.registerEvent(Type.PLAYER_CHAT, playerListenerHighest, Priority.Highest, this);

View File

@ -22,12 +22,17 @@ public abstract class EssentialsChatPlayer extends PlayerListener
protected final static Logger logger = Logger.getLogger("Minecraft"); protected final static Logger logger = Logger.getLogger("Minecraft");
protected final transient Map<String, IEssentialsChatListener> listeners; protected final transient Map<String, IEssentialsChatListener> listeners;
protected final transient Server server; protected final transient Server server;
protected final transient Map<PlayerChatEvent, ChatStore> chatStorage;
public EssentialsChatPlayer(Server server, IEssentials ess, Map<String, IEssentialsChatListener> listeners) public EssentialsChatPlayer(final Server server,
final IEssentials ess,
final Map<String, IEssentialsChatListener> listeners,
final Map<PlayerChatEvent, ChatStore> chatStorage)
{ {
this.ess = ess; this.ess = ess;
this.listeners = listeners; this.listeners = listeners;
this.server = server; this.server = server;
this.chatStorage = chatStorage;
} }
public void onPlayerChat(final PlayerChatEvent event) public void onPlayerChat(final PlayerChatEvent event)
@ -35,11 +40,6 @@ public abstract class EssentialsChatPlayer extends PlayerListener
} }
public boolean isAborted(final PlayerChatEvent event) public boolean isAborted(final PlayerChatEvent event)
{
return isAborted(event, "chat");
}
public boolean isAborted(final PlayerChatEvent event, final String command)
{ {
if (event.isCancelled()) if (event.isCancelled())
{ {
@ -52,13 +52,6 @@ public abstract class EssentialsChatPlayer extends PlayerListener
return true; return true;
} }
} }
final User user = ess.getUser(event.getPlayer());
if (!isAffordableFor(user, command))
{
event.setCancelled(true);
return true;
}
return false; return false;
} }
@ -75,65 +68,75 @@ public abstract class EssentialsChatPlayer extends PlayerListener
} }
} }
protected void charge(final CommandSender sender, final String command) throws ChargeException public ChatStore getChatStore(final PlayerChatEvent event)
{ {
if (sender instanceof Player) return chatStorage.get(event);
{
final Trade charge = new Trade(command, ess);
charge.charge(ess.getUser((Player)sender));
}
} }
protected boolean isAffordableFor(final CommandSender sender, final String command) public void setChatStore(final PlayerChatEvent event, final ChatStore chatStore)
{ {
if (sender instanceof Player) chatStorage.put(event, chatStore);
}
public ChatStore delChatStore(final PlayerChatEvent event)
{
return chatStorage.remove(event);
}
protected void charge(final User user, final Trade charge) throws ChargeException
{
charge.charge(user);
}
protected boolean charge(final PlayerChatEvent event, final ChatStore chatStore)
{ {
try try
{ {
final Trade charge = new Trade(command, ess); charge(chatStore.getUser(), chatStore.getCharge());
charge.isAffordableFor(ess.getUser((Player)sender));
} }
catch (ChargeException e) catch (ChargeException e)
{ {
ess.showError(chatStore.getUser(), e, chatStore.getLongType());
event.setCancelled(true);
return false; return false;
} }
}
else
{
return false;
}
return true; return true;
} }
protected void sendLocalChat(final User sender, final long radius, final PlayerChatEvent event) protected void sendLocalChat(final PlayerChatEvent event, final ChatStore chatStore)
{ {
event.setCancelled(true); event.setCancelled(true);
final User sender = chatStore.getUser();
logger.info(_("localFormat", sender.getName(), event.getMessage())); logger.info(_("localFormat", sender.getName(), event.getMessage()));
final Location loc = sender.getLocation(); final Location loc = sender.getLocation();
final World world = loc.getWorld(); final World world = loc.getWorld();
if (charge(event, chatStore) == false)
{
return;
}
for (Player onlinePlayer : server.getOnlinePlayers()) for (Player onlinePlayer : server.getOnlinePlayers())
{ {
String type = "[L]"; String type = "[L]";
final User user = ess.getUser(onlinePlayer); final User onlineUser = ess.getUser(onlinePlayer);
//TODO: remove reference to op //TODO: remove reference to op
if (user.isIgnoredPlayer(sender.getName()) && !sender.isOp()) if (onlineUser.isIgnoredPlayer(sender.getName()) && !sender.isOp())
{ {
continue; continue;
} }
if (!user.equals(sender)) if (!onlineUser.equals(sender))
{ {
final Location playerLoc = user.getLocation(); final Location playerLoc = onlineUser.getLocation();
if (playerLoc.getWorld() != world) if (playerLoc.getWorld() != world)
{ {
continue; continue;
} }
final double delta = playerLoc.distanceSquared(loc); final double delta = playerLoc.distanceSquared(loc);
if (delta > radius) if (delta > chatStore.getRadius())
{ {
if (user.isAuthorized("essentials.chat.spy")) if (onlineUser.isAuthorized("essentials.chat.spy"))
{ {
type = type.concat("[Spy]"); type = type.concat("[Spy]");
} }
@ -149,7 +152,7 @@ public abstract class EssentialsChatPlayer extends PlayerListener
{ {
message = listener.modifyMessage(event, onlinePlayer, message); message = listener.modifyMessage(event, onlinePlayer, message);
} }
user.sendMessage(message); onlineUser.sendMessage(message);
} }
} }
} }

View File

@ -10,25 +10,17 @@ import org.bukkit.event.player.PlayerChatEvent;
public class EssentialsChatPlayerListenerHighest extends EssentialsChatPlayer public class EssentialsChatPlayerListenerHighest extends EssentialsChatPlayer
{ {
private final transient Map<PlayerChatEvent, String> charges;
public EssentialsChatPlayerListenerHighest(final Server server, public EssentialsChatPlayerListenerHighest(final Server server,
final IEssentials ess, final IEssentials ess,
final Map<String, IEssentialsChatListener> listeners, final Map<String, IEssentialsChatListener> listeners,
final Map<PlayerChatEvent, String> charges) final Map<PlayerChatEvent, ChatStore> chatStorage)
{ {
super(server, ess, listeners); super(server, ess, listeners, chatStorage);
this.charges = charges;
} }
@Override @Override
public void onPlayerChat(final PlayerChatEvent event) public void onPlayerChat(final PlayerChatEvent event)
{ {
String charge = charges.remove(event);
if (charge == null)
{
charge = "chat";
}
if (isAborted(event)) if (isAborted(event))
{ {
return; return;
@ -37,17 +29,6 @@ public class EssentialsChatPlayerListenerHighest extends EssentialsChatPlayer
/** /**
* This file should handle charging the user for the action before returning control back * This file should handle charging the user for the action before returning control back
*/ */
final User user = ess.getUser(event.getPlayer()); charge(event, getChatStore(event));
try
{
charge(user, charge);
}
catch (ChargeException e)
{
ess.showError(user, e, charge);
event.setCancelled(true);
return;
}
} }
} }

View File

@ -13,9 +13,10 @@ public class EssentialsChatPlayerListenerLowest extends EssentialsChatPlayer
{ {
public EssentialsChatPlayerListenerLowest(final Server server, public EssentialsChatPlayerListenerLowest(final Server server,
final IEssentials ess, final IEssentials ess,
final Map<String, IEssentialsChatListener> listeners) final Map<String, IEssentialsChatListener> listeners,
final Map<PlayerChatEvent, ChatStore> chatStorage)
{ {
super(server, ess, listeners); super(server, ess, listeners, chatStorage);
} }
@Override @Override
@ -26,14 +27,19 @@ public class EssentialsChatPlayerListenerLowest extends EssentialsChatPlayer
return; return;
} }
final User user = ess.getUser(event.getPlayer());
final ChatStore chatStore = new ChatStore(ess, user, getChatType(event.getMessage()));
setChatStore(event, chatStore);
/** /**
* This listener should apply the general chat formatting only...then return control back the event handler * This listener should apply the general chat formatting only...then return control back the event handler
*/ */
final User user = ess.getUser(event.getPlayer());
if (user.isAuthorized("essentials.chat.color")) if (user.isAuthorized("essentials.chat.color"))
{ {
event.setMessage(event.getMessage().replaceAll("&([0-9a-fk])", "\u00a7$1")); event.setMessage(event.getMessage().replaceAll("&([0-9a-fk])", "\u00a7$1"));
} else { }
else
{
event.setMessage(Util.stripColor(event.getMessage())); event.setMessage(Util.stripColor(event.getMessage()));
} }
event.setFormat(ess.getSettings().getChatFormat(user.getGroup()).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))); event.setFormat(ess.getSettings().getChatFormat(user.getGroup()).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)));

View File

@ -11,15 +11,12 @@ import org.bukkit.event.player.PlayerChatEvent;
public class EssentialsChatPlayerListenerNormal extends EssentialsChatPlayer public class EssentialsChatPlayerListenerNormal extends EssentialsChatPlayer
{ {
private final transient Map<PlayerChatEvent, String> charges;
public EssentialsChatPlayerListenerNormal(final Server server, public EssentialsChatPlayerListenerNormal(final Server server,
final IEssentials ess, final IEssentials ess,
final Map<String, IEssentialsChatListener> listeners, final Map<String, IEssentialsChatListener> listeners,
final Map<PlayerChatEvent, String> charges) final Map<PlayerChatEvent, ChatStore> chatStorage)
{ {
super(server, ess, listeners); super(server, ess, listeners, chatStorage);
this.charges = charges;
} }
@Override @Override
@ -34,46 +31,39 @@ public class EssentialsChatPlayerListenerNormal extends EssentialsChatPlayer
* This file should handle detection of the local chat features... if local chat is enabled, we need to handle * This file should handle detection of the local chat features... if local chat is enabled, we need to handle
* it here * it here
*/ */
final String chatType = getChatType(event.getMessage());
final StringBuilder command = new StringBuilder();
command.append("chat");
if (chatType.length() > 0)
{
command.append("-").append(chatType);
}
long radius = ess.getSettings().getChatRadius(); long radius = ess.getSettings().getChatRadius();
if (radius < 1) if (radius < 1)
{ {
return; return;
} }
radius *= radius; radius *= radius;
final User user = ess.getUser(event.getPlayer());
if (event.getMessage().length() > 0 && chatType.length() > 0) final ChatStore chatStore = getChatStore(event);
final User user = chatStore.getUser();
chatStore.setRadius(radius);
if (event.getMessage().length() > 0 && chatStore.getType().length() > 0)
{ {
final StringBuilder permission = new StringBuilder(); final StringBuilder permission = new StringBuilder();
permission.append("essentials.chat.").append(chatType); permission.append("essentials.chat.").append(chatStore.getType());
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));
if (user.isAuthorized(permission.toString())) if (user.isAuthorized(permission.toString()))
{ {
final StringBuilder format = new StringBuilder();
format.append(chatStore.getType()).append("Format");
event.setMessage(event.getMessage().substring(1)); event.setMessage(event.getMessage().substring(1));
event.setFormat(_(format.toString(), event.getFormat())); event.setFormat(_(format.toString(), event.getFormat()));
charges.put(event, command.toString());
return; 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())); user.sendMessage(_(errorMsg.toString()));
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
sendLocalChat(user, radius, event); sendLocalChat(event, chatStore);
} }
} }