Remove old chat hook

Use Bukkit API for local chat handling
Implement localChatSpy event
This commit is contained in:
KHobbits 2013-07-13 17:24:40 +01:00
parent f5624d4cb6
commit ef3b052c64
24 changed files with 224 additions and 174 deletions

View File

@ -519,3 +519,4 @@ inventoryClearingAllStack=\u00a76Cleared all\u00a7c {0} \u00a76from {1}\u00a76.
inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76from {2}\u00a76.
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=

View File

@ -525,3 +525,4 @@ inventoryClearingStack=\u00a76Odstraneno\u00a7c {0}\u00a7cz {1} \u00a76od {2}\u0
inventoryClearFail=\u00a74Hrac {0} \u00a74nema\u00a7c {1} \u00a74z\u00a7c {2}\u00a74.
localNoOne=

View File

@ -520,3 +520,4 @@ inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76fro
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=

View File

@ -520,3 +520,4 @@ inventoryClearingStack=\u00a7c {0} {1} \u00a76von {2} \u00a76entfernt.
inventoryClearFail=\u00a74Spieler {0} \u00a74hat keine\u00a7c {1} {2}\u00a74.
localNoOne=

View File

@ -519,3 +519,4 @@ inventoryClearingAllStack=\u00a76Cleared all\u00a7c {0} \u00a76from {1}\u00a76.
inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76from {2}\u00a76.
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=

View File

@ -520,3 +520,4 @@ inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76fro
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=

View File

@ -520,3 +520,4 @@ inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76fro
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=

View File

@ -520,3 +520,4 @@ inventoryClearingStack=\u00a7c{0} \u00a7c {1} \u00a76ont \u00e9t\u00e9 supprim\u
inventoryClearFail=\u00a74Le joueur {0} \u00a74n''a pas\u00a7c {1}\u00a7c {2}\u00a74 sur lui.
localNoOne=

View File

@ -520,3 +520,4 @@ inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76fro
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=

View File

@ -520,3 +520,4 @@ inventoryClearingStack=\u00a76\u00a7c {0} \u00a76stuks\u00a7c {1} \u00a76zijn ve
inventoryClearFail=\u00a74Player {0} \u00a74heeft geen\u00a7c {1} \u00a74stuks\u00a7c {2}\u00a74.
localNoOne=

View File

@ -520,3 +520,4 @@ inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76fro
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=

View File

@ -520,3 +520,4 @@ inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76fro
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=

View File

@ -520,3 +520,4 @@ inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76fro
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=

View File

@ -520,3 +520,4 @@ inventoryClearingStack=\u00a76Tog bort \u00a7c {0} \u00a76av\u00a7c {1} \u00a76f
inventoryClearFail=\u00a74Spelaren {0} \u00a74har inte \u00a7c {1} \u00a74av\u00a7c {2}\u00a74.
localNoOne=

View File

@ -520,3 +520,4 @@ inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76fro
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=

View File

@ -520,3 +520,4 @@ inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76fro
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=

View File

@ -520,3 +520,4 @@ inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76fro
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=

View File

