Skip to content

Writing Your First Plugin

ced777ric edited this page Feb 10, 2025 · 1 revision

First Lines of Code

LabAPI implements a Module Pattern to load plugins. Because of this, plugins should have a main class that implements the Plugin abstract class.

public class MyFirstPlugin : Plugin
{
    // The name of the plugin
    public override string Name { get; } = "My First Plugin";

    // The description of the plugin
    public override string Description { get; } = "This plugin does magic!";

    // The author of the plugin
    public override string Author { get; } = "Me!"

    // The current version of the plugin
    public override Version Version { get; } = new Version(1, 0, 0, 0);

    // The required version of LabAPI (usually the version the plugin was built with)
    public override Version RequiredApiVersion { get; } = new (LabApiProperties.CompiledVersion);
}

Entry point

You can override the Plugin::Enable() method to make your entry point:

{
    public override string Name { get; } = "MyPlugin";
    
    public override void Enable()
    {
        // This will run when the plugin loads!
    }
}

Exit Point

You can override the Plugin::Disable() method to make your exit point:

public class MyFirstPlugin : Plugin
{
    public static ConnectionProvider Connector = new ConnectionProvider();
    
    public override void Enable()
    {
        Connector.Start();
    }
    
    public override void Disable()
    {
        // Here we can release all the garbage we have
        // Which is really helpful to avoid memory leaks!
        Connector.Stop();
    }
}

Loading Priorities

In the case of a server with multiple plugins, your plugin may have to use an event before others because it has more importance or priority, for those cases you may override the Priority property.

// This will ensure the plugin loads before the plugins with less priority
// Allowing the plugin to register events before other plugins do
// As events are based on registration order, they will be called before
public override LoadPriority Priority { get; } = LoadPriority.High;

It can also be useful for some plugins that serve as libraries for other plugins, such as custom permissions systems, etc.

[^1]: The properties that we just saw!

[^2]: This doesn't exist, it is just an example where OnUnloaded may result useful.

Clone this wiki locally