Class DefaultPacketListenerRegistry
java.lang.Object
eu.cloudnetservice.driver.network.protocol.defaults.DefaultPacketListenerRegistry
- All Implemented Interfaces:
PacketListenerRegistry
The default implementation of the packet listener registry.
- Since:
- 4.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final com.google.common.collect.Multimap<Integer, PacketListener> private final PacketListenerRegistry -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new packet listener registry instance with no parent packet listener given.Constructs a new packet listener registry instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(int channel, @NonNull PacketListener listener) Adds a packet listener for each packet that uses the provided channel id.voidaddListener(int channel, @NonNull Class<? extends PacketListener> listenerClass) Adds a packet listener for each packet that uses the provided channel id.channels()Get all channel ids to which a listener was registered previously.booleanhandlePacket(@NonNull NetworkChannel channel, @NonNull Packet packet) Handles an incoming packets and post this to all listeners which are registered to the channel the packet was sent to.booleanhasListeners(int channel) Checks if this registry has at least one listener for the specified channel.Get all listener instances which are registered to any channel in this registry.Get a channel id - listeners mapping of listeners which were registered to this registry.parent()Get the parent listener registry of this registry or null if no parent registry is present.voidremoveListener(int channel, @NonNull PacketListener listener) Removes the given listener from the given channel if it was registered previously.voidRemoves all listeners from this registry which are registered to any channel.voidremoveListeners(int channel) Removes all previously registered listeners from the given channel.voidremoveListeners(@NonNull ClassLoader classLoader) Removes all listeners from this registry whose classes were loaded by the given class loader.
-
Field Details
-
parent
-
listeners
-
-
Constructor Details
-
DefaultPacketListenerRegistry
public DefaultPacketListenerRegistry()Constructs a new packet listener registry instance with no parent packet listener given. This call is equivalent tonew DefaultPacketListenerRegistry(null). -
DefaultPacketListenerRegistry
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:
parentin interfacePacketListenerRegistry- Returns:
- the parent listener registry of this registry.
-
addListener
public void addListener(int channel, @NonNull @NonNull Class<? extends PacketListener> listenerClass) Description copied from interface:PacketListenerRegistryAdds 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:
addListenerin interfacePacketListenerRegistry- Parameters:
channel- the channel the listener wants to listen to.listenerClass- the listener class to instantiate and register.
-
addListener
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:
addListenerin interfacePacketListenerRegistry- Parameters:
channel- the channel the listener wants to listen to.listener- the listeners to register for the channel.
-
removeListener
Removes the given listener from the given channel if it was registered previously.- Specified by:
removeListenerin interfacePacketListenerRegistry- 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:
removeListenersin interfacePacketListenerRegistry- Parameters:
channel- the id of the channel to remove the listeners from.
-
removeListeners
Removes all listeners from this registry whose classes were loaded by the given class loader.- Specified by:
removeListenersin interfacePacketListenerRegistry- 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:
hasListenersin interfacePacketListenerRegistry- 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:
removeListenersin interfacePacketListenerRegistry
-
channels
Get all channel ids to which a listener was registered previously.- Specified by:
channelsin interfacePacketListenerRegistry- 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:
listenersin interfacePacketListenerRegistry- Returns:
- all listener instances registered in this registry.
-
packetListeners
@NonNull public @NonNull @UnmodifiableView Map<Integer,Collection<PacketListener>> packetListeners()Get a channel id - listeners mapping of listeners which were registered to this registry.- Specified by:
packetListenersin interfacePacketListenerRegistry- 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:
handlePacketin interfacePacketListenerRegistry- 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.
-