Developer API
On this page(6)
SnNPCs exposes a public developer API for other plugins: custom Bukkit events and a query service that can also set an NPC's glow. The API lives in the com.sn.npcs.api package inside the plugin jar. There is no separate artifact.
Only com.sn.npcs.api is a stable, kept contract. Everything else in the jar is obfuscated and internal. Do not call it.
Getting the jar
-
Download the latest release jar.
-
Install it into your local Maven repository:
mvn install:install-file -Dfile=SnNPCs-<version>.jar -DgroupId=com.sn \ -DartifactId=SnNPCs -Dversion=<version> -Dpackaging=jar -
Depend on it with
providedscope. Never shade it.
Quick start
Declare the dependency in your plugin.yml:
depend: [SnNPCs] # or softdepend if optionalResolve the API when you need it:
SnNPCsAPI api = SnNPCsProvider.get();
if (api != null) {
// use the api
}Resolve the reference when you need it. Do not cache it across a SnNPCs disable.
Master switch
API events can be disabled by the server owner with api-events.enabled: false in config.yml. The query service stays available either way.
Events
Cancellable events fire before the action. Cancelling aborts it.
| Event | Fired when | Cancel effect |
|---|---|---|
NpcClickEvent | A player clicks an NPC or its hologram, after its requirements and cooldown, before its actions | The actions do not run and no cooldown starts |
NpcCreateEvent | The create or clone command is about to add a new NPC | Nothing is created and the sender is told |
NpcDeleteEvent | The delete command is about to remove an NPC | The NPC stays and the sender is told |
NpcClickEvent also fires for a click on an NPC with no action for that click. Your plugin can handle such NPCs itself. A click that is cooling down or fails the requirements does not fire it.
Every event fires on the main thread. Each one carries a read-only NpcInfoView from getNpc().
| Event | Extra getters |
|---|---|
NpcClickEvent | getPlayer(), isRightClick(), isSneaking() |
NpcCreateEvent | getSender() |
NpcDeleteEvent | getSender() |
Listen like any Bukkit event:
@EventHandler
public void on(NpcClickEvent event) {
if (event.getNpc().id().equalsIgnoreCase("guard")) {
event.setCancelled(true);
}
}Event payloads are read-only. setCancelled is the only change a listener can make.
Query service
Synchronous methods read in-memory state. Call them on the main thread. Ids match ignoring case.
| Method | Returns | Notes |
|---|---|---|
getNpc(String id) | NpcInfoView | Null when no NPC has that id |
getNpcs() | List<NpcInfoView> | Every loaded NPC, sorted by id |
getNpcIds() | List<String> | Every loaded id, sorted |
hasNpc(String id) | boolean | File NPCs and runtime NPCs alike |
isShown(String id) | boolean | True when the NPC is enabled and rendered |
getViewerCount(String id) | int | Players who are sent the NPC now |
getAppearance(String id, Player viewer) | NpcAppearanceView | Null when the NPC is not sent to that player |
getClient(Player player) | ClientView | The server's own version until ViaVersion answers |
setGlow(String id, boolean glowing, Integer rgb) | boolean | Sets a file NPC's outline (rgb is 0xRRGGBB, or null for the default outline), shows it at once and saves it to the NPC's file. Writes nothing when already in place. False when no file NPC has that id |
getApiVersion() | String | The API contract version |
The views are immutable snapshots. Read them again after an edit or a reload.
| View | Components |
|---|---|
NpcInfoView | id, folder, runtime, enabled, world, x, y, z, yaw, pitch, viewDistance, entityType, scale, model, modelScale, cooldownTicks, plus location() and hasModel() |
NpcAppearanceView | npcId, form, height, scaleApplied, hologramY |
ClientView | protocol, bedrock, supportsScale |
form is model, player, hidden or entity:<TYPE>. location() is null when the world of the NPC is not loaded.
Versioning
Call getApiVersion() for the API contract version. It is independent of the plugin version. Additions bump the minor component. Existing members are never removed or changed; deprecated members keep working.