Class PacketEvent

java.lang.Object
java.util.EventObject
com.comphenix.protocol.events.PacketEvent
All Implemented Interfaces:
Serializable, org.bukkit.event.Cancellable

public class PacketEvent extends EventObject implements org.bukkit.event.Cancellable
Represents a packet sending or receiving event. Changes to the packet will be reflected in the final version to be sent or received. It is also possible to cancel an event.
See Also:
  • Field Details

    • REPORT_CHANGING_PACKET_TYPE_IS_CONFUSING

      public static final ReportType REPORT_CHANGING_PACKET_TYPE_IS_CONFUSING
  • Constructor Details

    • PacketEvent

      public PacketEvent(Object source)
      Use the static constructors to create instances of this event.
      Parameters:
      source - - the event source.
  • Method Details

    • fromClient

      public static PacketEvent fromClient(Object source, PacketContainer packet, org.bukkit.entity.Player client)
      Creates an event representing a client packet transmission.
      Parameters:
      source - - the event source.
      packet - - the packet.
      client - - the client that sent the packet.
      Returns:
      The event.
    • fromClient

      public static PacketEvent fromClient(Object source, PacketContainer packet, NetworkMarker marker, org.bukkit.entity.Player client)
      Creates an event representing a client packet transmission.
      Parameters:
      source - - the event source.
      packet - - the packet.
      marker - - the network marker.
      client - - the client that sent the packet.
      Returns:
      The event.
    • fromClient

      public static PacketEvent fromClient(Object source, PacketContainer packet, NetworkMarker marker, org.bukkit.entity.Player client, boolean filtered)
      Creates an event representing a client packet transmission.

      If filtered is FALSE, then this event is only processed by packet monitors.

      Parameters:
      source - - the event source.
      packet - - the packet.
      marker - - the network marker.
      client - - the client that sent the packet.
      filtered - - whether or not this packet event is processed by every packet listener.
      Returns:
      The event.
    • fromServer

      public static PacketEvent fromServer(Object source, PacketContainer packet, org.bukkit.entity.Player recipient)
      Creates an event representing a server packet transmission.
      Parameters:
      source - - the event source.
      packet - - the packet.
      recipient - - the client that will receieve the packet.
      Returns:
      The event.
    • fromServer

      public static PacketEvent fromServer(Object source, PacketContainer packet, NetworkMarker marker, org.bukkit.entity.Player recipient)
      Creates an event representing a server packet transmission.
      Parameters:
      source - - the event source.
      packet - - the packet.
      marker - - the network marker.
      recipient - - the client that will receieve the packet.
      Returns:
      The event.
    • fromServer

      public static PacketEvent fromServer(Object source, PacketContainer packet, NetworkMarker marker, org.bukkit.entity.Player recipient, boolean filtered)
      Creates an event representing a server packet transmission.

      If filtered is FALSE, then this event is only processed by packet monitors.

      Parameters:
      source - - the event source.
      packet - - the packet.
      marker - - the network marker.
      recipient - - the client that will receieve the packet.
      filtered - - whether or not this packet event is processed by every packet listener.
      Returns:
      The event.
    • fromServer

      public static PacketEvent fromServer(Object source, PacketContainer packet, NetworkMarker marker, org.bukkit.entity.Player recipient, boolean filtered, @Nullable PacketEvent bundle)
      Creates an event representing a server packet transmission.

      If filtered is FALSE, then this event is only processed by packet monitors.

      Parameters:
      source - - the event source.
      packet - - the packet.
      marker - - the network marker.
      recipient - - the client that will receieve the packet.
      filtered - - whether this packet event is processed by every packet listener.
      bundle - - The corresponding packet event of the bundle if this packet is part of a bundle. Otherwise, null.
      Returns:
      The event.
    • fromSynchronous

      public static PacketEvent fromSynchronous(PacketEvent event, AsyncMarker marker)
      Create an asynchronous packet event from a synchronous event and a async marker.
      Parameters:
      event - - the original synchronous event.
      marker - - the asynchronous marker.
      Returns:
      The new packet event.
    • isAsync

      public boolean isAsync()
      Determine if we are executing the packet event in an asynchronous thread.

      If so, you must synchronize all calls to the Bukkit API.

      Generally, most server packets are executed on the main thread, whereas client packets are all executed asynchronously.

      Returns:
      TRUE if we are, FALSE otherwise.
    • getPacket

      public PacketContainer getPacket()
      Retrieves the packet that will be sent to the player.
      Returns:
      Packet to send to the player.
    • setPacket

      public void setPacket(PacketContainer packet)
      Replace the packet that will be sent to the player.
      Parameters:
      packet - - the packet that will be sent instead.
    • getPacketID

      @Deprecated public int getPacketID()
      Deprecated.
      Retrieves the packet ID.

      Deprecated: Use getPacketType() instead.

      Returns:
      The current packet ID.
    • getPacketType

      public PacketType getPacketType()
      Retrieve the packet type.
      Returns:
      The type.
    • isCancelled

      public boolean isCancelled()
      Retrieves whether or not the packet should be cancelled.
      Specified by:
      isCancelled in interface org.bukkit.event.Cancellable
      Returns:
      TRUE if it should be cancelled, FALSE otherwise.
    • setCancelled

      public void setCancelled(boolean cancel)
      Sets whether or not the packet should be cancelled. Uncancelling is possible.

      Warning: A cancelled packet should never be re-transmitted. Use the asynchronous packet manager if you need to perform extensive processing. It should also be used if you need to synchronize with the main thread.

      This ensures that other plugins can work with the same packet.

      An asynchronous listener can also delay a packet indefinitely without having to block its thread.

      Specified by:
      setCancelled in interface org.bukkit.event.Cancellable
      Parameters:
      cancel - - TRUE if it should be cancelled, FALSE otherwise.
    • getNetworkMarker

      public NetworkMarker getNetworkMarker()
      Retrieve the object responsible for managing the serialized input and output of a packet.

      Note that the serialized input data is only available for client-side packets, and the output handlers can only be applied to server-side packets.

      Returns:
      The network manager.
    • setNetworkMarker

      public void setNetworkMarker(NetworkMarker networkMarker)
      Update the network manager.

      This method is internal - do not call.

      Parameters:
      networkMarker - - the new network manager.
    • getPlayer

      public org.bukkit.entity.Player getPlayer()
      Retrieves the player that has sent the packet or is receiving it.
      Returns:
      The player associated with this event.
    • isPlayerTemporary

      public boolean isPlayerTemporary()
      Whether or not the player in this PacketEvent is temporary (i.e. they don't have a true player instance yet). Temporary players have a limited subset of methods that may be used:
      • getPlayer
      • getAddress
      • getServer
      • chat
      • sendMessage
      • kickPlayer
      • isOnline

      Anything else will throw an UnsupportedOperationException. Use this check before calling other methods when dealing with packets early in the login sequence or if you get the aforementioned exception.

      Returns:
      True if the player is temporary, false if not.
    • isFiltered

      public boolean isFiltered()
      Determine if this packet is filtered by every packet listener.

      If not, it will only be intercepted by monitor packets.

      Returns:
      TRUE if it is, FALSE otherwise.
    • isServerPacket

      public boolean isServerPacket()
      Whether or not this packet was created by the server.

      Most listeners can deduce this by noting which listener method was invoked.

      Returns:
      TRUE if the packet was created by the server, FALSE if it was created by a client.
    • getAsyncMarker

      public AsyncMarker getAsyncMarker()
      Retrieve the asynchronous marker.

      If the packet is synchronous, this marker will be used to schedule an asynchronous event. In the following asynchronous event, the marker is used to correctly pass the packet around to the different threads.

      Note that if there are no asynchronous events that can receive this packet, the marker is NULL.

      Returns:
      The current asynchronous marker, or NULL.
    • setAsyncMarker

      public void setAsyncMarker(AsyncMarker asyncMarker)
      Set the asynchronous marker.

      If the marker is non-null at the end of an synchronous event processing, the packet will be scheduled to be processed asynchronously with the given settings.

      Note that if there are no asynchronous events that can receive this packet, the marker should be NULL.

      Parameters:
      asyncMarker - - the new asynchronous marker, or NULL.
      Throws:
      IllegalStateException - If the current event is asynchronous.
    • isReadOnly

      public boolean isReadOnly()
      Determine if the current packet event is read only.

      This is used to ensure that a monitor listener doesn't accidentally alter the state of the event. However, it is still possible to modify the packet itself, as it would require too many resources to verify its integrity.

      Thus, the packet is considered immutable if the packet event is read only.

      Returns:
      TRUE if it is, FALSE otherwise.
    • setReadOnly

      public void setReadOnly(boolean readOnly)
      Set the read-only state of this packet event.

      This will be reset for every packet listener.

      Parameters:
      readOnly - - TRUE if it is read-only, FALSE otherwise.
    • isAsynchronous

      public boolean isAsynchronous()
      Determine if the packet event has been executed asynchronously or not.
      Returns:
      TRUE if this packet event is asynchronous, FALSE otherwise.
    • schedule

      public void schedule(ScheduledPacket scheduled)
      Schedule a packet for sending or receiving after the current packet event is successful.

      The packet will be added to NetworkMarker.getScheduledPackets().

      Parameters:
      scheduled - - the packet to transmit or receive.
    • unschedule

      public boolean unschedule(ScheduledPacket scheduled)
      Unschedule a specific packet.
      Parameters:
      scheduled - - the scheduled packet.
      Returns:
      TRUE if it was unscheduled, FALSE otherwise.
    • getBundle

      @Nullable public PacketEvent getBundle()
      Returns the packet event corresponding to the bundle if this packet is sent as a part of a bundle, t. Otherwise, null.
      Returns:
      Corresponding packet event or null.
    • toString

      public String toString()
      Overrides:
      toString in class EventObject