Search in sources :

Example 1 with PacketListener

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

Example 2 with PacketListener

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

Example 3 with PacketListener

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;
}
Also used : PacketListener(com.bergerkiller.bukkit.common.protocol.PacketListener) Plugin(org.bukkit.plugin.Plugin)

Example 4 with PacketListener

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;
}
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)

Example 5 with PacketListener

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

Aggregations

PacketListener (com.bergerkiller.bukkit.common.protocol.PacketListener)6 PacketType (com.bergerkiller.bukkit.common.protocol.PacketType)3 PacketReceiveEvent (com.bergerkiller.bukkit.common.events.PacketReceiveEvent)2 PacketSendEvent (com.bergerkiller.bukkit.common.events.PacketSendEvent)2 CommonPacket (com.bergerkiller.bukkit.common.protocol.CommonPacket)2 PacketMonitor (com.bergerkiller.bukkit.common.protocol.PacketMonitor)2 EventHandler (org.bukkit.event.EventHandler)1 Listener (org.bukkit.event.Listener)1 PlayerPortalEvent (org.bukkit.event.player.PlayerPortalEvent)1 Plugin (org.bukkit.plugin.Plugin)1