Skip to content

Handlers

frosxt edited this page Jul 4, 2023 · 1 revision

Handlers are an easy way to register a class and keep it contained in a Map where it is easily accessible. These help keep code manageable, readable and extendable.

Creating a handler

A handler can be created by implementing the IHandler interface. This can be done as follows:

public class ExampleHandler implements IHandler {
    
    @Override
    public void load() {
        // Logic when the handler is loaded
    }

    @Override
    public void unload() {
        // Logic when the handler is unloaded
    }
}

Registering a handler

Handlers can be easily registered by creating a new instance of the HandlerManager class. This has all the methods necessary to store, register and unregister handlers.

The handler can be registered by executing the following code:

HandlerManager handlerManager = new HandlerManager();
handlerManager.registerHandler(new ExampleHandler());

or alternatively, you can register multiple handlers parsing a String List or multiple IHandler objects to the registerHandlers(...) method.

Unregistering a handler

Handlers can be easily unregistered by accessing the HandlerManager instance and executing the unregisterHandler(...) method. This can be done as follows:

handlerManager.unregisterHandler(ExampleHandler.class);

Retrieving a handler

You can easily retrieve a handler with the use of the HandlerManager#getHandler() method. This can be done as follows:

handlerManager.getHandler(ExampleHandler.class);

Retrieving multiple handlers

You can easily retrieve all registered handlers with the use of the HandlerManager#getHandlers() method. This method returns Collection<IHandler>

Clone this wiki locally