-
Notifications
You must be signed in to change notification settings - Fork 124
Event System
Destination Sol uses the Gestalt framework for creating entities. See its wiki for more details.
Events are defined in a new class that implements Event
. It defines the value sent to the event receiver. It should have private fields and a public getter for externally relevant values. See this example event:
public class EditTextInformationEvent implements Event {
private String newInformation;
public EditTextInformationEvent(String newInformation) {
this.newInformation = newInformation;
}
public String getNewInformation() {
return newInformation;
}
}
Event receivers decide what to go with a given event. They should be in a class annotated with @RegisterEventReceivers
Each receiver is its own method annotated with @ReceiveEvent(components = MyComponent.class)
, which tells gestalt it is only interested in entities with the listed components. The method parameters are the Event
and the EntityRef
that is to be dealt with.
This example updates the Information
value for the entity, using the above EditTextInformationEvent
event:
@RegisterEventReceivers
public class EditTextInformationSystem {
@ReceiveEvent(components = TextInformation.class)
public EventResult onEditTextInformation(EditTextInformationEvent event, EntityRef entity) {
entity.getComponent(TextInformation.class).ifPresent(textInformation -> {
textInformation.setInformation(event.getNewInformation());
entity.setComponent(textInformation);
});
return EventResult.CONTINUE;
}
}
Send an event to entities with MyComponent
by using entitySystemManager.sendEvent(myEvent, new MyComponent());
The following example command creates a new EditTextInformationEvent
from above and sends it to all entities with the TextInformation
component. It will then be handled by the event receiver.
@RegisterCommands
public class EditTextInformationCommand {
@In
private EntitySystemManager entitySystemManager;
@Command(shortDescription = "Edits the text stored under Notepad entity.")
public void setTextInformation(@CommandParam(value = "text") String text) {
entitySystemManager.sendEvent(new EditTextInformationEvent(text), new TextInformation());
}
}