@ -1,5 +1,9 @@
package net.ess3.api.events;
import static com.earth2me.essentials.I18n._;
import java.util.IllegalFormatException;
import java.util.Set;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
@ -8,11 +12,107 @@ import org.bukkit.event.HandlerList;
public class LocalChatSpyEvent extends Event implements Cancellable
{
private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private boolean cancelled = false;
private String message;
private String format;
private Player player;
private final Set<Player> recipients;
public LocalChatSpyEvent()
public LocalChatSpyEvent(final boolean async, final Player who, final String format, final String message, final Set<Player> players)
{
super(async);
this.format = _("chatTypeSpy").concat(format);
this.message = message;
recipients = players;
player = who;
}
/**
* Gets the message that the player is attempting to send. This message will be used with {@link #getFormat()}.
*
* @return Message the player is attempting to send
*/
public String getMessage()
{
return message;
}
/**
* Sets the message that the player will send. This message will be used with {@link #getFormat()}.
*
* @param message New message that the player will send
*/
public void setMessage(String message)
{
this.message = message;
}
/**
* Gets the format to use to display this chat message. When this event finishes execution, the first format
* parameter is the {@link Player#getDisplayName()} and the second parameter is {@link #getMessage()}
*
* @return {@link String#format(String, Object...)} compatible format string
*/
public String getFormat()
{
return format;
}
/**
* Sets the format to use to display this chat message. When this event finishes execution, the first format
* parameter is the {@link Player#getDisplayName()} and the second parameter is {@link #getMessage()}
*
* @param format {@link String#format(String, Object...)} compatible format string
* @throws IllegalFormatException if the underlying API throws the exception
* @throws NullPointerException if format is null
* @see String#format(String, Object...)
*/
public void setFormat(final String format) throws IllegalFormatException, NullPointerException
{
// Oh for a better way to do this!
try
{
String.format(format, player, message);
}
catch (RuntimeException ex)
{
ex.fillInStackTrace();
throw ex;
}
this.format = format;
}
/**
* Gets a set of recipients that this chat message will be displayed to.
*
* @return All Players who will see this chat message
*/
public Set<Player> getRecipients()
{
return recipients;
}
/**
* Returns the player involved in this event
*
* @return Player who is involved in this event
*/
public final Player getPlayer()
{
return player;
}
@Override
public boolean isCancelled()
{
return cancelled;
}
@Override
public void setCancelled(boolean cancel)
{
this.cancelled = cancel;
}
@Override
@ -25,16 +125,4 @@ public class LocalChatSpyEvent extends Event implements Cancellable
{
return handlers;
}
@Override
public boolean isCancelled()
{
return cancelled;
}
@Override
public void setCancelled(boolean cancelled)
{
this.cancelled = cancelled;
}
}

View File

@ -16,7 +16,6 @@ import org.bukkit.plugin.java.JavaPlugin;
public class EssentialsChat extends JavaPlugin
{
private static final Logger LOGGER = Logger.getLogger("Minecraft");
private transient Map<String, IEssentialsChatListener> chatListener;
@Override
public void onEnable()
@ -33,35 +32,16 @@ public class EssentialsChat extends JavaPlugin
return;
}
chatListener = new ConcurrentSkipListMap<String, IEssentialsChatListener>();
final Map<AsyncPlayerChatEvent, ChatStore> chatStore = Collections.synchronizedMap(new HashMap<AsyncPlayerChatEvent, ChatStore>());
final EssentialsChatPlayerListenerLowest playerListenerLowest = new EssentialsChatPlayerListenerLowest(getServer(), ess, chatListener, chatStore);
final EssentialsChatPlayerListenerNormal playerListenerNormal = new EssentialsChatPlayerListenerNormal(getServer(), ess, chatListener, chatStore);
final EssentialsChatPlayerListenerHighest playerListenerHighest = new EssentialsChatPlayerListenerHighest(getServer(), ess, chatListener, chatStore);
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);
}
@Override
public void onDisable()
{
if (chatListener != null)
{
chatListener.clear();
}
}
public void addEssentialsChatListener(final String plugin, final IEssentialsChatListener listener)
{
chatListener.put(plugin, listener);
}
public IEssentialsChatListener removeEssentialsChatListener(final String plugin)
{
return chatListener.remove(plugin);
}
}

View File

