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 fccfd35f..ad1a2d55 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 @@ -406,57 +406,74 @@ public class BukkitActionManager implements ActionManager { private void registerItemAction() { registerAction((args, chance) -> { - if (args instanceof Section section) { - boolean mainOrOff = section.getString("hand", "main").equalsIgnoreCase("main"); - int amount = section.getInt("amount", 1); - return context -> { - if (Math.random() > chance) return; - Player player = context.holder(); - 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)); - }; + Boolean mainOrOff; + int amount; + if (args instanceof Integer integer) { + mainOrOff = null; + amount = integer; + } else if (args instanceof Section section) { + String hand = section.getString("hand"); + mainOrOff = hand == null ? null : hand.equalsIgnoreCase("main"); + amount = section.getInt("amount", 1); } else { plugin.getPluginLogger().warn("Invalid value type: " + args.getClass().getSimpleName() + " found at item-amount action which is expected to be `Section`"); return Action.empty(); } + return context -> { + if (context.holder() == null) return; + if (Math.random() > chance) return; + Player player = context.holder(); + EquipmentSlot hand = context.arg(ContextKeys.SLOT); + if (mainOrOff == null && hand == null) { + return; + } + boolean tempHand = Objects.requireNonNullElseGet(mainOrOff, () -> hand == EquipmentSlot.HAND); + ItemStack itemStack = tempHand ? player.getInventory().getItemInMainHand() : player.getInventory().getItemInOffHand(); + if (amount < 0) { + itemStack.setAmount(Math.max(0, itemStack.getAmount() + amount)); + } else if (amount > 0) { + PlayerUtils.giveItem(player, itemStack, amount); + } + }; }, "item-amount"); registerAction((args, chance) -> { - if (args instanceof Section section) { - EquipmentSlot slot = Optional.ofNullable(section.getString("slot")) + int amount; + EquipmentSlot slot; + if (args instanceof Integer integer) { + slot = null; + amount = integer; + } else if (args instanceof Section section) { + 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.holder(); - 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) - return; - if (amount > 0) { - plugin.getItemManager().decreaseDamage(context.holder(), itemStack, amount); - } else { - plugin.getItemManager().increaseDamage(context.holder(), itemStack, -amount, true); - } - }; + amount = section.getInt("amount", 1); } else { plugin.getPluginLogger().warn("Invalid value type: " + args.getClass().getSimpleName() + " found at durability action which is expected to be `Section`"); return Action.empty(); } + return context -> { + if (Math.random() > chance) return; + Player player = context.holder(); + if (player == null) return; + EquipmentSlot tempSlot = slot; + EquipmentSlot equipmentSlot = context.arg(ContextKeys.SLOT); + if (tempSlot == null && 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) + return; + if (amount > 0) { + plugin.getItemManager().decreaseDamage(context.holder(), itemStack, amount); + } else { + plugin.getItemManager().increaseDamage(context.holder(), itemStack, -amount, true); + } + }; }, "durability"); registerAction((args, chance) -> { if (args instanceof Section section) {