From 709c156ff67b4517e7e57fc1c48f74d80402859d Mon Sep 17 00:00:00 2001 From: XiaoMoMi <972454774@qq.com> Date: Mon, 5 Aug 2024 19:36:56 +0800 Subject: [PATCH] Improved check --- .../api/mechanic/context/ContextKeys.java | 2 ++ .../bukkit/action/BukkitActionManager.java | 22 +++++++++++++++---- .../bukkit/event/BukkitEventManager.java | 9 +++++--- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/context/ContextKeys.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/context/ContextKeys.java index 5003c102..031c7677 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/context/ContextKeys.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/context/ContextKeys.java @@ -20,6 +20,7 @@ package net.momirealms.customfishing.api.mechanic.context; import net.momirealms.customfishing.api.mechanic.competition.CompetitionGoal; import net.momirealms.customfishing.api.mechanic.loot.LootType; import org.bukkit.Location; +import org.bukkit.inventory.EquipmentSlot; import java.util.Objects; @@ -79,6 +80,7 @@ public class ContextKeys { public static final ContextKeys RECORD_FORMATTED = of("record_formatted", String.class); public static final ContextKeys CLICKS_LEFT = of("left_clicks", Integer.class); public static final ContextKeys REQUIRED_TIMES = of("clicks", Integer.class); + public static final ContextKeys SLOT = of("hand", EquipmentSlot.class); private final String key; private final Class type; diff --git a/core/src/main/java/net/momirealms/customfishing/bukkit/action/BukkitActionManager.java b/core/src/main/java/net/momirealms/customfishing/bukkit/action/BukkitActionManager.java index 64430e6a..07253745 100644 --- a/core/src/main/java/net/momirealms/customfishing/bukkit/action/BukkitActionManager.java +++ b/core/src/main/java/net/momirealms/customfishing/bukkit/action/BukkitActionManager.java @@ -46,7 +46,6 @@ import net.momirealms.sparrow.heart.feature.armorstand.FakeArmorStand; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.entity.Entity; import org.bukkit.entity.ExperienceOrb; import org.bukkit.entity.Player; import org.bukkit.inventory.EquipmentSlot; @@ -403,7 +402,12 @@ public class BukkitActionManager implements ActionManager { return context -> { if (Math.random() > chance) return; Player player = context.getHolder(); - ItemStack itemStack = mainOrOff ? player.getInventory().getItemInMainHand() : player.getInventory().getItemInOffHand(); + boolean tempHand = mainOrOff; + EquipmentSlot hand = context.arg(ContextKeys.SLOT); + if (hand == EquipmentSlot.OFF_HAND || hand == EquipmentSlot.HAND) { + tempHand = hand == EquipmentSlot.HAND; + } + ItemStack itemStack = tempHand ? player.getInventory().getItemInMainHand() : player.getInventory().getItemInOffHand(); itemStack.setAmount(Math.max(0, itemStack.getAmount() + amount)); }; } else { @@ -413,12 +417,22 @@ public class BukkitActionManager implements ActionManager { }); registerAction("durability", (args, chance) -> { if (args instanceof Section section) { - EquipmentSlot slot = EquipmentSlot.valueOf(section.getString("slot", "hand").toUpperCase(Locale.ENGLISH)); + EquipmentSlot slot = Optional.ofNullable(section.getString("slot")) + .map(hand -> EquipmentSlot.valueOf(hand.toUpperCase(Locale.ENGLISH))) + .orElse(null); int amount = section.getInt("amount", 1); return context -> { if (Math.random() > chance) return; Player player = context.getHolder(); - ItemStack itemStack = player.getInventory().getItem(slot); + EquipmentSlot tempSlot = slot; + EquipmentSlot equipmentSlot = context.arg(ContextKeys.SLOT); + if (equipmentSlot != null) { + tempSlot = equipmentSlot; + } + if (tempSlot == null) { + return; + } + ItemStack itemStack = player.getInventory().getItem(tempSlot); if (itemStack.getType() == Material.AIR || itemStack.getAmount() == 0) return; if (itemStack.getItemMeta() == null) diff --git a/core/src/main/java/net/momirealms/customfishing/bukkit/event/BukkitEventManager.java b/core/src/main/java/net/momirealms/customfishing/bukkit/event/BukkitEventManager.java index 9323a4cc..b17d183a 100644 --- a/core/src/main/java/net/momirealms/customfishing/bukkit/event/BukkitEventManager.java +++ b/core/src/main/java/net/momirealms/customfishing/bukkit/event/BukkitEventManager.java @@ -74,17 +74,20 @@ public class BukkitEventManager implements EventManager, Listener { @EventHandler public void onInteract(PlayerInteractEvent event) { - if (event.getHand() != EquipmentSlot.HAND) - return; if (event.getAction() != org.bukkit.event.block.Action.RIGHT_CLICK_AIR && event.getAction() != org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK) return; - ItemStack itemStack = event.getPlayer().getInventory().getItemInMainHand(); + EquipmentSlot slot = event.getHand(); + if (slot == null) { + return; + } + ItemStack itemStack = event.getPlayer().getInventory().getItem(slot); if (itemStack.getType() == Material.AIR || itemStack.getAmount() == 0) return; String id = this.plugin.getItemManager().getItemID(itemStack); Context context = Context.player(event.getPlayer()); Block clicked = event.getClickedBlock(); context.arg(ContextKeys.OTHER_LOCATION, clicked == null ? event.getPlayer().getLocation() : clicked.getLocation()); + context.arg(ContextKeys.SLOT, event.getHand()); List mechanics = MechanicType.getTypeByID(id); if (mechanics != null) { for (MechanicType type : mechanics) {