Search in sources :

Example 1 with PacketReceiveEvent

use of com.bergerkiller.bukkit.common.events.PacketReceiveEvent 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 2 with PacketReceiveEvent

use of com.bergerkiller.bukkit.common.events.PacketReceiveEvent in project BKCommonLib by bergerhealer.

the class PortalHandler_1_9 method enable.

@Override
public void enable(CommonPlugin plugin) {
    // Listener to disable player portal events pre-emptively while players are viewing credits
    // This is required, otherwise other plugins get very confused and teleport players out of the
    // credits screen.
    plugin.register(new Listener() {

        @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
        public void onPortalEvent(PlayerPortalEvent event) {
            if (EntityPlayerHandle.fromBukkit(event.getPlayer()).isViewingCredits()) {
                event.setCancelled(true);
            }
        }
    });
    // While the player is inside the end portal, it continuously fires a portal enter event.
    // This causes a horrible sound effect to play, because that effect is sent every tick.
    // This is obviously very unwanted while viewing the credits!
    plugin.register(new PacketListener() {

        @Override
        public void onPacketReceive(PacketReceiveEvent event) {
        }

        @Override
        public void onPacketSend(PacketSendEvent event) {
            if (EntityPlayerHandle.fromBukkit(event.getPlayer()).isViewingCredits()) {
                event.setCancelled(true);
            }
        }
    }, PacketType.OUT_WORLD_EVENT);
}
Also used : Listener(org.bukkit.event.Listener) PacketListener(com.bergerkiller.bukkit.common.protocol.PacketListener) EventHandler(org.bukkit.event.EventHandler) PlayerPortalEvent(org.bukkit.event.player.PlayerPortalEvent) PacketListener(com.bergerkiller.bukkit.common.protocol.PacketListener) PacketReceiveEvent(com.bergerkiller.bukkit.common.events.PacketReceiveEvent) PacketSendEvent(com.bergerkiller.bukkit.common.events.PacketSendEvent)

Aggregations

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