Skip to content
Pieter edited this page Jul 18, 2025 · 9 revisions

Creating an Event

It's easy to create an event. Just create a class that implements the SEvent interface.

  1. Create a new class.
  2. Implement the SEvent interface.
  3. 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;
    }
}

Creating a Cancellable Event

If you want to create an event that can be cancelled by listeners, either:

  • Extend the SCancelableEventBase class (recommended), or
  • Implement the SCancelableEvent interface directly (if you need custom behaviour).

Extending SCancelableEventBase (Recommended)

This approach provides a default cancellable implementation, so you only need to define your event data.

  1. Create a new class.
  2. Extend SCancelableEventBase.
  3. 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;
    }
}

Implementing SCancelableEvent

Implement this interface directly if you want full control over how the cancellable behavior works.

  1. Create a new class.
  2. Implement the SCancelableEvent interface.
  3. Implement the cancel, isCancellable, and isCancelled methods.
  4. 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;
    }
}

Creating a Returnable Event

If you want to create an event that returns a value from its listeners, implement the SReturnableEvent interface.

  1. Create a new class.
  2. Implement the SReturnableEvent interface, specifying the return type.
  3. Add a field for the result.
  4. Implement the getResult and setResult methods.
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;
    }
}

Creating an Event with Records

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 {
}

Dispatching an Event

To dispatch an event, use the dispatch method on a Sees instance.

  • dispatch returns true if 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.");
        }
    }
}

Dispatching an Event and Getting the Result

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);
    }
}