ServerAPIv1.1.0

開發者 APIDeveloper API

這頁給想串接 ServerAPI 的插件開發者,講怎麼把它加進你的專案、以及可用的 API。 完整的教學、模式怎麼選、回傳型別,見第三方站點串接

導入依賴

你需要在編譯時看得到 kevin.serverapi.api 底下的 ServerApiRegistryCustomStation。兩種做法擇一,都不要把 ServerAPI 打包(shade)進你的 jar, 它執行期由伺服器上的插件提供。

做法一:JitPack。加 repository 和 com.github 座標,第一次編譯時 JitPack 會自動幫你建好:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.github.Kevin28576</groupId>
    <artifactId>ServerAPI</artifactId>
    <version>v1.1.0</version>
    <scope>provided</scope>
    <exclusions>
        <exclusion><groupId>*</groupId><artifactId>*</artifactId></exclusion>
    </exclusions>
</dependency>

version 填 GitHub 上的 tag 名(例如 v1.1.0)。exclusions 把 ServerAPI 自己的相依全排掉,因為你只要那兩個介面。

做法二:直接複製。ServerApiRegistry.javaCustomStation.java 複製進你的專案。它們只依賴 Bukkit 的 Plugin,複製過去就能編。

ServerApiRegistry

ServerAPI 啟用時會把它登記到 Bukkit 的 ServicesManager,你在自己的 onEnable()(會晚於 ServerAPI)取得:

var rsp = getServer().getServicesManager().getRegistration(ServerApiRegistry.class);
if (rsp == null) return;                 // 沒裝 ServerAPI,就當作沒這回事
ServerApiRegistry api = rsp.getProvider();
方法作用
register(CustomStation)註冊或以相同名稱更新一個站點
unregister(String)取消註冊;停止對外但保留設定檔
isServed(String)查詢站點是否正對外提供(已啟用且掛上)

CustomStation.builder

方法必填作用
builder(name, plugin)開始建立,指定站點名稱與來源插件
.supplier(fn)回傳資料的函式,純 Java 即可
.description(text)顯示在 /api/v1 索引上的說明
.cached(seconds)快照模式:主執行緒每幾秒呼叫一次
.async()即時模式(預設):HTTP 執行緒上呼叫
.build()產生 CustomStation,交給 register

不加 .async().cached() 時,預設就是即時模式。什麼時候該用哪個、 供應者能回傳哪些型別,見第三方站點串接

This page is for plugin developers integrating with ServerAPI: how to add it to your project, and the API you get. For the full walkthrough, choosing a mode, and return types, see Third-party stations.

Adding the dependency

You need ServerApiRegistry and CustomStation from kevin.serverapi.api at compile time. Pick one of two ways, and do not shade ServerAPI into your jar; it is provided at runtime by the plugin on the server.

Option 1: JitPack. Add the repository and the com.github coordinates; JitPack builds it for you on first use:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.github.Kevin28576</groupId>
    <artifactId>ServerAPI</artifactId>
    <version>v1.1.0</version>
    <scope>provided</scope>
    <exclusions>
        <exclusion><groupId>*</groupId><artifactId>*</artifactId></exclusion>
    </exclusions>
</dependency>

version is the GitHub tag name (e.g. v1.1.0). The exclusions strip ServerAPI's own dependencies, since you only want the two interfaces.

Option 2: copy the classes. Copy ServerApiRegistry.java and CustomStation.java into your project. They depend only on Bukkit's Plugin, so they compile as-is.

ServerApiRegistry

ServerAPI registers it into Bukkit's ServicesManager when it enables. Look it up in your own onEnable() (which runs after ServerAPI):

var rsp = getServer().getServicesManager().getRegistration(ServerApiRegistry.class);
if (rsp == null) return;                 // No ServerAPI installed, just skip it
ServerApiRegistry api = rsp.getProvider();
MethodWhat it does
register(CustomStation)Register, or update a station of the same name
unregister(String)Stop serving it, but keep the config file
isServed(String)Whether the station is currently served (enabled and mounted)

CustomStation.builder

MethodRequiredWhat it does
builder(name, plugin)YesStart building; the station name and owning plugin
.supplier(fn)YesThe function returning the data, plain Java
.description(text)NoShown in the /api/v1 index
.cached(seconds)NoCached mode: called on the main thread every few seconds
.async()NoLive mode (default): called on the HTTP thread
.build()YesProduce the CustomStation to pass to register

With neither .async() nor .cached(), the default is live mode. For when to use which, and which return types the supplier may produce, see Third-party stations.

ServerAPI · 僅供非商業用途,商業伺服器需另行取得授權。
ServerAPI · Free for non-commercial use; commercial servers need a separate licence.
Copyright © 2021-2026 CloudXact Studio. All Rights Reserved.