Skip to content
Guilherme Chaguri edited this page Apr 1, 2018 · 2 revisions

Retrieve the API instance

Platform independent

You can get an API instance with the following method:

IPvPTimeAPI api = PvPTimeAPI.getAPI();

You can't use PvPTime as a soft dependency using this method.

Bukkit

Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("PvPTime");

if(plugin != null && plugin instanceof PvPTimeBukkit) {
    IPvPTimeAPI<String> api = ((PvPTimeBukkit)plugin).getAPI();
    // Use the API
}

Sponge

Using the service manager:

Optional<IPvPTimeAPI> optional = Sponge.getServiceManager().provide(IPvPTimeAPI.class);

if(optional.isPresent()) {
    IPvPTimeAPI pvptime = opt.get();
    // Use the API
}

Getting directly from the plugin instance:

Optional<PluginContainer> optional = Sponge.getPluginManager().getPlugin("pvptime");

if(optional.isPresent()) {
    Optional<?> plugin = optional.get().getInstance();
    if(plugin.isPresent() && plugin.get() instanceof PvPTimeSponge) {
        IPvPTimeAPI<String> api = ((PvPTimeSponge)plugin.get()).getAPI();
        // Use the API
    }
}

Forge

FMLInterModComms.sendFunctionMessage("pvptime", "api", APIRetriever.class.getName());
public class APIRetriever implements Function<IPvPTimeAPI<Integer>, Void> {
    @Nullable
    @Override
    public Void apply(@Nullable IPvPTimeAPI<Integer> api) {
        // Use the API
        return null;
    }
}
Clone this wiki locally