Skip to content
Loving11ish edited this page Dec 27, 2024 · 2 revisions

API Implementation

Current API Version:

Adding UStatsAPI To Your Project

To start, you're going to want to add UStatsAPI as a dependency or soft dependancy to your project. While you could download the jar file and add it as a classpath dependency, we highly recommend using a build system such as Maven or Gradle.

Using Maven

  • Repository
	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>
  • Dependency
	<dependency>
	    <groupId>com.github.CraptiCraft-Development</groupId>
	    <artifactId>UStats-API</artifactId>
	    <version>[CURRENT-VERSION]</version>
            <scope>provided</scope>
	</dependency>

Using Gradle

  • build.gralde
	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}
  • Dependency
	dependencies {
	        implementation 'com.github.CraptiCraft-Development:UStats-API:[CURRENT-VERSION]'
	}

Getting The API Instance

Once you have added the API to your project, you can start interfacing with it. The first thing you need to do load the instance of the plugin within the onEnable() method in your plugin.

UStatsAPI uses the less well known RegisteredServiceProvider system provided by Bukkit for its interfacing with external plugins. The UStats plugin itself already registers as the provider for the API, so if the UStats plugin is correctly enabled on the server, then the API classes have been successfully registered. Don't forget to add UStats as either a soft dependency or a dependency in your plugin.yml too.

  • Here is an example main class for how to get the API instance:
package me.loving11ish.ustatsapitest;

import com.tcoded.folialib.FoliaLib;
import me.loving11ish.ustatsapi;
import org.bukkit.Bukkit;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

public final class UStatsAPITest extends JavaPlugin {

    private static UStatsAPITest plugin;
    private static FoliaLib foliaLib;

    // UStatsAPI - Always null check before using! This must always be null at the start of the plugin!
    private UStatsAPI uStatsAPI = null;

    @Override
    public void onLoad() {
        // Plugin startup logic
        plugin = this;
        foliaLib = new FoliaLib(this);
    }

    @Override
    public void onEnable() {
        // Plugin startup logic

        // Load UStatsAPI
        // UStatsAPI is a soft-dependency, so we need to check if it's loaded
        // UStatsAPI uses a service provider, so we need to check if it's registered by the UStats plugin itself
        // If it's not registered, we need to disable the plugins ability to run any UStatsAPI code or errors will occur!
        if (!Bukkit.getPluginManager().isPluginEnabled("UStats")) {
            getLogger().severe("UStats not found!");
            Bukkit.getPluginManager().disablePlugin(this);
            return;
        }

        RegisteredServiceProvider<UStatsAPI> rsp = Bukkit.getServicesManager().getRegistration(UStatsAPI.class);
        if (rsp != null) {
            uStatsAPI = rsp.getProvider();
        } else {
            getLogger().severe("UStatsAPI not found!");
            Bukkit.getPluginManager().disablePlugin(this);
            return;
        }

        // Now you can run you plugin startup logic

        // Final startup message
        getLogger().info("UStatsAPITest has been enabled!");
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic

        // Unregister listeners
        HandlerList.unregisterAll(this);

        // Final shutdown message
        getLogger().info("UStatsAPITest has been disabled!");

        // Nullify plugin
        plugin = null;
        foliaLib = null;
        uStatsAPI = null;
    }

    public static UStatsAPITest getPlugin() {
        return plugin;
    }

    public static FoliaLib getFoliaLib() {
        return foliaLib;
    }

    public UStatsAPI getUStatsAPI() {
        return uStatsAPI;
    }
}

Home Page

Click Here

Clone this wiki locally