Skip to content

Plugin Main Class

Lars edited this page Sep 7, 2020 · 3 revisions

Table of contents

Create Plugin Main Class

Create a Java class with a name you want. The improtant thing is that your main class must extend from the PLugin class.

package com.maxo.myplugin;

import de.lars.remotelightplugins.Plugin;

public class PluginMain extends Plugin {
    
}

Your IDE will probably show an error that you have unimplemented methods. Just let your IDE auto-fix it for you or add the required method yourself.
Should look like this:

package com.maxo.myplugin;

import de.lars.remotelightplugins.Plugin;

public class PluginMain extends Plugin {

    @Override
    public boolean isLoaded() {
        return false;
    }
}

With the isLoaded() method you can determine if your plugin was loaded successfully (e.g. if required resources are available). If isLoaded() returns false your plugin will be disabled and onEnable() will never be called.
If you have nothing to check, then change the return statement to return true so that your plugin can be activated.

Your Plugins Entry Point

When activating your plugin the onEnable() method is executed. Override this method to have an entry point for your plugin.

...
public class PluginMain extends Plugin {

    @Override
    public boolean isLoaded() {
        return true;
    }

    @Override
    public void onEnable() {
        // do some stuff
    }
}

Access Program Features

Use the plugin interface to easily access all functions of RemoteLight. Each plugin instance has a plugin interface that can be accessed with the getInterface() method.

...
public class PluginMain extends Plugin {

    @Override
    public boolean isLoaded() {
        return true;
    }

    @Override
    public void onEnable() {
        PluginInterface plInterface = getInterface();
        // create notification
        Notification notification = new Notification(NotificationType.NOTIFICATION, "MyPlugin Startup", "Welcome! MyPlugin was successfully enabled.");
        // submit notification
        plInterface.getNotificationManager().addNotification(notification);
        
        // example usage of registering a custom animation
        MyAnimation animation = new MyAnimation();
        plInterface.getAnimationManager().addAnimation(animation);
    }
}

Plugin Shutdown

To execute something before closing (e.g. save file) the onDisable() method can be overwritten. It is called when the plugin gets disabled.

public class PluginMain extends Plugin {

    @Override
    public boolean isLoaded() {
        return true;
    }

    @Override
    public void onEnable() {
        PluginInterface plInterface = getInterface();
        // create notification
        Notification notification = new Notification(NotificationType.NOTIFICATION, "MyPlugin Startup", "Welcome! MyPlugin was successfully enabled.");
        // submit notification
        plInterface.getNotificationManager().addNotification(notification);

        // example usage of registering a custom animation
        MyAnimation animation = new MyAnimation();
        plInterface.getAnimationManager().addAnimation(animation);
    }

    @Override
    public void onDisable() {
        // save data to disk
        try(FileWriter writer = new FileWriter("data.txt")) {
            writer.write("Really important data.");
        } catch (IOException e) {
            System.err.println("Could not save data :(");
        }
    }
}

➥ Next: Export Plugin