@ -19,17 +19,14 @@ public abstract class EssentialsChatPlayer implements Listener
{
protected transient IEssentials ess;
protected final static Logger logger = Logger.getLogger("Minecraft");
protected final transient Map<String, IEssentialsChatListener> listeners;
protected final transient Server server;
protected final transient Map<AsyncPlayerChatEvent, ChatStore> chatStorage;
public EssentialsChatPlayer(final Server server,
final IEssentials ess,
final Map<String, IEssentialsChatListener> listeners,
final IEssentials ess,
final Map<AsyncPlayerChatEvent, ChatStore> chatStorage)
{
this.ess = ess;
this.listeners = listeners;
this.server = server;
this.chatStorage = chatStorage;
}
@ -43,31 +40,7 @@ public abstract class EssentialsChatPlayer implements Listener
if (event.isCancelled())
{
return true;
}
synchronized (listeners)
{
for (Map.Entry<String, IEssentialsChatListener> listener : listeners.entrySet())
{
try
{
if (listener.getValue().shouldHandleThisChat(event))
{
return true;
}
}
catch (Throwable t)
{
if (ess.getSettings().isDebug())
{
logger.log(Level.WARNING, "Error with EssentialsChat listener of " + listener.getKey() + ": " + t.getMessage(), t);
}
else
{
logger.log(Level.WARNING, "Error with EssentialsChat listener of " + listener.getKey() + ": " + t.getMessage());
}
}
}
}
}
return false;
}
@ -120,76 +93,4 @@ public abstract class EssentialsChatPlayer implements Listener
}
return true;
}
protected void sendLocalChat(final AsyncPlayerChatEvent event, final ChatStore chatStore)
{
event.setCancelled(true);
final User sender = chatStore.getUser();
logger.info(_("localFormat", sender.getName(), event.getMessage()));
final Location loc = sender.getLocation();
final World world = loc.getWorld();
if (charge(event, chatStore) == false)
{
return;
}
for (Player onlinePlayer : event.getRecipients())
{
String type = _("chatTypeLocal");
final User onlineUser = ess.getUser(onlinePlayer);
if (!onlineUser.equals(sender))
{
boolean abort = false;
final Location playerLoc = onlineUser.getLocation();
if (playerLoc.getWorld() != world)
{
abort = true;
}
else
{
final double delta = playerLoc.distanceSquared(loc);
if (delta > chatStore.getRadius())
{
abort = true;
}
}
if (abort)
{
if (onlineUser.isAuthorized("essentials.chat.spy"))
{
type = type.concat(_("chatTypeSpy"));
}
else
{
continue;
}
}
}
String message = type.concat(String.format(event.getFormat(), sender.getDisplayName(), event.getMessage()));
synchronized (listeners)
{
for (Map.Entry<String, IEssentialsChatListener> listener : listeners.entrySet())
{
try
{
message = listener.getValue().modifyMessage(event, onlinePlayer, message);
}
catch (Throwable t)
{
if (ess.getSettings().isDebug())
{
logger.log(Level.WARNING, "Error with EssentialsChat listener of " + listener.getKey() + ": " + t.getMessage(), t);
}
else
{
logger.log(Level.WARNING, "Error with EssentialsChat listener of " + listener.getKey() + ": " + t.getMessage());
}
}
}
}
onlineUser.sendMessage(message);
}
}
}

View File

