-
Notifications
You must be signed in to change notification settings - Fork 0
Events
Pieter edited this page Jul 18, 2025
·
9 revisions
It's easy to create an event. Just create a class that implements the SEvent interface.
- Create a new class.
- Implement the
SEventinterface. - Add any fields and methods you need.
import nl.devpieter.sees.event.SEvent;
public class UserLoggedInEvent implements SEvent {
private final String username;
public UserLoggedInEvent(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
}If you want to create an event that can be cancelled by listeners, either:
-
Extend the
SCancelableEventBaseclass (recommended), or -
Implement the
SCancelableEventinterface directly (if you need custom behaviour).
This approach provides a default cancellable implementation, so you only need to define your event data.
- Create a new class.
- Extend
SCancelableEventBase. - Add any fields and methods you need.
import nl.devpieter.sees.event.SCancelableEventBase;
public class UserLoggingInEvent extends SCancelableEventBase {
private final String username;
public UserLoggingInEvent(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
}Implement this interface directly if you want full control over how the cancellable behavior works.
- Create a new class.
- Implement the
SCancelableEventinterface. - Implement the
cancel,isCancellable, andisCancelledmethods. - Add any fields and methods you need.
import nl.devpieter.sees.event.SCancelableEvent;
public class UserLoggingInEvent implements SCancelableEvent {
private final String username;
private boolean cancelled = false;
public UserLoggingInEvent(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
@Override
public void cancel() {
if (isCancellable()) {
this.cancelled = true;
}
}
@Override
public boolean isCancellable() {
return true;
}
@Override
public boolean isCancelled() {
return cancelled;
}
}If you want to create an event that returns a value from its listeners, implement the SReturnableEvent interface.
- Create a new class.
- Implement the
SReturnableEventinterface, specifying the return type. - Add a field for the result.
- Implement the
getResultandsetResultmethods.
import nl.devpieter.sees.event.SReturnableEvent;
public class UserNameEvent implements SReturnableEvent<String> {
private String result;
@Override
public String getResult() {
return result;
}
@Override
public void setResult(String result) {
this.result = result;
}
}You can also create an event using a Java record. Just implement the SEvent interface on your record.
import nl.devpieter.sees.event.SEvent;
public record UserLoggedInEvent(String username) implements SEvent {
}To dispatch an event, use the dispatch method on a Sees instance.
-
dispatchreturnstrueif the event was cancelled by a listener. - Otherwise, it returns
false.
import nl.devpieter.sees.Sees;
public class Main {
public static void main(String[] args) {
// Get the default Sees instance.
Sees sees = Sees.getInstance();
// Dispatch the event.
boolean cancelled = sees.dispatch(new UserLoggingInEvent("Pieter"));
if (cancelled) {
System.out.println("Login was cancelled by a listener.");
} else {
System.out.println("Login was successful.");
}
}
}Use dispatchWithResult for events that implement SReturnableEvent to get a result from the listeners.
import nl.devpieter.sees.Sees;
public class Main {
public static void main(String[] args) {
// Get the default Sees instance.
Sees sees = Sees.getInstance();
// Dispatch the event and get the result.
String username = sees.dispatchWithResult(new UserNameEvent());
System.out.println("Username from event: " + username);
}
}