|
16 | 16 | import org.bukkit.event.inventory.InventoryClickEvent; |
17 | 17 | import org.bukkit.event.inventory.InventoryCloseEvent; |
18 | 18 | import org.bukkit.inventory.Inventory; |
| 19 | +import org.bukkit.inventory.InventoryHolder; |
19 | 20 | import org.bukkit.inventory.ItemStack; |
20 | 21 | import org.bukkit.inventory.meta.ItemMeta; |
21 | 22 | import org.bukkit.persistence.PersistentDataContainer; |
|
26 | 27 | import net.kyori.adventure.text.Component; |
27 | 28 | import net.kyori.adventure.text.minimessage.MiniMessage; |
28 | 29 |
|
29 | | -public class LandmarkMenu implements Listener { |
| 30 | +public class LandmarkMenu implements InventoryHolder, Listener { |
30 | 31 |
|
31 | 32 | private static final String LANDMARK_KEY = "landmark_name"; |
32 | 33 | private final LandmarkPlugin plugin; |
33 | 34 | private final Player player; |
34 | | - private final Inventory inventory; |
| 35 | + private Inventory inventory; |
35 | 36 | private final MiniMessage miniMessage = MiniMessage.miniMessage(); |
36 | 37 | private final NamespacedKey landmarkKey; |
37 | 38 |
|
38 | 39 | public LandmarkMenu(LandmarkPlugin plugin, Player player) { |
39 | 40 | this.plugin = plugin; |
40 | 41 | this.player = player; |
41 | 42 | this.landmarkKey = new NamespacedKey(plugin, LANDMARK_KEY); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public Inventory getInventory() { |
| 47 | + return inventory; |
| 48 | + } |
| 49 | + |
| 50 | + public void open() { |
42 | 51 | Component title = plugin.getConfigManager().getMessage("gui.title", "<gold>锚点传送菜单</gold>"); |
43 | 52 | int size = plugin.getConfigManager().getConfig().getInt("gui.size", 54); |
44 | | - this.inventory = Bukkit.createInventory(null, size, title); |
45 | | - |
| 53 | + this.inventory = Bukkit.createInventory(this, size, title); |
46 | 54 | initializeItems(); |
| 55 | + player.openInventory(inventory); |
47 | 56 | } |
48 | 57 |
|
49 | 58 | private void initializeItems() { |
@@ -281,57 +290,37 @@ private void addLockedLore(List<Component> lore, Landmark landmark) { |
281 | 290 | } |
282 | 291 | } |
283 | 292 |
|
284 | | - private void updateMenu() { |
285 | | - inventory.clear(); |
286 | | - initializeItems(); |
287 | | - } |
288 | | - |
289 | | - public void open() { |
290 | | - // 在打开菜单时注册监听器并更新内容 |
291 | | - plugin.getServer().getPluginManager().registerEvents(this, plugin); |
292 | | - updateMenu(); |
293 | | - player.openInventory(inventory); |
294 | | - } |
295 | | - |
296 | | - public static void handleClick(InventoryClickEvent event) { |
297 | | - if (!(event.getWhoClicked() instanceof Player player)) { |
298 | | - return; |
299 | | - } |
300 | | - |
301 | | - LandmarkPlugin plugin = LandmarkPlugin.getInstance(); |
302 | | - if (!event.getView().title().equals(plugin.getConfigManager().getMessage("gui.title", "<gold>锚点传送菜单</gold>"))) { |
303 | | - return; |
304 | | - } |
305 | | - |
| 293 | + public void onClick(int slot, InventoryClickEvent event) { |
306 | 294 | event.setCancelled(true); |
307 | 295 | ItemStack clickedItem = event.getCurrentItem(); |
308 | 296 | if (clickedItem == null || clickedItem.getType() == Material.AIR) { |
309 | 297 | return; |
310 | 298 | } |
311 | 299 |
|
312 | 300 | try { |
313 | | - ItemMeta meta = clickedItem.getItemMeta(); |
314 | | - if (meta == null) { |
315 | | - return; |
316 | | - } |
317 | | - |
318 | | - PersistentDataContainer container = meta.getPersistentDataContainer(); |
319 | | - String landmarkName = container.get(new NamespacedKey(plugin, LANDMARK_KEY), PersistentDataType.STRING); |
320 | | - |
321 | | - if (landmarkName != null && plugin.getLandmarkManager().isLandmarkUnlocked(player, landmarkName)) { |
322 | | - plugin.getServer().getGlobalRegionScheduler().execute(plugin, () -> { |
323 | | - if (event.getInventory().getHolder() instanceof LandmarkMenu menu) { |
324 | | - menu.cleanup(); |
325 | | - } |
326 | | - player.closeInventory(); |
327 | | - plugin.getLandmarkManager().teleport(player, landmarkName); |
328 | | - }); |
329 | | - } |
| 301 | + handleItemClick(clickedItem, (Player) event.getWhoClicked()); |
330 | 302 | } catch (Exception e) { |
331 | 303 | plugin.getSLF4JLogger().error("GUI传送处理出错: {}", e.getMessage(), e); |
332 | 304 | } |
333 | 305 | } |
334 | 306 |
|
| 307 | + private void handleItemClick(ItemStack item, Player player) { |
| 308 | + ItemMeta meta = item.getItemMeta(); |
| 309 | + if (meta == null) { |
| 310 | + return; |
| 311 | + } |
| 312 | + |
| 313 | + PersistentDataContainer container = meta.getPersistentDataContainer(); |
| 314 | + String landmarkName = container.get(landmarkKey, PersistentDataType.STRING); |
| 315 | + |
| 316 | + if (landmarkName != null && plugin.getLandmarkManager().isLandmarkUnlocked(player, landmarkName)) { |
| 317 | + plugin.getServer().getGlobalRegionScheduler().execute(plugin, () -> { |
| 318 | + player.closeInventory(); |
| 319 | + plugin.getLandmarkManager().teleport(player, landmarkName); |
| 320 | + }); |
| 321 | + } |
| 322 | + } |
| 323 | + |
335 | 324 | // 添加关闭菜单时的清理方法 |
336 | 325 | private void cleanup() { |
337 | 326 | // 清理物品栏内容 |
|
0 commit comments