Search in sources :

Example 1 with PacketType

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

the class VehicleMountHandler_BaseImpl method handlePacketSend.

/**
 * Call this to handle a relevant packet that was sent from the server to the client
 *
 * @param packet The packet sent
 */
public final void handlePacketSend(CommonPacket packet) {
    synchronizeAndQueuePackets(() -> {
        // Refresh player dimension if none could be set (temporary player, pre-join)
        if (this._playerDimension == null) {
            this._playerDimension = PlayerUtil.getPlayerDimension(this._player);
        }
        // Event handler for further implementations
        onPacketSend(packet);
        // Handle packets
        PacketType type = packet.getType();
        if (type == PacketType.OUT_ENTITY_DESTROY) {
            PacketPlayOutEntityDestroyHandle dp = PacketPlayOutEntityDestroyHandle.createHandle(packet.getHandle());
            if (dp.hasMultipleEntityIds()) {
                for (int entityId : dp.getEntityIds()) {
                    handleDespawn(entityId);
                }
            } else {
                handleDespawn(dp.getSingleEntityId());
            }
        } else if (type == PacketType.OUT_RESPAWN) {
            DimensionType dimension;
            try {
                dimension = packet.read(PacketType.OUT_RESPAWN.dimensionType);
            } catch (IllegalArgumentException ex) {
                // Logging.LOGGER_NETWORK.log(Level.WARNING, "Failed to decide dimension from respawn packet", ex);
                dimension = null;
            }
            if (dimension != null && !dimension.equals(this._playerDimension)) {
                this._playerDimension = dimension;
                handleReset();
            }
        } else if (type == PacketType.OUT_CAMERA) {
            // Called when vanilla/server/Bukkit starts or stops spectating an Entity
            // We remember this choice for later
            this._vanillaSpectatedEntity = packet.read(PacketType.OUT_CAMERA.entityId);
            // If currently spectating something already, override
            int len = this._spectatorStack.length;
            if (len > 0) {
                packet.write(PacketType.OUT_CAMERA.entityId, this._spectatorStack[len - 1]);
            }
        } else {
            if (this.isPositionTracked()) {
                // Also decode position
                if (type == PacketType.OUT_ENTITY_SPAWN) {
                    PacketPlayOutSpawnEntityHandle handle = PacketPlayOutSpawnEntityHandle.createHandle(packet.getHandle());
                    handleSpawn(handle.getEntityId(), handle.getCommonEntityType(), new Vector(handle.getPosX(), handle.getPosY(), handle.getPosZ()));
                } else if (type == PacketType.OUT_ENTITY_SPAWN_LIVING) {
                    PacketPlayOutSpawnEntityLivingHandle handle = PacketPlayOutSpawnEntityLivingHandle.createHandle(packet.getHandle());
                    handleSpawn(handle.getEntityId(), handle.getCommonEntityType(), new Vector(handle.getPosX(), handle.getPosY(), handle.getPosZ()));
                } else if (type == PacketType.OUT_ENTITY_SPAWN_NAMED) {
                    PacketPlayOutNamedEntitySpawnHandle handle = PacketPlayOutNamedEntitySpawnHandle.createHandle(packet.getHandle());
                    handleSpawn(handle.getEntityId(), CommonEntityType.PLAYER, new Vector(handle.getPosX(), handle.getPosY(), handle.getPosZ()));
                } else if (type == PacketType.OUT_ENTITY_TELEPORT) {
                    PacketPlayOutEntityTeleportHandle handle = PacketPlayOutEntityTeleportHandle.createHandle(packet.getHandle());
                    handleMove(handle.getEntityId(), (position) -> {
                        position.setX(handle.getPosX());
                        position.setY(handle.getPosY());
                        position.setZ(handle.getPosZ());
                    });
                } else if (type == PacketType.OUT_ENTITY_MOVE || type == PacketType.OUT_ENTITY_MOVE_LOOK) {
                    PacketPlayOutEntityHandle handle = PacketPlayOutEntityHandle.createHandle(packet.getHandle());
                    handleMove(handle.getEntityId(), (position) -> {
                        position.setX(position.getX() + handle.getDeltaX());
                        position.setY(position.getY() + handle.getDeltaY());
                        position.setZ(position.getZ() + handle.getDeltaZ());
                    });
                }
            } else {
                // No decoding/tracking of position
                if (type == PacketType.OUT_ENTITY_SPAWN) {
                    PacketPlayOutSpawnEntityHandle handle = PacketPlayOutSpawnEntityHandle.createHandle(packet.getHandle());
                    handleSpawn(handle.getEntityId(), handle.getCommonEntityType(), null);
                } else if (type == PacketType.OUT_ENTITY_SPAWN_LIVING) {
                    PacketPlayOutSpawnEntityLivingHandle handle = PacketPlayOutSpawnEntityLivingHandle.createHandle(packet.getHandle());
                    handleSpawn(handle.getEntityId(), handle.getCommonEntityType(), null);
                } else if (type == PacketType.OUT_ENTITY_SPAWN_NAMED) {
                    PacketPlayOutNamedEntitySpawnHandle handle = PacketPlayOutNamedEntitySpawnHandle.createHandle(packet.getHandle());
                    handleSpawn(handle.getEntityId(), CommonEntityType.PLAYER, null);
                }
            }
        }
    });
}
Also used : Arrays(java.util.Arrays) PacketPlayOutCameraHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutCameraHandle) PacketPlayOutEntityDestroyHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutEntityDestroyHandle) CommonPlugin(com.bergerkiller.bukkit.common.internal.CommonPlugin) PacketPlayOutSpawnEntityHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutSpawnEntityHandle) Player(org.bukkit.entity.Player) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) PacketPlayOutSpawnEntityLivingHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutSpawnEntityLivingHandle) MathUtil(com.bergerkiller.bukkit.common.utils.MathUtil) VehicleMountController(com.bergerkiller.bukkit.common.controller.VehicleMountController) CommonEntityType(com.bergerkiller.bukkit.common.entity.CommonEntityType) DimensionType(com.bergerkiller.bukkit.common.resources.DimensionType) PacketType(com.bergerkiller.bukkit.common.protocol.PacketType) PacketPlayOutEntityHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutEntityHandle) CommonPacket(com.bergerkiller.bukkit.common.protocol.CommonPacket) IntHashMap(com.bergerkiller.bukkit.common.wrappers.IntHashMap) PacketUtil(com.bergerkiller.bukkit.common.utils.PacketUtil) Logging(com.bergerkiller.bukkit.common.Logging) PacketPlayOutEntityTeleportHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutEntityTeleportHandle) PlayerUtil(com.bergerkiller.bukkit.common.utils.PlayerUtil) Consumer(java.util.function.Consumer) Vector(org.bukkit.util.Vector) PacketPlayOutNamedEntitySpawnHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutNamedEntitySpawnHandle) List(java.util.List) PacketPlayOutMountHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutMountHandle) PacketHandle(com.bergerkiller.generated.net.minecraft.network.protocol.PacketHandle) Queue(java.util.Queue) Collections(java.util.Collections) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) DimensionType(com.bergerkiller.bukkit.common.resources.DimensionType) PacketPlayOutEntityHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutEntityHandle) PacketType(com.bergerkiller.bukkit.common.protocol.PacketType) PacketPlayOutSpawnEntityHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutSpawnEntityHandle) PacketPlayOutEntityDestroyHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutEntityDestroyHandle) Vector(org.bukkit.util.Vector) PacketPlayOutEntityTeleportHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutEntityTeleportHandle) PacketPlayOutNamedEntitySpawnHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutNamedEntitySpawnHandle) PacketPlayOutSpawnEntityLivingHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutSpawnEntityLivingHandle)

