Class DefaultPacketListenerRegistry

java.lang.Object
eu.cloudnetservice.driver.network.protocol.defaults.DefaultPacketListenerRegistry
All Implemented Interfaces:
PacketListenerRegistry

public class DefaultPacketListenerRegistry extends Object implements PacketListenerRegistry
The default implementation of the packet listener registry.
Since:
4.0
  • Field Details

  • Constructor Details

    • DefaultPacketListenerRegistry

      public DefaultPacketListenerRegistry()
      Constructs a new packet listener registry instance with no parent packet listener given. This call is equivalent to new DefaultPacketListenerRegistry(null).
    • DefaultPacketListenerRegistry

      public DefaultPacketListenerRegistry(@Nullable @Nullable PacketListenerRegistry parent)
      Constructs a new packet listener registry instance.
      Parameters:
      parent - the parent registry or null if no parent registry should be set.
  • Method Details

    • parent

      Get the parent listener registry of this registry or null if no parent registry is present.
      Specified by:
      parent in interface PacketListenerRegistry
      Returns:
      the parent listener registry of this registry.
    • addListener

      public void addListener(int channel, @NonNull @NonNull Class<? extends PacketListener> listenerClass)
      Description copied from interface: PacketListenerRegistry
      Adds a packet listener for each packet that uses the provided channel id. Multiple listeners for a channel are indeed possible and will be called in the order of registration.

      The only channel to which no listener can be registered is the channel with the id -1 as it is used for query responses and therefore reserved.

      This method takes a class instead of an instance and creates the instance using our dependency injection framework. Make sure that the given class supports the instantiation using dependency injection.

      Specified by:
      addListener in interface PacketListenerRegistry
      Parameters:
      channel - the channel the listener wants to listen to.
      listenerClass - the listener class to instantiate and register.
    • addListener

      public void addListener(int channel, @NonNull @NonNull PacketListener listener)
      Adds a packet listener for each packet that uses the provided channel id. Multiple listeners for a channel are indeed possible and will be called in the order of registration.

      The only channel to which no listener can be registered is the channel with the id -1 as it is used for query responses and therefore reserved.

      Specified by:
      addListener in interface PacketListenerRegistry
      Parameters:
      channel - the channel the listener wants to listen to.
      listener - the listeners to register for the channel.
    • removeListener

      public void removeListener(int channel, @NonNull @NonNull PacketListener listener)
      Removes the given listener from the given channel if it was registered previously.
      Specified by:
      removeListener in interface PacketListenerRegistry
      Parameters:
      channel - the id of the channel to remove the listener from.
      listener - the listener to remove from the channel.
    • removeListeners

      public void removeListeners(int channel)
      Removes all previously registered listeners from the given channel.
      Specified by:
      removeListeners in interface PacketListenerRegistry
      Parameters:
      channel - the id of the channel to remove the listeners from.
    • removeListeners

      public void removeListeners(@NonNull @NonNull ClassLoader classLoader)
      Removes all listeners from this registry whose classes were loaded by the given class loader.
      Specified by:
      removeListeners in interface PacketListenerRegistry
      Parameters:
      classLoader - the class loader of the packet listeners to unregister.
    • hasListeners

      public boolean hasListeners(int channel)
      Checks if this registry has at least one listener for the specified channel.
      Specified by:
      hasListeners in interface PacketListenerRegistry
      Parameters:
      channel - the channel to check.
      Returns:
      true if at least one listener is registered to the channel, false otherwise.
    • removeListeners

      public void removeListeners()
      Removes all listeners from this registry which are registered to any channel.
      Specified by:
      removeListeners in interface PacketListenerRegistry
    • channels

      Get all channel ids to which a listener was registered previously.
      Specified by:
      channels in interface PacketListenerRegistry
      Returns:
      all channels which have a listener in this registry.
    • listeners

      Get all listener instances which are registered to any channel in this registry.
      Specified by:
      listeners in interface PacketListenerRegistry
      Returns:
      all listener instances registered in this registry.
    • packetListeners

      Get a channel id - listeners mapping of listeners which were registered to this registry.
      Specified by:
      packetListeners in interface PacketListenerRegistry
      Returns:
      all channel ids mapped to the listeners this registry has listeners for.
    • handlePacket

      public boolean handlePacket(@NonNull @NonNull NetworkChannel channel, @NonNull @NonNull Packet packet)
      Handles an incoming packets and post this to all listeners which are registered to the channel the packet was sent to. The parent listener registry (if one exists) is called before this one. The listeners are handled in a frfc (first registered, first called) order.
      Specified by:
      handlePacket in interface PacketListenerRegistry
      Parameters:
      channel - the channel from which the packet came.
      packet - the packet which was received.
      Returns:
      true if a listener handled the packet, false otherwise.