-
Notifications
You must be signed in to change notification settings - Fork 24
Creating an AttributeListener
What is an AttributeListener?
An AttributeListener is a listener that is fired upon adding, changing or remove an attribute in an AttributeContainer (see Adding, getting and removing attributes). Like attributes an AttributeListener is assigned to an attribute name and type (see Creating an AttributeType). When a listener method is fired an AttributeEvent is created and supplied for the trigger.
What is an AttributeEvent?
An AttributeEvent contains all the information about the specific trigger event. This includes attribute name, type, value and replaced attribute. As well as this the event contains the container that this event was fired from (which could be an Entity).
AttributeListener is an interface with each method signifying a different trigger situation.
void attributeAdded(final AttributeEvent<T> event);
void attributeChanged(final AttributeEvent<T> event);
void attributeRemoved(final AttributeEvent<T> event);To avoid having to supply empty dummy implementations of each method AttributeListener methods are all default. The below listener outputs the attribute value to the console whenever an attribute is added.
AttributeListener<String> listener = new AttributeListener<String>() {
@Override
public void attributeAdded(final AttributeEvent<String> event) {
System.out.println(event.getValue());
}
}This will associate the listener to the supplied attribute name and type.
AttributeListener<String> listener = /* See above */
AttributeContainer container = new DefaultAttributeContainer();
container.addAttributeListener("username", Attributes.STRING_TYPE, listener);There is a NamedAttributeType equivalent: container.addAttributeListener(NamedAttributeType, AttributeListener).
Some attributes are not immutable but it is still useful to know that they have changed. This has to be a manual process:
AttributeContainer container = /* See above */
container.fireAttributeChanged("username", Attributes.STRING_TYPE);This will manually fire the attributeChanged(AttributeEvent) method in the listener.
There is a NamedAttributeType equivalent: container.fireAttributeChanged(NamedAttributeType, AttributeListener).
This is disassociate the listener from the supplied name and type.
AttributeListener<String> listener = /* See above */
AttributeContainer container = /* See above */
container.removeAttributeListener("username", Attributes.STRING_TYPE, listener);There is a NamedAttributeType equivalent: container.removeAttributeListener(NamedAttributeType, AttributeListener).
AttributeContainer offers similar management of AttributeListeners as is does to attributes.
All listener names: container.getAttributeListenerNames()
All listener types: container.getAttributeListenerTypes("username")
All listeners for name and type: container.getAttributeListeners("username", Attributes.STRING_TYPE)
Remove all listeners for name and type: container.removeAttributeListeners("username", Attributes.STRING_TYPE)
Add all listeners from another container: container.addAllAttributeListeners(otherContainer)
Removes all listeners: container.removeAllAttributeListeners()
Check out the Example projects and Code snippets!
Getting Started
- Getting the latest release
- Building from source
- Example projects
- Code snippets
- How to contribute
- Have bugs or questions?
How To Use
- JALSE
- Entities
- Attributes
- Actions
- Tags
Misc