Class ProtocolManager

java.lang.Object
dev.magicmq.pyspigot.bukkit.manager.protocol.ProtocolManager

public class ProtocolManager extends Object
Manager to interface with ProtocolLib's ProtocolManager. Primarily used by scripts to register and unregister packet listeners on Bukkit/Minecraft.

Do not call this manager if ProtocolLib is not loaded and enabled on the server! It will not work.

See Also:
  • Method Details

    • getProtocolManager

      public ProtocolManager getProtocolManager()
      Get the current ProtocolLib ProtocolManager.
      Returns:
      The ProtocolManager
    • async

      public AsyncProtocolManager async()
      Get the async protocol manager for working with asynchronous listeners.
      Returns:
      The AsyncProtocolManager
    • registerPacketListener

      public ScriptPacketListener registerPacketListener(PyFunction function, PacketType type)
      Register a new packet listener with default priority.

      This method will automatically register a PacketReceivingListener or a PacketSendingListener, depending on if the PacketType is for the server or client, respectively.

      Note: This should be called from scripts only!

      Parameters:
      function - The function that should be called when the packet event occurs
      type - The packet type to listen for
      Returns:
      A ScriptPacketListener representing the packet listener that was registered
    • registerPacketListener

      public ScriptPacketListener registerPacketListener(PyFunction function, PacketType type, ListenerPriority priority)
      Register a new packet listener.

      This method will automatically register a PacketReceivingListener or a PacketSendingListener, depending on if the PacketType is for the server or client, respectively.

      Note: This should be called from scripts only!

      Parameters:
      function - The function that should be called when the packet event occurs
      type - The packet type to listen for
      priority - The priority of the packet listener relative to other packet listeners
      Returns:
      A ScriptPacketListener representing the packet listener that was registered
    • unregisterPacketListener

      public void unregisterPacketListener(ScriptPacketListener listener)
      Unregister a packet listener.

      Note: This should be called from scripts only!

      Parameters:
      listener - The packet listener to unregister
    • unregisterPacketListeners

      public void unregisterPacketListeners(dev.magicmq.pyspigot.manager.script.Script script)
      Unregister all normal packet listeners belonging to a script, excluding asynchronous packet listeners.

      Use AsyncProtocolManager.unregisterAsyncPacketListeners(Script) to unregister asynchronous packet listeners.

      Parameters:
      script - The script whose normal packet listeners should be unregistered
    • getPacketListeners

      public List<ScriptPacketListener> getPacketListeners(dev.magicmq.pyspigot.manager.script.Script script)
      Get all normal packet listeners associated with a script, excluding asynchronous packet listeners.

      Use AsyncProtocolManager.getAsyncPacketListeners(Script) to get a script's asynchronous packet listeners.

      Parameters:
      script - The script to get normal packet listeners from
      Returns:
      An immutable list of ScriptPacketListener containing all normal packet listeners associated with this script. Will return an empty list if there are no normal packet listeners associated with the script
    • getPacketListener

      public ScriptPacketListener getPacketListener(dev.magicmq.pyspigot.manager.script.Script script, PacketType packetType)
      Get the normal packet listener for a particular packet type associated with a script.

      Use AsyncProtocolManager.getAsyncPacketListener(Script, PacketType) to get a script's asynchronous packet listener of a specific packet type.

      Parameters:
      script - The script
      packetType - The packet type
      Returns:
      The ScriptPacketListener associated with the script and packet type, or null if there is none
    • createPacket

      public PacketContainer createPacket(PacketType type)
      Create a new packet with the given type. This method will assign sensible default values to all fields within the packet where a non-null value is required.

      This method is the preferred way to create a packet that will later be sent or broadcasted.

      Note: This should be called from scripts only!

      Parameters:
      type - The type of packet to create
      Returns:
      A PacketContainer representing the packet that was created.
    • sendServerPacket

      public void sendServerPacket(Player player, PacketContainer packet)
      Send a packet to a player.

      Note: This should be called from scripts only!

      Parameters:
      player - The player to send the packet to
      packet - The packet to send
    • broadcastServerPacket

      public void broadcastServerPacket(PacketContainer packet)
      Broadcast a packet to the entire server. The packet will be sent to all online players.

      Note: This should be called from scripts only!

      Parameters:
      packet - The packet to broadcast
    • broadcastServerPacket

      public void broadcastServerPacket(PacketContainer packet, Entity entity)
      Broadcast a packet to players receiving information about a particular entity. Will also broadcast the packet to the entity, if the entity is a tracker.

      Note: This should be called from scripts only!

      Parameters:
      packet - The packet to broadcast
      entity - The entity whose trackers will be informed
      See Also:
    • broadcastServerPacket

      public void broadcastServerPacket(PacketContainer packet, Entity entity, boolean includeTracker)
      Broadcast a packet to players receiving information about a particular entity.

      Usually, this would be every player in the same world within an observable distance. If the entity is a player, it will be included only if includeTracker is set to true.

      Note: This should be called from scripts only!

      Parameters:
      packet - The packet to broadcast
      entity - The entity whose trackers will be informed
      includeTracker - Whether to also transmit the packet to the entity, if it is a tracker
    • broadcastServerPacket

      public void broadcastServerPacket(PacketContainer packet, Location origin, int maxObserverDistance)
      Broadcast a packet to all players within a given max observer distance from an origin location (center point).

      Note: This should be called from scripts only!

      Parameters:
      packet - The packet to broadcast
      origin - The origin location (center point) to consider when calculating distance to each observer
      maxObserverDistance - The maximum distance from origin wherein packets will be broadcasted
    • broadcastServerPacket

      public void broadcastServerPacket(PacketContainer packet, Collection<? extends Player> targetPlayers)
      Broadcast a packet to a specified list of players.

      Note: This should be called from scripts only!

      Parameters:
      packet - The packet to broadcast
      targetPlayers - The list of players to which the packet should be broadcasted
    • get

      public static ProtocolManager get()
      Get the singleton instance of this ProtocolManager.
      Returns:
      The instance