Skip to content
Hackusate_PvP edited this page Aug 4, 2023 · 4 revisions

Events are actions that happen during the game or in the background. For example, if a player clicks on a button a ButtonClickEvent is called which will allow you to execute code when that happens.

Setup Events

To setup Events you will need to have a EventListener class. An EventListener is an interface that groups Event methods together.

public class ExampleEventListener implements EventListener {

}

To add Listeners to the class you will need to use the @Listener annotation and define a method with an Event as a parameter.

    @Listener
    public void onGameStart(GameStartEvent event) {
        // Called when the game starts!
        RenJava.getInstance().getLogger().info("Game has started.");
    }

This goes right inside the class we just created. Next you will have to register the listener inside the class that extends RenJava.

    @Override
    public void preEnabled() {
        // Register Events here.
        this.registerListener(new ExampleEventListener());
    }

You can create as many EventListener classes as you want.

Create your own Events

You can also create your own events.

public class ExampleEvent extends Event {
    // Here you can put any form of data.
    // If are you confused on this part ask yourself what is the purpose of the event.

    // I'm just going to require a string as this event really has no purpose because it's an example. Your events should have a purpose.
    private final String string;

    /**
     * A Constructor is required or well should be... You can technically have empty events but that is not common.
     * @param string This is just an example value and serves no real purpose.
     */
    public ExampleEvent(String string) {
        this.string = string;
    }

    // Make sure you add getter methods to your data. You can even do setters if you want.
    public String getString() {
        return string;
    }
}

When you create an event you will have to call the event to be executed.

ExampleEvent event = new ExampleEvent("This is an example event");
RenJava.callEvent(event);

Clone this wiki locally