From 84b2c14f6d746fa0e043c61b547ec737f074d559 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Mon, 5 Aug 2024 22:22:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=80=92=E5=BD=92Bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bukkit/module/DynamicLight.java | 7 ++- .../bukkit/module/QuickShulkerBox.java | 53 ++++++++++--------- .../invactions/bukkit/module/RootModule.java | 3 ++ 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/main/java/me/xpyex/plugin/invactions/bukkit/module/DynamicLight.java b/src/main/java/me/xpyex/plugin/invactions/bukkit/module/DynamicLight.java index 8a2f9f0..4636f54 100644 --- a/src/main/java/me/xpyex/plugin/invactions/bukkit/module/DynamicLight.java +++ b/src/main/java/me/xpyex/plugin/invactions/bukkit/module/DynamicLight.java @@ -22,13 +22,18 @@ public class DynamicLight extends RootModule { private static final String[] LIGHTS = {"LANTERN", "TORCH", "GLOW", "ShroomLight", "FrogLight", "END_ROD", "CampFire", "LAVA"}; private static BlockData LIGHT_DATA; + @Override + public void registerCustomListener() { + registerTask(); + // + } + @Override protected boolean canLoad() { try { Block.class.getMethod("getBlockData"); //1.13+ Material light = Material.getMaterial("LIGHT"); //1.17的光源方块. LIGHT_DATA = Bukkit.createBlockData(light != null ? light : Material.TORCH); //除了火把外的大部分光源都有碰撞箱,所以选火把 - registerTask(); return true; } catch (NoSuchMethodError | NoSuchMethodException ignored) { return false; diff --git a/src/main/java/me/xpyex/plugin/invactions/bukkit/module/QuickShulkerBox.java b/src/main/java/me/xpyex/plugin/invactions/bukkit/module/QuickShulkerBox.java index 13fd218..c773664 100644 --- a/src/main/java/me/xpyex/plugin/invactions/bukkit/module/QuickShulkerBox.java +++ b/src/main/java/me/xpyex/plugin/invactions/bukkit/module/QuickShulkerBox.java @@ -24,24 +24,27 @@ public class QuickShulkerBox extends RootModule { @EventHandler(ignoreCancelled = true) public void onInvClick(InventoryClickEvent event) { if (!(event.getWhoClicked() instanceof Player)) return; - if (serverEnabled()) { - if (playerEnabled((Player) event.getWhoClicked())) { - if (event.isShiftClick() && event.isRightClick()) { - if (event.getCursor() != null || event.getCursor().getType() != Material.AIR) - return; - if (event.getCurrentItem() == null) return; - if (event.getCurrentItem().getAmount() != 1) return; - ItemMeta meta = event.getCurrentItem().getItemMeta(); - if (meta instanceof BlockStateMeta) { //此处同时判断 != null - BlockState state = ((BlockStateMeta) meta).getBlockState(); - if (state instanceof ShulkerBox) { - event.setCancelled(true); + if (serverEnabled() && playerEnabled((Player) event.getWhoClicked())) { + if (event.isShiftClick() && event.isRightClick()) { + if (event.getCursor() != null || event.getCursor().getType() != Material.AIR) + return; + if (event.getCurrentItem() == null) return; + if (event.getCurrentItem().getAmount() != 1) return; + ItemMeta meta = event.getCurrentItem().getItemMeta(); + if (meta instanceof BlockStateMeta) { //此处同时判断 != null + BlockState state = ((BlockStateMeta) meta).getBlockState(); + if (state instanceof ShulkerBox) { + event.setCancelled(true); + Inventory boxInv = ((ShulkerBox) state).getInventory(); + try { + event.getWhoClicked().openInventory(boxInv); + } catch (Throwable e) { Inventory inventory = Bukkit.createInventory(event.getWhoClicked(), 27); - inventory.setContents(((ShulkerBox) state).getInventory().getContents()); + inventory.setContents(boxInv.getContents()); event.getWhoClicked().openInventory(inventory); - event.getWhoClicked().setMetadata(METADATA_KEY, new FixedMetadataValue(InvActions.getInstance(), event.getCurrentItem())); - ((Player) event.getWhoClicked()).playSound(event.getWhoClicked().getLocation(), Sound.BLOCK_SHULKER_BOX_OPEN, 1f, 1f); } + event.getWhoClicked().setMetadata(METADATA_KEY, new FixedMetadataValue(InvActions.getInstance(), event.getCurrentItem())); + ((Player) event.getWhoClicked()).playSound(event.getWhoClicked().getLocation(), Sound.BLOCK_SHULKER_BOX_OPEN, 1f, 1f); } } } @@ -50,23 +53,25 @@ public void onInvClick(InventoryClickEvent event) { @EventHandler public void onRightClick(PlayerInteractEvent event) { - if (!serverEnabled()) { - return; - } - if (!playerEnabled(event.getPlayer())) { + if (!serverEnabled() || !playerEnabled(event.getPlayer())) { return; } if (event.getPlayer().isSneaking() && event.getAction().toString().startsWith("RIGHT_")) { - if (event.getItem() != null && event.getItem().hasItemMeta()) { + if (event.getItem() != null) { if (event.getItem().getAmount() != 1) return; ItemMeta meta = event.getItem().getItemMeta(); - if (meta instanceof BlockStateMeta) { + if (meta instanceof BlockStateMeta) { //判断not null BlockState state = ((BlockStateMeta) meta).getBlockState(); if (state instanceof ShulkerBox) { event.setCancelled(true); - Inventory inventory = Bukkit.createInventory(event.getPlayer(), 27); - inventory.setContents(((ShulkerBox) state).getInventory().getContents()); - event.getPlayer().openInventory(inventory); + Inventory boxInv = ((ShulkerBox) state).getInventory(); + try { + event.getPlayer().openInventory(boxInv); + } catch (Throwable e) { + Inventory inventory = Bukkit.createInventory(event.getPlayer(), 27); + inventory.setContents(boxInv.getContents()); + event.getPlayer().openInventory(inventory); + } event.getPlayer().setMetadata(METADATA_KEY, new FixedMetadataValue(InvActions.getInstance(), event.getItem())); event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.BLOCK_SHULKER_BOX_OPEN, 1f, 1f); } diff --git a/src/main/java/me/xpyex/plugin/invactions/bukkit/module/RootModule.java b/src/main/java/me/xpyex/plugin/invactions/bukkit/module/RootModule.java index 7919bb2..3ffae2b 100644 --- a/src/main/java/me/xpyex/plugin/invactions/bukkit/module/RootModule.java +++ b/src/main/java/me/xpyex/plugin/invactions/bukkit/module/RootModule.java @@ -27,10 +27,13 @@ public class RootModule implements Listener { ValueUtil.ifPresent(LangUtil.getMessage(InvActions.getInstance(), "ActionBarSuffix"), s -> SETTING_HELP = s); } + public void registerCustomListener() {} + public RootModule() { if (canLoad()) { try { InvActions.getInstance().registerListener(this); + registerCustomListener(); } catch (Throwable e) { InvActions.getInstance().getLogger().severe("无法为模块 " + getName() + " 注册监听器: " + e); e.printStackTrace();