44 SubAPI
ME1312 edited this page 2022-01-15 13:26:38 -05:00

SubAPI is the way to access the server manager through custom plugins. This page goes over how get started using it.

Topics on This Page:
SubServers.Bungee
SubServers.Host
SubServers.Sync
SubServers.Client

SubAPI via Maven

You may want to use Maven to handle the SubAPI dependency for you. If so, here's the code you need to make it happen.

<repositories>
    <repository>
        <id>ME1312.net</id>
        <url>https://dev.ME1312.net/maven</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>net.ME1312.SubServers</groupId>
        <artifactId>SubServers.Bungee</artifactId>
        <version>00w00a</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

artifactId can be replaced with the SubServers app name for the platform of your choice.
version can be replaced with the build version of your choice.

Using SubAPI as a Soft Dependancy

A "Soft Dependancy" is where your plugin will use SubServers, but it is not required for your plugin to function properly.

First, Make a method to see if, for example, SubServers v2.14a+ is available using reflection:

public boolean isSubServers2() {
    try {
        net.ME1312.SubServers.Bungee.SubAPI api = net.ME1312.SubServers.Bungee.SubAPI.class.cast(net.ME1312.SubServers.Bungee.SubAPI.class.getMethod("getInstance").invoke(null));
        if (api.getWrapperVersion().compareTo(net.ME1312.Galaxi.Library.Version.Version.class.getConstructor(String.class).newInstance("2.14a")) >= 0) {
            return true;
        } else {
            return false;
        }
    } catch (Throwable e) {
        return false;
    }
}

So what happens here is:

  1. If there is any sort of error getting these classes, return false.
  2. If the SubServers Version is less than 2.14a, return false.
  3. Otherwise, your good to go (returns true)

Pro-Tips for using SubServers as a Soft-Dependancy:

  • Make sure SubServers is available using the above method before accessing SubServers classes
  • Never use imports for classes starting with "net.ME1312"
    new Version(1); is now new net.ME1312.Galaxi.Library.Version.Version(1);
    SubAPI.getInstance(); is now net.ME1312.SubServers.Bungee.SubAPI.getInstance();

SubServers.Bungee

This will show how to use SubAPI for SubServers.Bungee. For more detailed information visit this page:
https://dev.me1312.net/jenkins/job/SubServers Platform/javadoc/SubServers.Bungee/

Using SubAPI for Bungee

Most of SubAPI can be accessed using this simple method SubAPI.getInstance(), however there's a bit more to it than just that. SubServers.Bungee works by extending BungeeCord classes to add more functionality, and overriding the methods to serve the extended classes. Knowing this, you can use it to your advantage.

Bungee ServerInfo:
Just about any time you get a ServerInfo variable you can parse it to it's SubServers equivalent:

Server server = (serverinfo instanceof Server)?(Server)serverinfo:null;
// This will get you the SubServers wrapped version of a server
// if that server has been registered within SubServers

SubServer subserver = (serverinfo instanceof SubServer)?(SubServer)serverinfo:null;
// This will get you the server as a SubServer
// if it was registered as a SubServer in the first place

SubServers.Sync & SubServers.Client

This will show how to use SubAPI for SubServers.Sync and SubServers.Client since they are extremely similar in that regard. For more detailed information visit the page for your platform:
https://dev.me1312.net/jenkins/job/SubServers Platform/javadoc/SubServers.Sync/
https://dev.me1312.net/jenkins/job/SubServers Platform/javadoc/SubServers.Sync.Velocity/
https://dev.me1312.net/jenkins/job/SubServers Platform/javadoc/SubServers.Client.Bukkit/
https://dev.me1312.net/jenkins/job/SubServers Platform/javadoc/SubServers.Client.Sponge/

Using SubAPI for SubServers.Sync/SubServers.Client

Like SubServers.Bungee, SubAPI methods can be accessed using the method SubAPI.getInstance(), however here most of the API is SubData packet based. This means that the emulated API, although similar, now heavily uses callbacks to achieve it's goals.

Working with Cached Data:
Something you have to watch out for is that when you call to something like SubAPI.getServer(), you are downloading a snapshot of the data that has been frozen in time since the moment you requested it. To get an updated version of the data, you can just request it again using the same method, or use the resulting object's .refresh() method.

SubServers.Host

This will show how to use SubAPI for SubServers.Host. For more detailed information visit this page:
https://dev.me1312.net/jenkins/job/SubServers Platform/javadoc/SubServers.Host/

GalaxiAPI

GalaxiEngine is the backend that drives SubServers.Host as a standalone application. It provides most of the basic API methods you've come to expect, for example:

  • A Plugin Manager
  • An Event API
  • Configuration APIs

Creating your plugin

Plugin structure is pretty simple, all you need is a main class annotated with @Plugin:

import net.ME1312.Galaxi.Event.GalaxiStartEvent;
import net.ME1312.Galaxi.Event.GalaxiStopEvent;
import net.ME1312.Galaxi.Plugin.Plugin;
import net.ME1312.Galaxi.Library.Event.Subscribe;

@Plugin(name = "ExamplePlugin", version = "1.0.0a", authors = "ME1312")
public class ExamplePlugin {
    
    @Subscribe
    public void onEnable(GalaxiStartEvent event) {
        // Write enable code here
    }
    
    @Subscribe
    public void onDisable(GalaxiStopEvent event) {
        // Write disable code here
    }
}

So, what happens here is:

  1. You've given GalaxiEngine info about your plugin through @Plugin
  2. You're listening to GalaxiStartEvent and GalaxiStopEvent to enable and disable your plugin respectively.

Using SubAPI for Hosts

The SubAPI portion of the code more closely resembles the API for SubServers.Client (being SubData packet based). To access GalaxiAPI, call to Galaxi.getInstance().