Class NetworkMarker

java.lang.Object
com.comphenix.protocol.events.NetworkMarker
Direct Known Subclasses:
NettyNetworkMarker, NetworkMarker.EmptyBufferMarker

public abstract class NetworkMarker extends Object
Marker containing the serialized packet data seen from the network, or output handlers that will serialize the current packet.
Author:
Kristian
  • Constructor Details

    • NetworkMarker

      public NetworkMarker(@Nonnull ConnectionSide side, ByteBuffer inputBuffer, PacketType type)
      Construct a new network marker.
      Parameters:
      side - - which side this marker belongs to.
      inputBuffer - - the read serialized packet data.
      type - - packet type
    • NetworkMarker

      public NetworkMarker(@Nonnull ConnectionSide side, byte[] inputBuffer, PacketType type)
      Construct a new network marker.

      The input buffer is only non-null for client-side packets.

      Parameters:
      side - - which side this marker belongs to.
      inputBuffer - - the read serialized packet data.
      type - - packet type
  • Method Details

    • getSide

      public ConnectionSide getSide()
      Retrieve whether or not this marker belongs to a client or a server side packet.
      Returns:
      The side the parent packet belongs to.
    • getSerializer

      public StreamSerializer getSerializer()
      Retrieve a utility class for serializing and deserializing Minecraft objects.
      Returns:
      Serialization utility class.
    • getInputBuffer

      public ByteBuffer getInputBuffer()
      Retrieve the serialized packet data (excluding the header by default) from the network input stream.

      The returned buffer is read-only. If the parent event is a server side packet this method throws IllegalStateException.

      It returns NULL if the packet was transmitted by a plugin locally.

      Returns:
      A byte buffer containing the raw packet data read from the network.
    • getInputBuffer

      public ByteBuffer getInputBuffer(boolean excludeHeader)
      Retrieve the serialized packet data from the network input stream.

      The returned buffer is read-only. If the parent event is a server side packet this method throws IllegalStateException.

      It returns NULL if the packet was transmitted by a plugin locally.

      Parameters:
      excludeHeader - - whether or not to exclude the packet ID header.
      Returns:
      A byte buffer containing the raw packet data read from the network.
    • getInputStream

      public DataInputStream getInputStream()
      Retrieve the serialized packet data (excluding the header by default) as an input stream.

      The data is exactly the same as in getInputBuffer().

      Returns:
      The incoming serialized packet data as a stream, or NULL if the packet was transmitted locally.
      See Also:
      getInputBuffer()
    • getInputStream

      public DataInputStream getInputStream(boolean excludeHeader)
      Retrieve the serialized packet data as an input stream.

      The data is exactly the same as in getInputBuffer().

      Parameters:
      excludeHeader - - whether or not to exclude the packet ID header.
      Returns:
      The incoming serialized packet data as a stream, or NULL if the packet was transmitted locally.
      See Also:
      getInputBuffer()
    • requireOutputHeader

      public boolean requireOutputHeader()
      Whether or not the output handlers have to write a packet header.
      Returns:
      TRUE if they do, FALSE otherwise.
    • addOutputHandler

      public boolean addOutputHandler(@Nonnull PacketOutputHandler handler)
      Enqueue the given output handler for managing how the current packet will be written to the network stream.

      Note that output handlers are not serialized, as most consumers will probably implement them using anonymous classes. It is not safe to serialize anonymous classes, as their name depend on the order in which they are declared in the parent class.

      This can only be invoked on server side packet events.

      Parameters:
      handler - - the handler that will take part in serializing the packet.
      Returns:
      TRUE if it was added, FALSE if it has already been added.
    • removeOutputHandler

      public boolean removeOutputHandler(@Nonnull PacketOutputHandler handler)
      Remove a given output handler from the serialization queue.

      This can only be invoked on server side packet events.

      Parameters:
      handler - - the handler to remove.
      Returns:
      TRUE if the handler was removed, FALSE otherwise.
    • getOutputHandlers

      @Nonnull public Collection<PacketOutputHandler> getOutputHandlers()
      Retrieve every registered output handler in no particular order.
      Returns:
      Every registered output handler.
    • addPostListener

      public boolean addPostListener(PacketPostListener listener)
      Add a listener that is invoked after a packet has been successfully sent to the client, or received by the server.

      Received packets are not guarenteed to have been fully processed, but packets passed to PacketStream.recieveClientPacket(Player, PacketContainer) will be processed after the current packet event.

      Note that post listeners will be executed asynchronously off the main thread. They are not executed in any defined order.

      Parameters:
      listener - - the listener that will be invoked.
      Returns:
      TRUE if it was added.
    • removePostListener

      public boolean removePostListener(PacketPostListener listener)
      Remove the first instance of the given listener.
      Parameters:
      listener - - listener to remove.
      Returns:
      TRUE if it was removed, FALSE otherwise.
    • getPostListeners

      public List<PacketPostListener> getPostListeners()
      Retrieve an immutable view of all the listeners that will be invoked once the packet has been sent or received.
      Returns:
      Every post packet listener. Never NULL.
    • getScheduledPackets

      public List<ScheduledPacket> getScheduledPackets()
      Retrieve a list of packets that will be schedule (in-order) when the current packet has been successfully transmitted.

      This list is modifiable.

      Returns:
      List of packets that will be scheduled.
    • skipHeader

      protected ByteBuffer skipHeader(ByteBuffer buffer) throws IOException
      Return a byte buffer without the header in the current packet.

      It's safe to modify the position of the buffer.

      Parameters:
      buffer - - a read-only byte source.
      Returns:
      A byte buffer without the header in the current packet.
      Throws:
      IOException - If integer reading fails
    • skipHeader

      protected abstract DataInputStream skipHeader(DataInputStream input) throws IOException
      Return an input stream without the header in the current packet.

      It's safe to modify the input stream.

      Parameters:
      input - - input stream
      Returns:
      An input stream without the header
      Throws:
      IOException - If integer reading fails
    • addHeader

      protected abstract ByteBuffer addHeader(ByteBuffer buffer, PacketType type)
      Return the byte buffer prepended with the packet header.
      Parameters:
      buffer - - the read-only byte buffer.
      type - - the current packet.
      Returns:
      The byte buffer.
    • addHeader

      protected abstract DataInputStream addHeader(DataInputStream input, PacketType type)
      Return the input stream prepended with the packet header.
      Parameters:
      input - - the input stream.
      type - - the current packet.
      Returns:
      The byte buffer.
    • hasOutputHandlers

      public static boolean hasOutputHandlers(NetworkMarker marker)
      Determine if the given marker has any output handlers.
      Parameters:
      marker - - the marker to check.
      Returns:
      TRUE if it does, FALSE otherwise.
    • hasPostListeners

      public static boolean hasPostListeners(NetworkMarker marker)
      Determine if the given marker has any post listeners.
      Parameters:
      marker - - the marker to check.
      Returns:
      TRUE if it does, FALSE otherwise.
    • getByteBuffer

      public static byte[] getByteBuffer(NetworkMarker marker)
      Retrieve the byte buffer stored in the given marker.
      Parameters:
      marker - - the marker.
      Returns:
      The byte buffer, or NULL if not found.
    • getNetworkMarker

      public static NetworkMarker getNetworkMarker(PacketEvent event)
      Retrieve the network marker of a particular event without creating it.

      This is an internal method that should not be used by API users.

      Parameters:
      event - - the event.
      Returns:
      The network marker.
    • readScheduledPackets

      public static List<ScheduledPacket> readScheduledPackets(NetworkMarker marker)
      Retrieve the scheduled packets of a particular network marker without constructing the list.

      This is an internal method that should not be used by API users.

      Parameters:
      marker - - the marker.
      Returns:
      The list, or NULL if not found or initialized.