Skip to content

作为依赖 ​

若要在你的插件中使用 WorldGuard 的 API,你必须将 WorldGuard 作为依赖导入。它可作为必选或可选的前置。

API 版本 ​

在主流版本下(5.x、6.x、7.x),WorldGuard 的 API 非常稳定。弃用特定内容会提前三个月公布,但一般正式废除会将其延长至六个月。

API 当前支持的版本为 7.x。旧版本的 API(以及 Minecraft)不再会接收到更新提醒或是技术支持。你可以使用右上角的“版本选择”按钮来找到旧版本的文档,但是其中可能包含改动或错误。

构建依赖脚本 ​

如果你正在使用 Maven 或者 Gradle(必须这么做!)等方式编译你的插件或模组,你就需要将 WorldGuard 添加至依赖列表。你可以在 sk89q 的 Maven 仓库中找到 WorldGuard 的构建品。

需要注意的是 WorldGuard 所使用的一些对象是来自于 WorldEdit 的。如果你需要直接使用它们,你也应当将 WorldEdit 用作编译时依赖(尽管它有可能根据你的构建配置被自动添加)。

  • Maven 仓库:https://maven.enginehub.org/repo/
  • 构建成品:com.sk89q.worldguard:worldguard-bukkit:VERSION(VERSION 处替换为你所需要的 WorldGuard 版本);需要注意的是这也包含 worldguard-code 构建的 API。

Maven 仓库应当是全天在线的,它也是 Minecraft 历史上运行时间最长的 Maven 仓库。如果遇到问题,请参阅“获取帮助”章节。

示例:配置一个 Maven 的 pom.xml

XML
<repositories>
    <repository>
        <id>sk89q-repo</id>
        <url>https://maven.enginehub.org/repo/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>com.sk89q.worldguard</groupId>
        <artifactId>worldguard-bukkit</artifactId>
        <version>VERSION</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

示例:配置一个 Gradle 的构建脚本

Kotlin
repositories {
    mavenCentral()
    maven { url "https://maven.enginehub.org/repo/" }
}

dependencies {
    compileOnly 'com.sk89q.worldguard:worldguard-bukkit:VERSION'
}

修改 plugin.yml ​

首先,在 plugin.yml 下指定 WorldGuard 为“硬依赖”或“软依赖”是非常重要的,这能让 Bukkit 知道你的插件是否需要在 WorldGuard 之后加载;

YAML
name: My Plugin
version: 1.0
description: 这是我的插件!
depend: [WorldGuard]

如果你需要将它设置为软依赖,则安装 WorldGuard 的情况下它会先于你的插件加载,否则按一般顺序加载。

通过自己的插件与 WorldGuard 交互 ​

大多数 WorldGuard API 可以通过 WorldGuard.getInstance() 方法进行交互。 也可以通过 WorldGuardPlugin.inst() 下的 wrapPlayer 方法修改玩家。见“引自 Bukkit 的对象”章节了解更多。

需要注意的是,在访问 WorldGuard 的特定部分时,你必须事先保留它的内部 API。

软依赖关系的类路径问题 ​

如果你正在以 WorldGuard 为硬前置,无需担心 WorldGuard 类运行时不存在。除非 WorldGuard 被设置为软依赖。

比如,当你尝试:

Java
class MyPlugin {
    public void onEnable() {
        ProtectedCuboidRegion region = new ProtectedCuboidRegion(...);
    }
}

你的插件会因为 ProtectedCuboidRegion 不存在而无法载入。插件管理器也不能调用 onEnable()。其中一种解决方法是将下列代码放入一个完全不同的类中:

Java
class RegionHolder {
    private final ProtectedCuboidRegion region;

    public RegionHolder() {
        region = new ProtectedCuboidRegion(...);
    }
}

在你遇到类似问题且无法创建 RegionHolder 时,至少你可以从另一个类中尝试捕获错误:

Java
class MyPlugin {
    public void onEnable() {
        try {
            new RegionHolder();
        } catch (NoClassDefFoundError e) {
            // 在此处填入代码
        }
    }
}

但是,这种问题也不会在所有情况中出现。链式方法通常从静态方法调用开始,可以用于防止含有它的类载入失败:

Java
class MyPlugin {
    public void onEnable() {
        try {
            boolean result = SomeClass.staticMethod();
        } catch (NoClassDefFoundError e) {
            // 在此处填入代码
        }
    }
}

如果你在插件中使用了任意形式的软依赖,非常建议先在不安装它们的情况下调试一次。

贡献者

页面历史