GUIEngine
GUIEngine 是一个轻量级、灵活的 API,用于在 Minecraft Paper 服务器(1.21.4)中创建交互式 GUI 菜单。与其在每个插件的菜单中编写重复的库存代码,GUIEngine 提供了一个干净、可重用的系统来创建丰富的交互界面。
用途
GUIEngine 设计为其他插件的依赖项,提供标准化的方式来:
- 创建带有交互按钮的自定义库存菜单
- 处理玩家点击和交互
- 管理菜单导航和状态
- 创建动态、响应式的界面
它的功能类似于 Vault 或 ProtocolLib 等其他实用插件,但专注于 GUI 管理。
主要特性
- 简单的 API:用少量代码即可创建复杂菜单
- 交互按钮:添加带有自定义操作的 clickable 按钮
- 菜单导航:轻松处理子菜单和菜单链接
- 动态更新:实时更新菜单内容
- 构建器模式:使用干净流畅的 API 创建菜单
- 资源管理:自动清理资源
- 事件处理:与 Bukkit 的事件系统正确集成
- 清晰的架构:组织良好的代码,便于扩展
工作原理
GUIEngine 由五个核心组件组成:
1. GUIEngine (核心)
主插件类,负责初始化系统、注册事件监听器,并为其他插件提供访问点。
2. GUI (菜单)
代表一个带有按钮槽位的库存菜单。每个 GUI 都处理自己的布局和按钮映射。
3. GUIButton (按钮)
菜单内的交互物品,点击时可触发操作。按钮可以执行代码、发送消息、打开其他菜单等。
4. GUIManager (状态管理器)
跟踪每个玩家当前打开的菜单,并管理菜单的打开/关闭。
5. InventoryListener (事件处理器)
拦截库存事件并将其路由到适当的 GUI 组件。
使用方法
作为依赖项添加
首先,在你的 plugin.yml 中将 GUIEngine 添加为依赖项:
depend: [GUIEngine]
然后,将其添加到你的 Maven/Gradle 构建文件中。
对于 Maven:
<dependencies>
<dependency>
<groupId>com.gui.engine</groupId>
<artifactId>gui-engine</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
创建基本菜单
以下是一个显示如何创建和显示菜单的简单示例:
// 创建一个包含 3 行(27 个槽位)的新 GUI
GUI myMenu = new GUI("§6§lMy Menu", 3);
// 添加一个点击时发送消息的按钮
GUIButton messageButton = new GUIButton(
Material.PAPER,
"§e§lClick Me",
Arrays.asList("§7Click to receive", "§7a message"),
(player, gui, button, slot) -> {
player.sendMessage("§aYou clicked the button!");
return true;
}
);
// 添加一个关闭菜单的按钮
GUIButton closeButton = new GUIButton(
Material.BARRIER,
"§c§lClose",
(player, gui, button, slot) -> {
player.closeInventory();
return true;
}
);
// 将按钮放置在特定槽位
myMenu.setButton(13, messageButton);
myMenu.setButton(26, closeButton);
// 为玩家打开菜单
myMenu.open(player);
使用构建器模式
你也可以使用构建器模式来更简洁地创建菜单:
GUI menu = new GUI.Builder()
.title("§6§lMy Menu")
.rows(3)
.build();
// 然后像往常一样添加按钮并打开
动态按钮更新
按钮可以根据点击进行自我更新:
GUIButton counterButton = new GUIButton(
Material.EMERALD,
"§a§lClicks: §f0",
(player, gui, button, slot) -> {
// 从名称中提取当前计数
String name = ChatColor.stripColor(button.getItem().getItemMeta().getDisplayName());
int count = Integer.parseInt(name.split(": ")[1]) + 1;
// 更新按钮名称
button.setName("§a§lClicks: §f" + count);
// 更新库存显示
gui.updateInventory();
return true;
}
);
子菜单和导航
你可以轻松地在菜单之间创建导航系统:
// 在你的主菜单创建中:
GUIButton settingsButton = new GUIButton(
Material.REDSTONE_TORCH,
"§b§lSettings",
(player, gui, button, slot) -> {
openSettingsMenu(player); // 你创建/打开设置菜单的方法
return true;
}
);
// 在你的设置菜单中:
GUIButton backButton = new GUIButton(
Material.ARROW,
"§e§lBack",
(player, gui, button, slot) -> {
openMainMenu(player); // 你创建/打开主菜单的方法
return true;
}
);
高级特性
自定义按钮操作
你可以创建不同类型的按钮操作:
// 传送按钮
GUIButton teleportButton = new GUIButton(
Material.ENDER_PEARL,
"§5§lTeleport to Spawn",
(player, gui, button, slot) -> {
player.closeInventory();
player.teleport(player.getWorld().getSpawnLocation());
player.sendMessage("§aTeleported to spawn!");
return true;
}
);
// 命令执行按钮
GUIButton commandButton = new GUIButton(
Material.COMMAND_BLOCK,
"§c§lExecute Command",
(player, gui, button, slot) -> {
player.closeInventory();
player.performCommand("spawn");
return true;
}
);
物品构建器
使用 GUIButton 构建器以更简洁的方式创建按钮:
GUIButton button = new GUIButton.Builder()
.material(Material.DIAMOND)
.name("§b§lPremium Features")
.lore("§7Click to view premium options")
.lore("§7Cost: $10")
.onClick((player, gui, button, slot) -> {
// 处理点击
return true;
})
.build();
最佳实践
- 资源管理:当你的插件禁用时,关闭 GUI。
- 错误处理:在处理库存时检查空值。
- 性能:按需创建菜单,而不是预先创建所有可能的菜单。
- 组织性:在你的插件中为所有 GUI 创建一个单独的管理器类。
- 玩家数据:将玩家特定的数据存储在外部的 GUI 本身之外。
完整示例
有关创建菜单、子菜单和各种按钮类型的完整示例,请参阅 GUIEngine 源代码中的 ExampleGUI.java 类。
许可证
GUIEngine 根据 MIT 许可证授权。您可以自由使用、修改和分发。
GUIEngine
GUIEngine is a lightweight, flexible API for creating interactive GUI menus in Minecraft Paper servers (1.21.4). Instead of writing repetitive inventory code for each menu in your plugins, GUIEngine provides a clean, reusable system to create rich interactive interfaces.
Purpose
GUIEngine is designed as a dependency for other plugins, providing a standardized way to:
- Create custom inventory menus with interactive buttons
- Handle player clicks and interactions
- Manage menu navigation and state
- Create dynamic, responsive interfaces
It functions similar to other utility plugins like Vault or ProtocolLib, but focused exclusively on GUI management.
Key Features
- Simple API: Create complex menus with minimal code
- Interactive Buttons: Add clickable buttons with custom actions
- Menu Navigation: Easily handle sub-menus and menu linking
- Dynamic Updates: Update menu contents in real-time
- Builder Pattern: Create menus using a clean, fluent API
- Resource Management: Automatic cleanup of resources
- Event Handling: Properly integrates with Bukkit's event system
- Clean Architecture: Well-organized code for easy extension
How It Works
GUIEngine consists of five core components:
1. GUIEngine (Core)
The main plugin class that initializes the system, registers event listeners, and provides access points for other plugins.
2. GUI (Menu)
Represents an inventory menu with slots for buttons. Each GUI handles its own layout and button mapping.
3. GUIButton (Buttons)
Interactive items within menus that can trigger actions when clicked. Buttons can execute code, send messages, open other menus, etc.
4. GUIManager (State Manager)
Keeps track of which menu is open for each player and manages the opening/closing of menus.
5. InventoryListener (Event Handler)
Intercepts inventory events and routes them to the appropriate GUI components.
How to Use
Adding as a Dependency
First, add GUIEngine as a dependency in your plugin.yml:
depend: [GUIEngine]
Then, add it to your Maven/Gradle build file.
For Maven:
<dependencies>
<dependency>
<groupId>com.gui.engine</groupId>
<artifactId>gui-engine</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
Creating a Basic Menu
Here's a simple example showing how to create and display a menu:
// Create a new GUI with 3 rows (27 slots)
GUI myMenu = new GUI("§6§lMy Menu", 3);
// Add a button that sends a message when clicked
GUIButton messageButton = new GUIButton(
Material.PAPER,
"§e§lClick Me",
Arrays.asList("§7Click to receive", "§7a message"),
(player, gui, button, slot) -> {
player.sendMessage("§aYou clicked the button!");
return true;
}
);
// Add a button that closes the menu
GUIButton closeButton = new GUIButton(
Material.BARRIER,
"§c§lClose",
(player, gui, button, slot) -> {
player.closeInventory();
return true;
}
);
// Place buttons in specific slots
myMenu.setButton(13, messageButton);
myMenu.setButton(26, closeButton);
// Open the menu for a player
myMenu.open(player);
Using the Builder Pattern
You can also use the builder pattern for cleaner menu creation:
GUI menu = new GUI.Builder()
.title("§6§lMy Menu")
.rows(3)
.build();
// Then add buttons and open as normal
Dynamic Button Updates
Buttons can update themselves in response to clicks:
GUIButton counterButton = new GUIButton(
Material.EMERALD,
"§a§lClicks: §f0",
(player, gui, button, slot) -> {
// Extract current count from name
String name = ChatColor.stripColor(button.getItem().getItemMeta().getDisplayName());
int count = Integer.parseInt(name.split(": ")[1]) + 1;
// Update button name
button.setName("§a§lClicks: §f" + count);
// Update the inventory display
gui.updateInventory();
return true;
}
);
Sub-Menus and Navigation
You can easily create navigation systems between menus:
// In your main menu creation:
GUIButton settingsButton = new GUIButton(
Material.REDSTONE_TORCH,
"§b§lSettings",
(player, gui, button, slot) -> {
openSettingsMenu(player); // Your method to create/open settings menu
return true;
}
);
// And in your settings menu:
GUIButton backButton = new GUIButton(
Material.ARROW,
"§e§lBack",
(player, gui, button, slot) -> {
openMainMenu(player); // Your method to create/open main menu
return true;
}
);
Advanced Features
Custom Button Actions
You can create different types of button actions:
// Teleport button
GUIButton teleportButton = new GUIButton(
Material.ENDER_PEARL,
"§5§lTeleport to Spawn",
(player, gui, button, slot) -> {
player.closeInventory();
player.teleport(player.getWorld().getSpawnLocation());
player.sendMessage("§aTeleported to spawn!");
return true;
}
);
// Command execution button
GUIButton commandButton = new GUIButton(
Material.COMMAND_BLOCK,
"§c§lExecute Command",
(player, gui, button, slot) -> {
player.closeInventory();
player.performCommand("spawn");
return true;
}
);
Item Builders
Use the GUIButton builder for even cleaner button creation:
GUIButton button = new GUIButton.Builder()
.material(Material.DIAMOND)
.name("§b§lPremium Features")
.lore("§7Click to view premium options")
.lore("§7Cost: $10")
.onClick((player, gui, button, slot) -> {
// Handle click
return true;
})
.build();
Best Practices
- Resource Management: Close GUIs when your plugin disables.
- Error Handling: Check for null values when dealing with inventories.
- Performance: Create menus on-demand rather than pre-creating all possible menus.
- Organization: Create a separate manager class in your plugin for all your GUIs.
- Player Data: Store player-specific data externally, not in the GUI itself.
Complete Example
See the ExampleGUI.java class in the GUIEngine source code for a complete example of creating menus, submenus, and various button types.
License
GUIEngine is licensed under the MIT License. Feel free to use, modify, and distribute as needed.
评论 0
登录后参与评分
还没有评论,来抢沙发吧