Skip to content

ExamplePlugin

adadot12 edited this page Dec 16, 2024 · 1 revision

Example of a DotX-enabled plugin

DotX provides a DotPlugin class. This class lets you use the full featureset of DotX with significantly reduced boilerplate code.

ExamplePlugin.java

package com.example.dotX;

import co.dotarch.x.DotPlugin;
import co.dotarch.x.data.DotPlayer;

public class DotExample extends DotPlugin
{

    private static DotExample theInstance;
    
    // Use this to setup your key namespace.
    public static final String PLUGIN_KEY_PREFIX = "yourplugin.";
    
    // Set final variables to represent your properties, magic strings are icky.
    public static final String EXAMPLE_PROPERTY_KEY = PLUGIN_KEY_PREFIX + "example_property";
    public static final String DEFAULT_EXAMPLE_PROPERTY = "example";
    public static final String EXAMPLE_TRANSIENT_PROPERTY_KEY = PLUGIN_KEY_PREFIX + "example_transient";
    public static final String DEFAULT_TRANSIENT_PROPERTY = "spooky example!";
    
    
    /**
     * Typical onEnable().
     */
    @Override
    public void onEnable()
    {
        // This allows DotX to register your plugin and read your configurations automagically. 
        // Without this, the majority of DotX features involving the plugin will not work.
        super.onEnable();
        
        // Reference variable, important for command-based configuration and accessing the plugin from elsewhere.
        // If you prefer injection, you're welcome to use that for your code, but because of the wonkiness of reflection
        // you need at least one static reference so instance() will work.
        theInstance = this;
    }

    /**
     * Unchanged onDisable (for now)
     */
    @Override
    public void onDisable()
    {
        // Please try and clean up your mess when you are done, its best practice lol
    }
    
    /**
     * Use this to route config files on a per-class basis.
     * Instead of complicated logic for picking config files, you can
     * write it all here and pass the class you need at runtime.
     * 
     * @param clazz The class for which this is being evaluated.
     * @return The correct/ideal file for this class.
     */
    @Override
    public String getConfigFilenameForClass(Class clazz)
    {
        // Use this to route to config files based on the calling class.
        return "config.yml";
    }

    /**
     * This method is how DotX is able to identify which files actually exist in your configuration. 
     * Just list the filepaths starting from resources to your config files.
     * @return String array of config filenames that exist.
     */
    @Override
    public String[] getConfigFilenames()
    {
        // Your config files, aside from messages.yml, must be in this array, otherwise DotX cannot virtualize them.
        return new String[]
                {
                        "config.yml"
                };
    }

    /**
     * In order to permit magic configuration via VConfig, DotX has to be able to get your plugin's instance.
     * It is done via reflection utilizing this method.
     * 
     * @return Your plugin instance
     */
    public static DotExample instance()
    {
        return self;
    }

     /**
     * Use this to set your default values for new players, and do whatever else you need when someone logs in.
     *
     * For more advanced logic, you are welcome to use co.dotarch.x.events.PlayerLoadedEvent.
     *
     * @param player The player who just logged into the server, and whose information has been loaded.
     */
    @Override
    public void handleLoadedPlayer(DotPlayer player)
    {
        // Save a default value for the example property if it doesn't exist already.
        player.initializeProperty(EXAMPLE_PROPERTY_KEY, String.valueOf(DEFAULT_EXAMPLE_PROPERTY), true);
        // Write a default transient property to this player upon join. 
        player.writeTransient(EXAMPLE_TRANSIENT_PROPERTY_KEY, String.valueOf(DEFAULT_TRANSIENT_PROPERTY));
    }
}

Clone this wiki locally