AdvancementInfo
AdvancementInfo API 是一个专用库,旨在简化在插件中检索和操作 Minecraft 进度(advancements)的过程。它通过提供统一的接口以及多个使用反射机制来兼容 Bukkit、Paper 和其他服务器内部结构的实现类,抽象了访问进度数据(如标题、描述、图标、标准、奖励和显示设置)的底层复杂性。
概述
该 API 的核心是 AdvancementInfo 接口,它定义了获取进度详细信息的方法。BukkitInfoImpl、ReflectInfoImpl 和 PaperInfoImpl 等实现类通过反射从 Bukkit 的 Advancement 对象中提取数据,并提供:
- 进度详情: 标题、描述、图标和显示选项。
- 显示坐标: 进度在界面上的 X 和 Y 位置。
- 视觉框架: 进度的框架类型(例如 TASK、GOAL、CHALLENGE)。
- 标准和需求: 定义如何达成进度的数据。
- 奖励: 完成时获得的奖励信息。
主要功能
-
统一接口:
AdvancementInfo 接口提供了访问进度数据的通用 API,无论底层实现如何。
-
多种实现:
不同的实现适应各种服务器版本和环境:
- BukkitInfoImpl: 使用 Bukkit 的标准进度显示方法。
- ReflectInfoImpl: 利用反射提取高级详细信息。
- PaperInfoImpl: 专为 Paper 服务器定制,使用 Paper 特定方法和 LegacyComponentSerializer 进行文本转换。
-
版本兼容性:
API 会根据服务器的 Minecraft 版本智能选择正确的实现,确保广泛的兼容性。
-
基于反射:
反射的使用使 API 能够访问进度的内部字段和方法,即使在 Bukkit API 未直接暴露的情况下也能提供丰富的信息。
使用示例
以下示例展示了如何使用 AdvancementInfo API 检索并显示 Minecraft 进度的信息。
示例:检索进度信息
package com.example.myplugin;
import me.croabeast.advancement.AdvancementInfo;
import org.bukkit.Bukkit;
import org.bukkit.advancement.Advancement;
import org.bukkit.plugin.java.JavaPlugin;
public class MyPlugin extends JavaPlugin {
@Override
public void onEnable() {
// 通过键值检索 Bukkit 进度
Advancement advancement = Bukkit.getAdvancement(new org.bukkit.NamespacedKey(this, "example_advancement"));
if (advancement != null) {
// 使用静态工厂方法从进度创建 AdvancementInfo 实例
AdvancementInfo info = AdvancementInfo.create(advancement);
if (info != null) {
// 将进度详情打印到控制台
getLogger().info("进度标题: " + info.getTitle());
getLogger().info("描述: " + info.getDescription());
// 获取格式化后的描述数组,最大行长度为 40 个字符
String[] lines = info.getDescriptionArray(40);
for (String line : lines) {
getLogger().info("行: " + line);
}
} else {
getLogger().warning("未能检索进度信息。");
}
} else {
getLogger().warning("未找到进度。");
}
}
}
说明
-
检索进度:
示例通过键值获取 Bukkit Advancement。
-
使用工厂方法:
AdvancementInfo.from(advancement) 会根据服务器版本返回适当的实现(Bukkit、Paper 或 Reflect)。
-
访问进度数据:
一旦获得 AdvancementInfo 实例,您就可以访问其标题、描述、标准、奖励和显示设置。
-
格式化描述:
示例演示了如何将描述拆分为行数组,确保没有一行超过指定的长度。
Maven / Gradle 安装
要将 AdvancementInfo 包含到项目中,请将以下仓库和依赖项添加到您的构建配置中。将 ${version} 替换为所需的版本标签。
Maven
将仓库和依赖项添加到您的 pom.xml 中:
<repositories>
<repository>
<id>croabeast-repo</id>
<url>https://croabeast.github.io/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>me.croabeast</groupId>
<artifactId>AdvancementInfo</artifactId>
<version>${version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
Gradle
将仓库和依赖项添加到您的 build.gradle 中:
repositories {
maven {
url "https://croabeast.github.io/repo/"
}
}
dependencies {
implementation "me.croabeast:AdvancementInfo:${version}"
}
将 ${version} 替换为相应的模块版本。
结论
AdvancementInfo API 提供了一种强大且统一的方式来访问和处理 Minecraft 进度。它抽象化了反射和服务器版本差异带来的复杂性,让你能够专注于在插件中使用进度数据。无论你是构建自定义进度显示、将进度数据集成到插件逻辑中,还是仅仅记录进度详情,该 API 都能让你以一致的方式更轻松地处理进度。
编码愉快!
— CroaBeast
AdvancementInfo
AdvancementInfo API is a specialized library designed to simplify the retrieval and manipulation of Minecraft advancements in your plugins. It abstracts the underlying complexity of accessing advancement data—such as titles, descriptions, icons, criteria, rewards, and display settings—by providing a uniform interface and several concrete implementations that use reflection to work with Bukkit, Paper, and other server internals.
Overview
The API is centered around the AdvancementInfo interface, which defines methods for obtaining detailed information about an advancement. Implementations like BukkitInfoImpl, ReflectInfoImpl, and PaperInfoImpl extract data from a Bukkit Advancement object using reflection and provide:
- Advancement Details: Title, description, icon, and display options.
- Display Coordinates: X and Y positions for advancement display.
- Visual Frame: The advancement’s frame type (e.g., TASK, GOAL, CHALLENGE).
- Criteria and Requirements: Data that defines how the advancement is achieved.
- Rewards: Information on the rewards granted upon completion.
Key Features
-
Unified Interface:
The AdvancementInfo interface provides a common API to access advancement data, regardless of the underlying implementation.
-
Multiple Implementations:
Different implementations adapt to various server versions and environments:
- BukkitInfoImpl: Uses Bukkit’s standard advancement display methods.
- ReflectInfoImpl: Leverages reflection to extract advanced details.
- PaperInfoImpl: Tailored for Paper servers, using Paper-specific methods and the LegacyComponentSerializer for text conversion.
-
Version Compatibility:
The API intelligently selects the correct implementation based on the server’s Minecraft version, ensuring broad compatibility.
-
Reflection Based:
The use of reflection enables the API to access internal fields and methods of advancements, providing rich information even when not directly exposed by the Bukkit API.
Usage Example
Below is an example of how to use the AdvancementInfo API to retrieve and display information about a Minecraft advancement.
Example: Retrieving Advancement Information
package com.example.myplugin;
import me.croabeast.advancement.AdvancementInfo;
import org.bukkit.Bukkit;
import org.bukkit.advancement.Advancement;
import org.bukkit.plugin.java.JavaPlugin;
public class MyPlugin extends JavaPlugin {
@Override
public void onEnable() {
// Retrieve a Bukkit advancement by its key
Advancement advancement = Bukkit.getAdvancement(new org.bukkit.NamespacedKey(this, "example_advancement"));
if (advancement != null) {
// Create an AdvancementInfo instance from the advancement using the static factory method
AdvancementInfo info = AdvancementInfo.create(advancement);
if (info != null) {
// Print advancement details to the console
getLogger().info("Advancement Title: " + info.getTitle());
getLogger().info("Description: " + info.getDescription());
// Retrieve formatted description array with a max line length of 40 characters
String[] lines = info.getDescriptionArray(40);
for (String line : lines) {
getLogger().info("Line: " + line);
}
} else {
getLogger().warning("Failed to retrieve advancement info.");
}
} else {
getLogger().warning("Advancement not found.");
}
}
}
Explanation
-
Retrieving an Advancement:
The example obtains a Bukkit Advancement via its key.
-
Using the Factory Method:
AdvancementInfo.from(advancement) returns an appropriate implementation (Bukkit, Paper, or Reflect) based on the server version.
-
Accessing Advancement Data:
Once the AdvancementInfo instance is obtained, you can access its title, description, criteria, rewards, and display settings.
-
Formatting Description:
The example demonstrates how to split the description into an array of lines, ensuring that no line exceeds a specified length.
Maven / Gradle Installation
To include AdvancementInfo to the project, add the following repository and dependency to your build configuration. Replace ${version} with the desired version tag.
Maven
Add the repository and dependency to your pom.xml:
<repositories>
<repository>
<id>croabeast-repo</id>
<url>https://croabeast.github.io/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>me.croabeast</groupId>
<artifactId>AdvancementInfo</artifactId>
<version>${version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
Gradle
Add the repository and dependency to your build.gradle:
repositories {
maven {
url "https://croabeast.github.io/repo/"
}
}
dependencies {
implementation "me.croabeast:AdvancementInfo:${version}"
}
Replace ${version} with the appropriate module version.
Conclusion
The AdvancementInfo API provides a powerful, unified approach to accessing and processing Minecraft advancements. It abstracts away the complexities of reflection and server version differences, allowing you to focus on using the advancement data in your plugin. Whether you are building custom advancement displays, integrating advancement data into your plugin logic, or simply logging advancement details, this API makes it easier to work with advancements in a consistent manner.
Happy coding!
— CroaBeast
评论 0
登录后参与评分
还没有评论,来抢沙发吧