强曰为道

与天地相似,故不违。知周乎万物,而道济天下,故不过。旁行而不流,乐天知命,故不忧.
文档目录

第 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 各字段详解

核心字段

字段必需类型说明
nameString插件名称,不能含空格
versionString语义化版本号(推荐)
mainString主类全限定名
api-version推荐StringAPI 版本(1.13 ~ 1.21
descriptionString插件描述(一行)
authorString单个作者
authorsList多个作者
websiteString网站链接
dependList必需依赖
softdependList可选依赖
loadbeforeList在这些插件之前加载
loadStringSTARTUPPOSTWORLD
prefixString日志前缀
folia-supportedBoolean是否兼容 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.ymlpaper-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.ymlbungee.yml
适用服务端Bukkit / Spigot / PaperBungeeCord
主类基类JavaPluginPlugin
API 包名org.bukkit.*net.md_5.bungee.api.plugin.*
命令注册yml + setExecutoryml + 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 Bukkitapi-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.ymlBukkit 插件必备,声明名称、主类、命令、权限
api-version建议设为目标版本,不要刻意设低
命令注册yml 声明 + 代码中 setExecutor / setTabCompleter
权限设计树形结构,通配符层级,default 控制默认授予
paper-plugin.ymlPaper 新格式,支持 Bootstrap 和高级依赖管理
资源过滤用 Maven/Gradle 变量自动填充版本号

下一章: 第 4 章:命令处理 — 深入掌握命令的注册、处理、Tab 补全和参数解析。