Interface PacketListenerRegistry
public interface PacketListenerRegistry
A packet listener registry is responsible for registering packet listeners and posting packet listeners to their
handler. Multiple packet listeners can be registered to a packet listener registry. If a parent registry is set, the
received packet will be posted to the parent registry first.
- Since:
- 4.0
-
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.
-
Method Details
-
parent
Get the parent listener registry of this registry or null if no parent registry is present.- Returns:
- the parent listener registry of this registry.
-
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.
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.
- Parameters:
channel- the channel the listener wants to listen to.listenerClass- the listener class to instantiate and register.- Throws:
NullPointerException- if the given listeners are null.IllegalArgumentException- if the given channel id is invalid.
-
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.
- Parameters:
channel- the channel the listener wants to listen to.listener- the listeners to register for the channel.- Throws:
NullPointerException- if the given listeners are null.IllegalArgumentException- if the given channel id is invalid.
-
removeListener
Removes the given listener from the given channel if it was registered previously.- Parameters:
channel- the id of the channel to remove the listener from.listener- the listener to remove from the channel.- Throws:
NullPointerException- if the given listeners are null.
-
removeListeners
void removeListeners(int channel) Removes all previously registered listeners from the given channel.- 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.- Parameters:
classLoader- the class loader of the packet listeners to unregister.- Throws:
NullPointerException- if the given class loader is null.
-
removeListeners
void removeListeners()Removes all listeners from this registry which are registered to any channel. -
hasListeners
boolean hasListeners(int channel) Checks if this registry has at least one listener for the specified channel.- Parameters:
channel- the channel to check.- Returns:
- true if at least one listener is registered to the channel, false otherwise.
-
channels
Get all channel ids to which a listener was registered previously.- Returns:
- all channels which have a listener in this registry.
-
listeners
Get all listener instances which are registered to any channel in this registry.- Returns:
- all listener instances registered in this registry.
-
packetListeners
Get a channel id - listeners mapping of listeners which were registered to this registry.- Returns:
- all channel ids mapped to the listeners this registry has listeners for.
-
handlePacket
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.- 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.
- Throws:
NullPointerException- if either the given channel or packet is null.
-