Example 2 with PacketType

use of com.bergerkiller.bukkit.common.protocol.PacketType 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 PacketType

use of com.bergerkiller.bukkit.common.protocol.PacketType 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 4 with PacketType

use of com.bergerkiller.bukkit.common.protocol.PacketType 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 PacketType

use of com.bergerkiller.bukkit.common.protocol.PacketType 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

PacketType (com.bergerkiller.bukkit.common.protocol.PacketType)5 CommonPacket (com.bergerkiller.bukkit.common.protocol.CommonPacket)3 PacketListener (com.bergerkiller.bukkit.common.protocol.PacketListener)3 PacketMonitor (com.bergerkiller.bukkit.common.protocol.PacketMonitor)2 Logging (com.bergerkiller.bukkit.common.Logging)1 VehicleMountController (com.bergerkiller.bukkit.common.controller.VehicleMountController)1 CommonEntityType (com.bergerkiller.bukkit.common.entity.CommonEntityType)1 PacketReceiveEvent (com.bergerkiller.bukkit.common.events.PacketReceiveEvent)1 PacketSendEvent (com.bergerkiller.bukkit.common.events.PacketSendEvent)1 CommonPlugin (com.bergerkiller.bukkit.common.internal.CommonPlugin)1 DimensionType (com.bergerkiller.bukkit.common.resources.DimensionType)1 MathUtil (com.bergerkiller.bukkit.common.utils.MathUtil)1 PacketUtil (com.bergerkiller.bukkit.common.utils.PacketUtil)1 PlayerUtil (com.bergerkiller.bukkit.common.utils.PlayerUtil)1 IntHashMap (com.bergerkiller.bukkit.common.wrappers.IntHashMap)1 PacketHandle (com.bergerkiller.generated.net.minecraft.network.protocol.PacketHandle)1 PacketPlayOutCameraHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutCameraHandle)1 PacketPlayOutEntityDestroyHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutEntityDestroyHandle)1 PacketPlayOutEntityHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutEntityHandle)1 PacketPlayOutEntityTeleportHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutEntityTeleportHandle)1