#Need help with creating an API

1 messages · Page 1 of 1 (latest)

warped mantle
#

Hi, I have made a plugin (provider) which has some functionality that I want to access via another plugin I am making (consumer). To make this work, I have made a third plugin which is supposed to act as an API for the provider.

The API project consists of an empty main class and an Interface which looks like this:

public interface AurumAPI {

    UUID spawnMobAtLocation(Location location, String mobName);
    void openDoorAtLocation(Location location);
    void clearTokensFromPlayer(Player player);

}

The provider has a class called APIProvider which implements the methods outlined in the api which looks like this:

public class APIProvider implements AurumAPI {

     ......
}

I am then using the bukkit service manager to register that class so that i can access it in my consumer like this:
Bukkit.getServicesManager().register(AurumAPI.class, new APIProvider(), plugin, ServicePriority.Normal);
After that i have made a check to see if it was properly registered like this getLogger().info("AurumAPI registered: " + Bukkit.getServicesManager().isProvidedFor(AurumAPI.class)); which returns true, so thats working fine.

In my consumer i then try to access the service like this in my onEnable:

aurumAPI = Bukkit.getServicesManager().load(AurumAPI.class);
        if (aurumAPI == null) {
            getLogger().warning("Failed to load AurumAPI!");
        }

Here the aurumAPI == null check always returns true which also means that i can use any of the functionality.
Does anyone know what I am doing wrong or if my whole approach is just incorrect?

coral hearth
warped mantle
tawny cloud
#

is the plugin that requests the service loaded after the one that registers it @warped mantle

coral hearth
#

Are you sure that the consumer depends on the api?

#

So bukkit loads the api first

warped mantle
#

it loads the api, then the consumer and then the provider which yeah, it probably the reason

tawny cloud
#

yep

#

just depend on the provider

#

and make the provider depend on the api*

warped mantle
#

ok it loads it in the correct order now but it still returns false :(

[11:16:16 INFO]: [AurumAPI] Enabling AurumAPI v${project.version}
[11:16:16 INFO]: [Aurum] Enabling Aurum v1.0-SNAPSHOT
[11:16:16 INFO]: [Aurum] AurumAPI registered: true
[11:16:16 INFO]: [Aurum] Starting up...
[11:16:16 INFO]: [TAqDungeons] Enabling TAqDungeons v1.0-SNAPSHOT
[11:16:16 INFO]: [TAqDungeons] AurumAPI registered: false
[11:16:16 WARN]: [TAqDungeons] Failed to load AurumAPI!
coral hearth
warped mantle
coral hearth
#

One says registered is true

#

One doesn't

warped mantle
#

yes

coral hearth
#

Make sure TAqDungeons has atleast softdepend ArumAPI

warped mantle
coral hearth
#

Weird

warped mantle
#

i just set up this for loop to trigger every second in the provider plugin and it keeps saying that APIProvider class is registered

for (RegisteredServiceProvider<?> provider : Bukkit.getServicesManager().getRegistrations(AurumAPI.class)) {
                    getLogger().info("Registered provider: " + provider.getProvider().getClass().getName());
                }

[11:50:58 INFO]: [Aurum] Registered provider: goldenshadow.aurum.APIProvider

So im assuming im doing something wrong in the consumer

raven crest
#

That's more like it

#

If you look at an example of a plugin that uses the registration api (vault, for example), they'll never use the load method

warped mantle
#

aurumAPI = Bukkit.getServicesManager().getRegistration(AurumAPI.class).getProvider(); causes a null pointer exception

raven crest
#

Interesting

raven crest
#
for (RegisteredServiceProvider<?> provider : Bukkit.getServicesManager().getRegistrations(AurumAPI.class)) {
  this.aurumAPI = provider.getProvider();
  break;
}
#

it'd be stupid but

warped mantle
raven crest
#

try and see