@ -12,10 +12,9 @@ public class EssentialsChatPlayerListenerHighest extends EssentialsChatPlayer
{
public EssentialsChatPlayerListenerHighest(final Server server,
final IEssentials ess,
final Map<String, IEssentialsChatListener> listeners,
final Map<AsyncPlayerChatEvent, ChatStore> chatStorage)
{
super(server, ess, listeners, chatStorage);
super(server, ess, chatStorage);
}
@EventHandler(priority = EventPriority.HIGHEST)

View File

@ -19,10 +19,9 @@ public class EssentialsChatPlayerListenerLowest extends EssentialsChatPlayer
{
public EssentialsChatPlayerListenerLowest(final Server server,
final IEssentials ess,
final Map<String, IEssentialsChatListener> listeners,
final Map<AsyncPlayerChatEvent, ChatStore> chatStorage)
{
super(server, ess, listeners, chatStorage);
super(server, ess, chatStorage);
}
@EventHandler(priority = EventPriority.LOWEST)
@ -36,11 +35,12 @@ public class EssentialsChatPlayerListenerLowest extends EssentialsChatPlayer
final User user = ess.getUser(event.getPlayer());
if (user == null) {
if (user == null)
{
event.setCancelled(true);
return;
}
final ChatStore chatStore = new ChatStore(ess, user, getChatType(event.getMessage()));
setChatStore(event, chatStore);
@ -51,16 +51,16 @@ public class EssentialsChatPlayerListenerLowest extends EssentialsChatPlayer
String group = user.getGroup();
String world = user.getWorld().getName();
Team team = user.getScoreboard().getPlayerTeam(user.getBase());
String format = ess.getSettings().getChatFormat(group);
format = format.replace("{0}", group);
format = format.replace("{1}", world);
format = format.replace("{2}", world.substring(0, 1).toUpperCase(Locale.ENGLISH));
format = format.replace("{3}", team == null ? "" : team.getPrefix());
format = format.replace("{4}", team == null ? "" : team.getSuffix());
format = format.replace("{5}", team == null ? "" : team.getDisplayName());
format = format.replace("{3}", team == null ? "" : team.getPrefix());
format = format.replace("{4}", team == null ? "" : team.getSuffix());
format = format.replace("{5}", team == null ? "" : team.getDisplayName());
synchronized (format)
{
{
event.setFormat(format);
}
}

View File

@ -3,9 +3,17 @@ package com.earth2me.essentials.chat;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User;
import static com.earth2me.essentials.chat.EssentialsChatPlayer.logger;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import net.ess3.api.events.LocalChatSpyEvent;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.AsyncPlayerChatEvent;
@ -15,12 +23,11 @@ public class EssentialsChatPlayerListenerNormal extends EssentialsChatPlayer
{
public EssentialsChatPlayerListenerNormal(final Server server,
final IEssentials ess,
final Map<String, IEssentialsChatListener> listeners,
final Map<AsyncPlayerChatEvent, ChatStore> chatStorage)
{
super(server, ess, listeners, chatStorage);
super(server, ess, chatStorage);
}
@EventHandler(priority = EventPriority.NORMAL)
@Override
public void onPlayerChat(final AsyncPlayerChatEvent event)
@ -33,40 +40,110 @@ public class EssentialsChatPlayerListenerNormal extends EssentialsChatPlayer
/**
* This file should handle detection of the local chat features... if local chat is enabled, we need to handle
* it here
*/
*/
long radius = ess.getSettings().getChatRadius();
if (radius < 1)
{
return;
}
radius *= radius;
final ChatStore chatStore = getChatStore(event);
final User user = chatStore.getUser();
chatStore.setRadius(radius);
if (event.getMessage().length() > 1 && chatStore.getType().length() > 0)
{
final StringBuilder permission = new StringBuilder();
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()));
event.setFormat(_(format.toString(), event.getFormat()));
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;
}
sendLocalChat(event, chatStore);
final Location loc = user.getLocation();
final World world = loc.getWorld();
if (charge(event, chatStore) == false)
{
return;
}
Set<Player> outList = event.getRecipients();
Set<Player> spyList = new HashSet<Player>();
try
{
outList.add(event.getPlayer());
}
catch (UnsupportedOperationException ex)
{
if (ess.getSettings().isDebug())
{
ess.getLogger().info("Plugin triggered custom chat event, local chat handling aborted.");
}
return;
}
String type = _("chatTypeLocal");
event.setFormat(type.concat(event.getFormat()));
logger.info(_("localFormat", user.getName(), event.getMessage()));
final Iterator<Player> it = outList.iterator();
while (it.hasNext())
{
final Player onlinePlayer = it.next();
final User onlineUser = ess.getUser(onlinePlayer);
if (!onlineUser.equals(user))
{
boolean abort = false;
final Location playerLoc = onlineUser.getLocation();
if (playerLoc.getWorld() != world)
{
abort = true;
}
else
{
final double delta = playerLoc.distanceSquared(loc);
if (delta > chatStore.getRadius())
{
abort = true;
}
}
if (abort)
{
if (onlineUser.isAuthorized("essentials.chat.spy"))
{
spyList.add(onlinePlayer);
}
outList.remove(onlinePlayer);
}
}
}
LocalChatSpyEvent spyEvent = new LocalChatSpyEvent(event.isAsynchronous(), event.getPlayer(), event.getFormat(), event.getMessage(), spyList);
server.getPluginManager().callEvent(spyEvent);
if (!spyEvent.isCancelled())
{
for (Player onlinePlayer : spyEvent.getRecipients())
{
onlinePlayer.sendMessage(String.format(event.getFormat(), user.getDisplayName(), event.getMessage()));
}
}
}
}

View File

@ -1,12 +0,0 @@
package com.earth2me.essentials.chat;
import org.bukkit.entity.Player;
import org.bukkit.event.player.AsyncPlayerChatEvent;
public interface IEssentialsChatListener
{
boolean shouldHandleThisChat(AsyncPlayerChatEvent event);
String modifyMessage(AsyncPlayerChatEvent event, Player target, String message);
}