use of com.bergerkiller.bukkit.common.protocol.PacketListener in project BKCommonLib by bergerhealer.
the class PacketHandlerHooked method removePacketListeners.
@Override
public void removePacketListeners(Plugin plugin) {
// Listeners
List<PacketListener> listeners = listenerPlugins.get(plugin);
if (listeners != null) {
for (PacketListener listener : listeners) {
removePacketListener(listener, false);
}
}
// Monitors
List<PacketMonitor> monitors = monitorPlugins.get(plugin);
if (monitors != null) {
for (PacketMonitor monitor : monitors) {
removePacketMonitor(monitor, false);
}
}
}
use of com.bergerkiller.bukkit.common.protocol.PacketListener in project BKCommonLib by bergerhealer.
the class PacketHandlerHooked method addPacketListener.
@Override
public void addPacketListener(Plugin plugin, PacketListener listener, PacketType[] types) {
if (listener == null) {
throw new IllegalArgumentException("Listener is not allowed to be null");
} else if (plugin == null) {
throw new IllegalArgumentException("Plugin is not allowed to be null");
}
// Register the listener
for (PacketType type : types) {
// Map to listener array
List<PacketListener> listenerList = listeners.get(type);
if (listenerList == null) {
listenerList = new ArrayList<PacketListener>();
listeners.put(type, listenerList);
}
listenerList.add(listener);
// Map to plugin list
List<PacketListener> list = listenerPlugins.get(plugin);
if (list == null) {
list = new ArrayList<PacketListener>(2);
listenerPlugins.put(plugin, list);
}
list.add(listener);
}
}
use of com.bergerkiller.bukkit.common.protocol.PacketListener in project BKCommonLib by bergerhealer.
the class PacketHandlerHooked method getListening.
@Override
public Collection<Plugin> getListening(PacketType type) {
List<PacketListener> listenerList = listeners.get(type);
if (listenerList == null) {
return Collections.emptySet();
}
List<Plugin> plugins = new ArrayList<Plugin>();
for (Entry<Plugin, List<PacketListener>> entry : listenerPlugins.entrySet()) {
for (PacketListener listener : listenerList) {
if (entry.getValue().contains(listener)) {
plugins.add(entry.getKey());
break;
}
}
}
return plugins;
}
use of com.bergerkiller.bukkit.common.protocol.PacketListener in project BKCommonLib by bergerhealer.
the class PacketHandlerHooked method handlePacketReceive.
/**
* Handles a packet before it is being handled by the server
*
* @param player from which the packet came
* @param packet that is handled
* @param wasCancelled - True if the packet is allowed to be received, False
* if not
* @return True if the packet is allowed to be received, False if not
*/
public boolean handlePacketReceive(Player player, Object packet, boolean wasCancelled) {
if (player == null || packet == null) {
return true;
}
// Handle listeners
PacketType type = PacketType.getType(packet);
List<PacketListener> listenerList = listeners.get(type);
if (listenerList != null) {
CommonPacket cp = new CommonPacket(packet, type);
PacketReceiveEvent ev = new PacketReceiveEvent(player, cp);
ev.setCancelled(wasCancelled);
for (PacketListener listener : listenerList) {
try {
listener.onPacketReceive(ev);
} catch (Throwable t) {
Logging.LOGGER_NETWORK.log(Level.SEVERE, "Error occurred in onPacketReceive handling " + type + ":", t);
}
}
if (ev.isCancelled()) {
return false;
}
}
// Handle monitors
List<PacketMonitor> monitorList = monitors.get(type);
if (monitorList != null) {
CommonPacket cp = new CommonPacket(packet, type);
for (PacketMonitor monitor : monitorList) {
try {
monitor.onMonitorPacketReceive(cp, player);
} catch (Throwable t) {
Logging.LOGGER_NETWORK.log(Level.SEVERE, "Error occurred in onMonitorPacketReceive handling " + type + ":", t);
}
}
}
return true;
}
use of com.bergerkiller.bukkit.common.protocol.PacketListener in project BKCommonLib by bergerhealer.
the class PacketHandlerHooked method handlePacketSend.
/**
* Handles a packet before it is being sent to a player
*
* @param player for which the packet was meant
* @param packet that is handled
* @param wasCancelled - True if it was originally cancelled, False if not
* @return True if the packet is allowed to be sent, False if not
*/
public boolean handlePacketSend(Player player, Object packet, boolean wasCancelled) {
if (player == null || packet == null) {
return true;
}
// Check if silent
boolean is_silent = this.silentQueue.take(player, packet);
// Handle listeners
PacketType type = PacketType.getType(packet);
if (!is_silent) {
List<PacketListener> listenerList = listeners.get(type);
if (listenerList != null) {
CommonPacket cp = new CommonPacket(packet, type);
PacketSendEvent ev = new PacketSendEvent(player, cp);
ev.setCancelled(wasCancelled);
for (PacketListener listener : listenerList) {
try {
listener.onPacketSend(ev);
} catch (Throwable t) {
Logging.LOGGER_NETWORK.log(Level.SEVERE, "Error occurred in onPacketSend handling " + type + ":", t);
}
}
if (ev.isCancelled()) {
return false;
}
}
}
// Handle monitors
handlePacketSendMonitor(player, type, packet);
return true;
}
Aggregations