Search in sources :

Example 1 with PacketMonitor

use of com.bergerkiller.bukkit.common.protocol.PacketMonitor 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);
        }
    }
}
Also used : PacketMonitor(com.bergerkiller.bukkit.common.protocol.PacketMonitor) PacketListener(com.bergerkiller.bukkit.common.protocol.PacketListener)

Example 2 with PacketMonitor

use of com.bergerkiller.bukkit.common.protocol.PacketMonitor in project BKCommonLib by bergerhealer.

the class PacketHandlerHooked method addPacketMonitor.

@Override
public void addPacketMonitor(Plugin plugin, PacketMonitor monitor, PacketType[] types) {
    if (monitor == null) {
        throw new IllegalArgumentException("Monitor 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<PacketMonitor> monitorList = monitors.get(type);
        if (monitorList == null) {
            monitorList = new ArrayList<PacketMonitor>();
            monitors.put(type, monitorList);
        }
        monitorList.add(monitor);
        // Map to plugin list
        List<PacketMonitor> list = monitorPlugins.get(plugin);
        if (list == null) {
            list = new ArrayList<PacketMonitor>(2);
            monitorPlugins.put(plugin, list);
        }
        list.add(monitor);
    }
}
Also used : PacketMonitor(com.bergerkiller.bukkit.common.protocol.PacketMonitor) PacketType(com.bergerkiller.bukkit.common.protocol.PacketType)

Example 3 with PacketMonitor

use of com.bergerkiller.bukkit.common.protocol.PacketMonitor 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;
}
Also used : PacketMonitor(com.bergerkiller.bukkit.common.protocol.PacketMonitor) PacketType(com.bergerkiller.bukkit.common.protocol.PacketType) CommonPacket(com.bergerkiller.bukkit.common.protocol.CommonPacket) PacketListener(com.bergerkiller.bukkit.common.protocol.PacketListener) PacketReceiveEvent(com.bergerkiller.bukkit.common.events.PacketReceiveEvent)

Aggregations

PacketMonitor (com.bergerkiller.bukkit.common.protocol.PacketMonitor)3 PacketListener (com.bergerkiller.bukkit.common.protocol.PacketListener)2 PacketType (com.bergerkiller.bukkit.common.protocol.PacketType)2 PacketReceiveEvent (com.bergerkiller.bukkit.common.events.PacketReceiveEvent)1 CommonPacket (com.bergerkiller.bukkit.common.protocol.CommonPacket)1