VenusAPIProvider class
The static accessor that hands out the VenusApi instance, in whichever form your plugin can cope with.
The class holds exactly one instance, published once Venus has loaded. Everything else on this site assumes you already hold that reference.
| Signature | Description |
|---|---|
static VenusApi get() | The live API. Throws VenusNotLoadedException when called before Venus is loaded. |
static VenusApi getOrNull() | The live API, or null. Useful for optional integrations that must not fail. |
static CompletableFuture<VenusApi> getAsync() | Completes with the API as soon as it exists. Already completed when the API is up, so it is safe to call at any time. |
static boolean isLoaded() | True once the instance has been published. Stays true until the JVM exits. |
static void init(VenusApi api) | Called by the platform bootstrap. A plugin must never call this. |
The pattern that always works
Whether Venus is already loaded or still starting, this returns exactly once and never throws:
public final class ApiHook {
private static volatile VenusApi api;
/** Called from your plugin's onEnable. */
public static void start() {
VenusAPIProvider.getAsync().thenAccept(ApiHook::onApi);
}
private static void onApi(VenusApi instance) {
api = instance;
instance.events().repository().register(ViolationEvent.class, event ->
plugin.getLogger().info(event.user().username()
+ " failed " + event.check().name() + " ("
+ Math.round(event.violationLevel()) + ")"));
}
public static Optional<VenusApi> api() {
return Optional.ofNullable(api);
}
}softdepend: [VenusLoader]
# and in code
if (VenusAPIProvider.isLoaded()) {
ApiHook.start();
}Why not just a weak reference?
The instance is stable for the life of the JVM: Venus does not unload itself, so the reference you get is the reference you keep. The API still avoids handing you objects that can go stale — the interfaces wrap live state and report an empty result when their subject is gone, rather than throwing at the call site.
Order of operations
- Your plugin is loaded. Do nothing with Venus here unless
isLoaded()already returns true. - In
onEnable, register a callback withgetAsync(). - In the callback, register listeners and cache the reference you need.
- In
onDisable, unregister your listeners withunregisterAll()or one handle at a time.
If your plugin needs to run while Venus is still starting, the loader hook explains how to defer work without risking a class-loading failure.