Skip to content

Custom Events

Asintoto edited this page Jul 19, 2024 · 3 revisions

Custom Events

Here you will find how to create custom events for you plugin


There are 2 type of events: NonCancellableEvent and CancellableEvent, both of them contains the needed methods (such as getHandlerList() or setCancellable(boolean) for cancellable events) so that you do not have to type them yourself. To use one of these events, just create a class that extends one of them

public class MyCancellableEvent extends CancellableEvent {
    // Attributes
    // Methods
}


public class MyNonCancellableEvent extends NonCancellableEvent {
    // Attributes
    // Methods
}

Note: This will simplifly the basic creation of the custom event class, all the logic must be implemented as needed

Registring Listeners

Basic allows you to easily register Event Listeners in you plugin by just adding the registerListener(new ListenerClass) to your onPluginEnable() or onEnable() method

public class MyPlugin extends BasicPlugin{
    @Override
    public void onPluginEnable() {
        registerListener(new MyBlockBreakEvent());  // This will register the Listener class MyBlockBreakEvent
    }
}

Note: You can also use the @AutoRegister annotation to automatically register you listener

@AutoRegister
public class MyListener implements Listener {
   ...
}

Note: Make sure that your listener class has ONLY the No Args Constructor (ex. public MyListener() {} )

Advanced Note: You can also specify the plugin you want to register the listener in just like this:

public class MyPlugin extends BasicPlugin{
    @Override
    public void onPluginEnable() {
        registerListener(new MyBlockBreakEvent(), otherPlugin);  // Where otherPlugin is a JavaPlugin class
    }
}
Clone this wiki locally