Get the API
Read this before you write anything else. Do not call
NxEquipementsProvider.get() from your onEnable. It will throw, every time, even with depend set. NxEquipements finishes starting up after every other plugin has enabled. Always use whenReady.The way that works
@Override
public void onEnable() {
NxEquipementsProvider.whenReady(this, api -> {
getLogger().info("NxEquipements ready, API " + api.apiVersion());
// everything that touches the API goes in here
});
}
Your callback runs as soon as NxEquipements is ready. If it's already ready, it runs immediately.
Keep a reference for later
public class MyPlugin extends JavaPlugin {
private NxEquipementsApi nxe;
@Override
public void onEnable() {
NxEquipementsProvider.whenReady(this, api -> {
this.nxe = api;
getServer().getPluginManager().registerEvents(new MyListener(this), this);
});
}
public NxEquipementsApi nxe() {
return nxe;
}
}
Check whether it's available at all
Inside a command, where NxEquipements may not be installed:
if (!NxEquipementsProvider.isAvailable()) {
sender.sendMessage("NxEquipements is not installed.");
return true;
}
NxEquipementsApi api = NxEquipementsProvider.get();
Or handle both cases in one go:
NxEquipementsProvider.getIfPresent().ifPresentOrElse(
api -> sender.sendMessage("Sets loaded: " + api.sets().size()),
() -> sender.sendMessage("NxEquipements is not installed.")
);
Every way to get it
| Call | Use it when |
|---|---|
whenReady(plugin, consumer) | Startup. Always use this in onEnable. |
isAvailable() | You just want a yes/no. |
getIfPresent() | You want an Optional and will handle both cases. |
get() | Later, at runtime, when you already know it's up. Throws if it isn't. |