PaperMC 插件开发完全指南 / 第 3 章:插件描述文件
第 3 章:插件描述文件
plugin.yml 是插件的"身份证",决定了服务端如何加载和管理你的插件。
3.1 plugin.yml 基础
plugin.yml 是 Bukkit 插件的核心描述文件,必须放在 src/main/resources/ 根目录下。服务端在加载插件时首先读取这个文件。
完整的 plugin.yml 示例
# 插件名称(必须与 Maven artifactId 一致)
name: MyPlugin
# 插件版本(建议使用 Maven 变量自动填充)
version: '${project.version}'
# 插件主类的完整类名(必须包含包名)
main: com.example.myplugin.MyPlugin
# API 版本(Paper 1.20.5+ 要求,推荐写 1.21)
api-version: '1.21'
# 插件描述
description: 一个功能强大的示例插件
# 作者(可以多个)
author: YourName
# 作者列表(也可以用这个)
authors:
- Author1
- Author2
# 插件网站
website: https://example.com
# 依赖关系
depend:
- Vault # 必须依赖,缺失时服务器不会启动
# 软依赖(可选依赖)
softdepend:
- PlaceholderAPI # 存在时会使用,缺失不影响启动
- WorldGuard
# 前置插件(在此插件之前加载)
loadbefore:
- SomePlugin
# 加载顺序(STARTUP = 尽早加载,POSTWORLD = 世界加载后加载)
load: POSTWORLD
# 前缀(用于日志标识)
prefix: MyPlugin
# 是否使用独立类加载器(Paper 推荐)
folia-supported: true
# 注册的命令
commands:
myplugin:
description: 插件主命令
usage: /myplugin [子命令]
aliases:
- mp
- myp
permission: myplugin.use
permission-message: §c你没有权限使用此命令!
heal:
description: 治疗玩家
usage: /heal [玩家名]
permission: myplugin.heal
# 权限声明
permissions:
myplugin.*:
description: 授予所有 MyPlugin 权限
default: op
children:
myplugin.use: true
myplugin.heal: true
myplugin.admin: true
myplugin.use:
description: 使用基本命令
default: true
myplugin.heal:
description: 治疗自己
default: true
myplugin.admin:
description: 管理员命令
default: op
children:
myplugin.heal.others: true
myplugin.heal.others:
description: 治疗其他玩家
default: op
3.2 各字段详解
核心字段
| 字段 | 必需 | 类型 | 说明 |
|---|---|---|---|
name | ✅ | String | 插件名称,不能含空格 |
version | ✅ | String | 语义化版本号(推荐) |
main | ✅ | String | 主类全限定名 |
api-version | 推荐 | String | API 版本(1.13 ~ 1.21) |
description | ❌ | String | 插件描述(一行) |
author | ❌ | String | 单个作者 |
authors | ❌ | List | 多个作者 |
website | ❌ | String | 网站链接 |
depend | ❌ | List | 必需依赖 |
softdepend | ❌ | List | 可选依赖 |
loadbefore | ❌ | List | 在这些插件之前加载 |
load | ❌ | String | STARTUP 或 POSTWORLD |
prefix | ❌ | String | 日志前缀 |
folia-supported | ❌ | Boolean | 是否兼容 Folia |
api-version 的影响
| api-version | 效果 |
|---|---|
| 未设置 | 按旧版(1.12 之前)方式运行,使用旧的材料名等 |
1.13 | 使用扁平化后的材料名(如 STONE) |
1.14+ | 使用新村民交易等 API |
1.20.5+ | Paper 要求必须设置,使用新的序列化 API |
注意: 不要为了"兼容旧版"而故意设置低版本的
api-version,这会导致使用过时的 API 行为。应始终使用你实际开发针对的版本。
3.3 命令注册详解
在 plugin.yml 中声明命令只是第一步,还需要在代码中注册处理器。
命令声明
commands:
warp:
description: 传送到指定地标
usage: /warp <名称>
aliases:
- warps
- tpwarp
permission: myplugin.warp
permission-message: §c你需要 myplugin.warp 权限!
处理器注册
// 在 onEnable() 中
var warpCmd = getCommand("warp");
if (warpCmd != null) {
WarpCommand executor = new WarpCommand(this);
warpCmd.setExecutor(executor); // 处理执行
warpCmd.setTabCompleter(executor); // Tab 补全
}
防御性编程
// getCommand() 可能返回 null(如果 plugin.yml 中没有声明)
var cmd = getCommand("warp");
if (cmd == null) {
getLogger().severe("命令 'warp' 未在 plugin.yml 中声明!");
getServer().getPluginManager().disablePlugin(this);
return;
}
3.4 权限系统详解
权限树结构
myplugin.* # 通配符,包含所有子权限
├── myplugin.use # 基本使用
├── myplugin.heal # 治疗自己
│ └── myplugin.heal.others # 治疗他人
└── myplugin.admin # 管理员权限
权限默认值
| default 值 | 含义 |
|---|---|
true | 所有玩家默认拥有 |
false | 默认不拥有 |
op | 仅 OP 默认拥有 |
not op | 非 OP 默认拥有 |
运行时检查权限
// 检查玩家是否有权限
if (player.hasPermission("myplugin.admin")) {
// 有管理员权限
}
// 检查是否有通配符权限
if (player.hasPermission("myplugin.*")) {
// 拥有所有权限
}
权限检查的层级行为
当检查 myplugin.heal.others 权限时,服务端会沿权限树向上查找:
myplugin.heal.others → myplugin.heal → myplugin.* → *
如果父节点被授予(或拒绝),子节点也会被影响。
3.5 Paper 插件描述文件(paper-plugin.yml)
Paper 1.19.4+ 支持新的插件描述格式 paper-plugin.yml,提供了更强大的依赖管理。
paper-plugin.yml 示例
name: MyPlugin
version: '1.0.0'
main: com.example.myplugin.MyPlugin
api-version: '1.21'
description: 使用 Paper 原生格式的插件
dependencies:
- id: vault
required: true
bootstrap: false
- id: placeholderapi
required: false
bootstrap: false
permissions:
myplugin.*:
description: 所有权限
default: op
children:
myplugin.use: true
myplugin.admin: true
commands:
myplugin:
description: 主命令
usage: /myplugin
permission: myplugin.use
新旧格式对比
| 特性 | plugin.yml | paper-plugin.yml |
|---|---|---|
| 兼容性 | 所有 Bukkit 服务端 | 仅 Paper 1.19.4+ |
| 依赖声明 | depend / softdepend | 结构化的 dependencies |
| Bootstrap 加载 | 不支持 | 支持(bootstrap: true) |
| 注入式依赖 | 不支持 | 支持(class-loader-load-strategy) |
| 推荐程度 | 通用推荐 | Paper 专属推荐 |
建议: 如果你的插件只运行在 Paper 上,推荐使用
paper-plugin.yml;如果需要兼容 Spigot,使用plugin.yml。
3.6 bungee.yml(BungeeCord 插件描述)
如果你的插件运行在 BungeeCord / Velocity 等代理服务器上,需要 bungee.yml。
bungee.yml 示例
name: MyBungeePlugin
version: '1.0.0'
main: com.example.mybungee.MyBungeePlugin
author: YourName
description: BungeeCord 代理插件
depends:
- SomeOtherPlugin
softDepends:
- OptionalPlugin
区别
| 项目 | plugin.yml | bungee.yml |
|---|---|---|
| 适用服务端 | Bukkit / Spigot / Paper | BungeeCord |
| 主类基类 | JavaPlugin | Plugin |
| API 包名 | org.bukkit.* | net.md_5.bungee.api.plugin.* |
| 命令注册 | yml + setExecutor | yml + onCommand 方法重写 |
3.7 Maven 资源过滤
使用 Maven 变量让版本号自动同步:
在 plugin.yml 中使用变量
name: MyPlugin
version: '${project.version}'
main: com.example.myplugin.MyPlugin
api-version: '${project.properties.api-version}'
pom.xml 配置
<properties>
<api-version>1.21</api-version>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
Gradle 的类似功能
tasks.processResources {
filesMatching("plugin.yml") {
expand(
"version" to project.version,
"apiVersion" to "1.21"
)
}
}
对应 plugin.yml:
version: '${version}'
api-version: '${apiVersion}'
3.8 常见错误排查
| 错误信息 | 原因 | 解决方案 |
|---|---|---|
Plugin requires a newer version of Bukkit | api-version 过高 | 更新 Paper 或降低 api-version |
Could not load plugin.yml | 文件格式错误 | 检查 YAML 缩进和语法 |
main class not found | 类名不匹配 | 确认 main 字段与实际类名一致 |
Duplicate permission | 多个插件声明同名权限 | 改用唯一前缀 |
Circular dependency detected | 依赖循环 | 用 softdepend 打破循环 |
3.9 业务场景:权限设计实战
以一个"传送系统"为例设计权限:
permissions:
teleport.*:
description: 传送系统所有权限
default: op
children:
teleport.warp: true
teleport.warp.create: true
teleport.warp.delete: true
teleport.home: true
teleport.home.set: true
teleport.tp: true
teleport.tp.others: true
teleport.warp:
description: 使用公共地标
default: true
teleport.warp.create:
description: 创建公共地标
default: op
teleport.warp.delete:
description: 删除公共地标
default: op
teleport.home:
description: 传送到家
default: true
teleport.home.set:
description: 设置家的位置
default: true
teleport.tp:
description: 传送到其他玩家
default: true
teleport.tp.others:
description: 传送到任意玩家
default: op
3.10 扩展阅读
3.11 本章小结
| 要点 | 内容 |
|---|---|
| plugin.yml | Bukkit 插件必备,声明名称、主类、命令、权限 |
| api-version | 建议设为目标版本,不要刻意设低 |
| 命令注册 | yml 声明 + 代码中 setExecutor / setTabCompleter |
| 权限设计 | 树形结构,通配符层级,default 控制默认授予 |
| paper-plugin.yml | Paper 新格式,支持 Bootstrap 和高级依赖管理 |
| 资源过滤 | 用 Maven/Gradle 变量自动填充版本号 |
下一章: 第 4 章:命令处理 — 深入掌握命令的注册、处理、Tab 补全和参数解析。