Getting Started
Initializing the API
Load SofyCoreApi from Bukkit's ServicesManager, wait for StorageReadyEvent without blocking and request services safely.
Load the entry point
import net.sofycore.api.SofyCoreApi;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
public final class MyPlugin extends JavaPlugin {
private SofyCoreApi sofy;
@Override
public void onEnable() {
sofy = Bukkit.getServicesManager().load(SofyCoreApi.class);
if (sofy == null) {
getLogger().severe("sofyCore is not available");
getServer().getPluginManager().disablePlugin(this);
return;
}
}
}Wait for storage without blocking
Persistence-backed services are registered after the asynchronous startup succeeds. Subscribe to StorageReadyEvent (published on the Minecraft thread) and also check whether the core is already ready. Never block onEnable waiting for the event.
import net.sofycore.api.storage.PersistenceService;
import net.sofycore.api.storage.StorageReadyEvent;
AutoCloseable readySub = sofy.events().subscribe(StorageReadyEvent.class, event -> start(event.api()));
if (sofy.service(PersistenceService.class).isPresent()) {
start(sofy); // storage was already ready
}
private void start(SofyCoreApi api) {
// request services here
}Request services
api.service(RankService.class).ifPresent(ranks -> {
// ranks module is running
});service() returns Optional.empty() when the module is off, failed, or not ready. Do not cache a service forever: a module reload unregisters and registers its service again.