Cnzw 发表于 2024-5-21 18:16:54

[1.16-1.19] ServerTap - 通过API获取玩家信息和操作服务器

本帖最后由 Cnzw 于 2024-5-23 18:38 编辑



https://s21.ax1x.com/2024/05/23/pkQMage.png

插件简介
ServerTap 是一个支持 Bukkit、Spigot 和 Paper 服务器的 REST API 插件。 它允许通过 HTTP、Webhooks 和 Websockets 与服务器进行交互。

@Cnzw
该插件很适合用于 QQ 机器人和各种状态监测网页。

安装要求

[*]Bukkit/Spigot/Paper 或其分支:1.16.5 或更高版本
[*]Java:16 或更高版本
[*]无前置插件


如何使用
将本插件文件复制到服务器的 plugins 文件夹中即完成了安装。

然后可以使用 Curl 或 Postman 或任意浏览器来访问 API。

例如,查询有关服务器信息的 API:

$ curl http: //localhost:4567/v1/server
{
    "name": "Paper",
    "motd": "This is my MOTD",
    "version": "git-Paper-89 (MC: 1.15.2)",
    "bukkitVersion": "1.15.2-R0.1-SNAPSHOT",
    "health": {
      "cpus": 4,
      "uptime": 744,
      "totalMemory": 2010644480,
      "maxMemory": 2010644480,
      "freeMemory": 1332389360
    },
    "bannedIps": [],
    "bannedPlayers": [{
      "target": "phybros",
      "source": "Server"
    }]
}


或查询在线玩家列表的 API:

$ curl http://localhost:4567/v1/players
[{
    "uuid": "55f584e4-f095-48e0-bb8a-eb5c87ffe494",
    "displayName": "phybros",
    "address": "localhost",
    "port": 58529,
    "exhaustion": 3.5640976,
    "exp": 0.45454547,
    "whitelisted": false,
    "banned": false,
    "op": true
}]


API 文档
插件自带 API 文档。可以在 http://服务器IP:4567/swagger 查看完整的 API 文档并且直接调试。

@Cnzw
我直接导出了API接口,在这里查看 。

API 验证
在 plugins/ServerTap/config.yml 文件中进行配置:

useKeyAuth: true
key: some-long-super-random-string

然后,在需要在请求头(Header)中包含 key 和对应的密钥。

WebHook
ServerTap 可以在服务器上发生事件时发送 WebHook 消息。

要使用 WebHook,需要配置 plugins/ServerTap/config.yml 文件:

webhooks:
default:
    listener: "https://your-webhook-target.com/whatever"
    events:
    - PlayerJoin
    - PlayerQuit

目前可用的事件有:

[*]PlayerJoin
[*]PlayerDeath
[*]PlayerChat
[*]PlayerKick
[*]PlayerQuit


WebHook 通过 POST 请求发送 JSON 数据:

{
"player": {
    "uuid": "55f584e4-f095-48e0-bb8a-eb5c87ffe494",
    "displayName": "phybros",
    "address": "localhost",
    "port": 52809,
    "exhaustion": 0,
    "exp": 0.5714286,
    "whitelisted": true,
    "banned": false,
    "op": true
},
"joinMessage": "§ephybros joined the game",
"eventType": "PlayerJoin"
}


Websockets
ServerTap 支持正向 WebSocket ,能够实时接收服务器日志。

一旦连接,服务器上通过正常日志过滤器的任何服务器日志行都会以类似这样的 JSON 对象形式通过 WebSocket 发送:

{
"message": "§6/version: §fGets the version of this server including any plugins in use",
"timestampMillis": 1631834015918,
"loggerName": "",
"level": "INFO"
}

WebSocket 地址是 ws://服务器IP:4567/v1/ws/console。

最后 1000 条服务器日志消息将缓存并在客户端连接时发送。您可以通过更改 config.yml 中的 websocketConsoleBuffer 来配置服务器日志缓冲区的大小。

使用统计
https://bstats.org/signatures/bukkit/ServerTap.svg
https://pump.goatcounter.com/count?p=www.mcshuo.com/thread-2263-1-1.html&r=www.mcshuo.com&q=utm_campaign%3DServerTap

更多
本插件已加入 插件搬运计划,会定期同步外网资源的更新。

搬运催更 & 问题反馈:快速响应问卷 或在下方留下评论。
https://s21.ax1x.com/2024/05/23/pkQQmVI.png
页: [1]
查看完整版本: [1.16-1.19] ServerTap - 通过API获取玩家信息和操作服务器