Search in sources :

Example 1 with PacketSendEvent

use of com.bergerkiller.bukkit.common.events.PacketSendEvent 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 = false;
    if (!silentQueue.isEmpty()) {
        long time = System.currentTimeMillis();
        synchronized (silentQueue) {
            ListIterator<SilentPacket> iter = silentQueue.listIterator();
            while (iter.hasNext()) {
                SilentPacket sp = iter.next();
                if (time >= sp.timeout) {
                    iter.remove();
                } else if (sp.player == player && sp.packet == packet) {
                    is_silent = true;
                    iter.remove();
                }
            }
        }
    }
    // 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) {
                listener.onPacketSend(ev);
            }
            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

PacketSendEvent (com.bergerkiller.bukkit.common.events.PacketSendEvent)1 CommonPacket (com.bergerkiller.bukkit.common.protocol.CommonPacket)1 PacketListener (com.bergerkiller.bukkit.common.protocol.PacketListener)1 PacketType (com.bergerkiller.bukkit.common.protocol.PacketType)1