From c0c8ea5c6189bd5c89480b15baeda82a722f64ce Mon Sep 17 00:00:00 2001 From: XiaoMoMi <972454774@qq.com> Date: Wed, 18 Oct 2023 16:42:44 +0800 Subject: [PATCH] durability changes --- .../api/manager/ItemManager.java | 2 +- .../compatibility/IntegrationManagerImpl.java | 2 +- .../mechanic/action/ActionManagerImpl.java | 2 +- .../mechanic/fishing/FishingManagerImpl.java | 6 ++-- .../mechanic/hook/HookManagerImpl.java | 27 ++++++++++++++++-- .../mechanic/item/ItemManagerImpl.java | 5 ++-- .../customfishing/util/ItemUtils.java | 28 +++++++++++-------- .../main/resources/contents/hook/default.yml | 8 ++++++ 8 files changed, 59 insertions(+), 21 deletions(-) diff --git a/api/src/main/java/net/momirealms/customfishing/api/manager/ItemManager.java b/api/src/main/java/net/momirealms/customfishing/api/manager/ItemManager.java index 1081e3a6..47e62c7b 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/manager/ItemManager.java +++ b/api/src/main/java/net/momirealms/customfishing/api/manager/ItemManager.java @@ -186,7 +186,7 @@ public interface ItemManager { * @param amount The amount by which to decrease the durability. * @param updateLore Whether to update the lore of the ItemStack. */ - void decreaseDurability(ItemStack itemStack, int amount, boolean updateLore); + void decreaseDurability(Player player, ItemStack itemStack, int amount, boolean updateLore); /** * Increases the durability of an ItemStack by a specified amount and optionally updates its lore. diff --git a/plugin/src/main/java/net/momirealms/customfishing/compatibility/IntegrationManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/compatibility/IntegrationManagerImpl.java index 18b8e6bb..cdfc1f78 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/compatibility/IntegrationManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/compatibility/IntegrationManagerImpl.java @@ -101,7 +101,7 @@ public class IntegrationManagerImpl implements IntegrationManager { } if (plugin.isHookedPluginEnabled("Jobs")) { registerLevelPlugin("JobsReborn", new JobsRebornImpl()); - hookMessage("Jobs"); + hookMessage("JobsReborn"); } if (plugin.isHookedPluginEnabled("MMOCore")) { registerLevelPlugin("MMOCore", new MMOCoreImpl()); diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/action/ActionManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/action/ActionManagerImpl.java index 16d370f6..db5eb97e 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/action/ActionManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/action/ActionManagerImpl.java @@ -609,7 +609,7 @@ public class ActionManagerImpl implements ActionManager { if (amount > 0) { ItemUtils.increaseDurability(itemStack, amount, true); } else { - ItemUtils.decreaseDurability(itemStack, -amount, true); + ItemUtils.decreaseDurability(condition.getPlayer(), itemStack, -amount, true); } }; } else { diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/fishing/FishingManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/fishing/FishingManagerImpl.java index 0617b719..357ec408 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/fishing/FishingManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/fishing/FishingManagerImpl.java @@ -256,7 +256,7 @@ public class FishingManagerImpl implements Listener, FishingManager { if (compound != null && compound.hasTag("max_dur")) { event.setCancelled(true); hook.remove(); - ItemUtils.decreaseDurability(itemStack, 2, true); + ItemUtils.decreaseDurability(player, itemStack, 2, true); } } } @@ -367,7 +367,7 @@ public class FishingManagerImpl implements Listener, FishingManager { if (nbtCompound != null && nbtCompound.hasTag("max_dur")) { event.getHook().remove(); event.setCancelled(true); - ItemUtils.decreaseDurability(itemStack, 5, true); + ItemUtils.decreaseDurability(player, itemStack, 5, true); } } } @@ -537,7 +537,7 @@ public class FishingManagerImpl implements Listener, FishingManager { break outer; } ItemUtils.decreaseHookDurability(rod, 1, false); - ItemUtils.decreaseDurability(rod, 1, true); + ItemUtils.decreaseDurability(player, rod, 1, true); } if (gamingPlayer.isSuccessful()) diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/hook/HookManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/hook/HookManagerImpl.java index e2a336de..af0afde5 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/hook/HookManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/hook/HookManagerImpl.java @@ -26,6 +26,7 @@ import net.momirealms.customfishing.api.mechanic.condition.Condition; import net.momirealms.customfishing.api.mechanic.effect.EffectCarrier; import net.momirealms.customfishing.api.mechanic.hook.HookSetting; import net.momirealms.customfishing.api.util.LogUtils; +import net.momirealms.customfishing.mechanic.item.ItemManagerImpl; import net.momirealms.customfishing.util.ItemUtils; import org.bukkit.Bukkit; import org.bukkit.GameMode; @@ -185,12 +186,16 @@ public class HookManagerImpl implements Listener, HookManager { if (setting == null) return false; + var curDurability = ItemUtils.getCustomDurability(hook); + if (curDurability.left() == 0) + return false; + NBTItem rodNBTItem = new NBTItem(rod); NBTCompound cfCompound = rodNBTItem.getOrCreateCompound("CustomFishing"); cfCompound.setString("hook_id", hookID); cfCompound.setItemStack("hook_item", hook); - cfCompound.setInteger("hook_dur", ItemUtils.getDurability(hook)); + cfCompound.setInteger("hook_dur", curDurability.right()); ItemUtils.updateNBTItemLore(rodNBTItem); rod.setItemMeta(rodNBTItem.getItem().getItemMeta()); @@ -273,6 +278,24 @@ public class HookManagerImpl implements Listener, HookManager { if (setting == null) return; + var cursorDurability = ItemUtils.getCustomDurability(cursor); + if (cursorDurability.left() == 0) { + if (plugin.getItemManager().getBuildableItem("hook", hookID) instanceof ItemManagerImpl.CFBuilder cfBuilder) { + ItemStack itemStack = cfBuilder.build(player, new HashMap<>()); + var pair = ItemUtils.getCustomDurability(itemStack); + cursorDurability = pair; + NBTItem nbtItem = new NBTItem(cursor); + NBTCompound compound = nbtItem.getOrCreateCompound("CustomFishing"); + compound.setInteger("max_dur", pair.left()); + compound.setInteger("cur_dur", pair.right()); + compound.setString("type", "hook"); + compound.setString("id", hookID); + cursor.setItemMeta(nbtItem.getItem().getItemMeta()); + } else { + return; + } + } + Condition condition = new Condition(player, new HashMap<>()); condition.insertArg("{rod}", plugin.getItemManager().getAnyPluginItemID(clicked)); EffectCarrier effectCarrier = plugin.getEffectManager().getEffectCarrier("hook", hookID); @@ -305,7 +328,7 @@ public class HookManagerImpl implements Listener, HookManager { cfCompound.setString("hook_id", hookID); cfCompound.setItemStack("hook_item", clonedHook); - cfCompound.setInteger("hook_dur", ItemUtils.getDurability(clonedHook)); + cfCompound.setInteger("hook_dur", cursorDurability.right()); ItemUtils.updateNBTItemLore(rodNBTItem); clicked.setItemMeta(rodNBTItem.getItem().getItemMeta()); diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java index 2b71318d..de75cee7 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java @@ -446,13 +446,14 @@ public class ItemManagerImpl implements ItemManager, Listener { /** * Decreases the durability of an ItemStack by a specified amount and optionally updates its lore. * + * @param player Player * @param itemStack The ItemStack to modify. * @param amount The amount by which to decrease the durability. * @param updateLore Whether to update the lore of the ItemStack. */ @Override - public void decreaseDurability(ItemStack itemStack, int amount, boolean updateLore) { - ItemUtils.decreaseDurability(itemStack, amount, updateLore); + public void decreaseDurability(Player player, ItemStack itemStack, int amount, boolean updateLore) { + ItemUtils.decreaseDurability(player, itemStack, amount, updateLore); } /** diff --git a/plugin/src/main/java/net/momirealms/customfishing/util/ItemUtils.java b/plugin/src/main/java/net/momirealms/customfishing/util/ItemUtils.java index b1603ef2..aae3c334 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/util/ItemUtils.java +++ b/plugin/src/main/java/net/momirealms/customfishing/util/ItemUtils.java @@ -25,12 +25,15 @@ import net.kyori.adventure.text.ScoreComponent; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.momirealms.customfishing.adventure.AdventureManagerImpl; import net.momirealms.customfishing.api.CustomFishingPlugin; +import net.momirealms.customfishing.api.common.Pair; import net.momirealms.customfishing.api.mechanic.hook.HookSetting; import net.momirealms.customfishing.api.util.LogUtils; import net.momirealms.customfishing.setting.CFConfig; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerItemDamageEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.meta.Damageable; @@ -52,9 +55,6 @@ public class ItemUtils { if (cfCompound == null) return nbtItem; - boolean hasLoreUpdate = cfCompound.hasTag("hook_id") || cfCompound.hasTag("max_dur"); - if (!hasLoreUpdate) return nbtItem; - NBTCompound displayCompound = nbtItem.getOrCreateCompound("display"); NBTList lore = displayCompound.getStringList("Lore"); lore.removeIf(it -> GsonComponentSerializer.gson().deserialize(it) instanceof ScoreComponent scoreComponent && scoreComponent.name().equals("cf")); @@ -202,7 +202,7 @@ public class ItemUtils { * @param amount The amount by which to reduce durability * @param updateLore Whether to update the lore after reducing durability */ - public static void decreaseDurability(ItemStack itemStack, int amount, boolean updateLore) { + public static void decreaseDurability(Player player, ItemStack itemStack, int amount, boolean updateLore) { if (itemStack == null || itemStack.getType() == Material.AIR) return; int unBreakingLevel = itemStack.getEnchantmentLevel(Enchantment.DURABILITY); @@ -227,6 +227,12 @@ public class ItemUtils { itemStack.setAmount(0); } } else { + ItemMeta previousMeta = itemStack.getItemMeta().clone(); + PlayerItemDamageEvent itemDamageEvent = new PlayerItemDamageEvent(player, itemStack, amount); + Bukkit.getPluginManager().callEvent(itemDamageEvent); + if (!itemStack.getItemMeta().equals(previousMeta)) { + return; + } int damage = nbtItem.getInteger("Damage") + amount; if (damage > itemStack.getType().getMaxDurability()) { itemStack.setAmount(0); @@ -304,17 +310,17 @@ public class ItemUtils { * @param itemStack The ItemStack to get durability from * @return The current durability value */ - public static int getDurability(ItemStack itemStack) { - if (!(itemStack.getItemMeta() instanceof Damageable damageable)) - return -1; - if (damageable.isUnbreakable()) - return -1; + public static Pair getCustomDurability(ItemStack itemStack) { + if (itemStack == null || itemStack.getType() == Material.AIR) + return Pair.of(0, 0); + if (itemStack.getItemMeta() instanceof Damageable damageable && damageable.isUnbreakable()) + return Pair.of(-1, -1); NBTItem nbtItem = new NBTItem(itemStack); NBTCompound cfCompound = nbtItem.getCompound("CustomFishing"); if (cfCompound != null && cfCompound.hasTag("max_dur")) { - return cfCompound.getInteger("cur_dur"); + return Pair.of(cfCompound.getInteger("max_dur"), cfCompound.getInteger("cur_dur")); } else { - return itemStack.getType().getMaxDurability() - damageable.getDamage(); + return Pair.of(0, 0); } } diff --git a/plugin/src/main/resources/contents/hook/default.yml b/plugin/src/main/resources/contents/hook/default.yml index 9c8f27e5..822c504d 100644 --- a/plugin/src/main/resources/contents/hook/default.yml +++ b/plugin/src/main/resources/contents/hook/default.yml @@ -1,3 +1,9 @@ +# Note: These are the default configurations of the plugin +# and do not necessarily mean that players can have a good +# gaming experience. We hope that you will create +# customized configurations based on your own ideas, +# allowing players to experience the uniqueness of your server. + delicate_hook: material: SHEARS display: @@ -13,6 +19,8 @@ delicate_hook: - '<#FFD700>Effects:' - ' - Increase the chance of getting high quality fish' max-durability: 16 + custom-model-data: 50000 + # {dur} / {max} lore-on-rod: - '' - '<#7FFFAA>Equipped hook:'