From 9b40f9e4b7959f4fbc76b03b6ac3217714dc969a Mon Sep 17 00:00:00 2001
From: Indyuce <cym.peyrat@yahoo.fr>
Date: Thu, 7 Jul 2022 15:29:35 +0200
Subject: [PATCH] Cooldown references for rpg pots

---
 .../java/net/Indyuce/mmoitems/ItemStats.java  |  1 +
 .../mmoitems/api/player/PlayerData.java       | 12 ++++++++++
 .../Indyuce/mmoitems/listener/ItemUse.java    | 22 ++++++++++++-------
 3 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/src/main/java/net/Indyuce/mmoitems/ItemStats.java b/src/main/java/net/Indyuce/mmoitems/ItemStats.java
index c75c8a02..826e30ab 100644
--- a/src/main/java/net/Indyuce/mmoitems/ItemStats.java
+++ b/src/main/java/net/Indyuce/mmoitems/ItemStats.java
@@ -131,6 +131,7 @@ public class ItemStats {
             SOULBOUND_LEVEL = new SoulboundLevel(),
           //  AUTO_SOULBIND = new BooleanStat("AUTO_SOULBIND", VersionMaterial.ENDER_EYE.toMaterial(), "Auto-Soulbind", new String[]{"Automatically soulbinds this item to", "a player when he acquires it."}, new String[]{"!consumable", "all"}),
             ITEM_COOLDOWN = new DoubleStat("ITEM_COOLDOWN", Material.COOKED_CHICKEN, "Item Cooldown", new String[]{"This cooldown applies for consumables", "as well as for item commands."}, new String[]{"!armor", "!gem_stone", "!block", "all"}),
+            COOLDOWN_REFERENCE = new StringStat("COOLDOWN_REFERENCE", Material.CHICKEN, "Cooldown Reference", new String[]{"Two items with the same cooldown reference", "will share their cooldowns. This is useful", "for health or mana pots for example."}, new String[]{"!armor", "!gem_stone", "!block", "all"}),
             VANILLA_EATING_ANIMATION = new VanillaEatingAnimation(),
             GEM_COLOR = new GemColor(),
             GEM_UPGRADE_SCALING = new GemUpgradeScaling(),
diff --git a/src/main/java/net/Indyuce/mmoitems/api/player/PlayerData.java b/src/main/java/net/Indyuce/mmoitems/api/player/PlayerData.java
index cdf93e33..b61f115d 100644
--- a/src/main/java/net/Indyuce/mmoitems/api/player/PlayerData.java
+++ b/src/main/java/net/Indyuce/mmoitems/api/player/PlayerData.java
@@ -366,14 +366,26 @@ public class PlayerData {
         mmoData.getCooldownMap().applyCooldown(type.name(), value);
     }
 
+    /**
+     * @deprecated Deprecated due to cooldown references
+     */
+    @Deprecated
     public boolean isOnCooldown(ItemReference ref) {
         return mmoData.getCooldownMap().isOnCooldown(ref);
     }
 
+    /**
+     * @deprecated Deprecated due to cooldown references
+     */
+    @Deprecated
     public void applyItemCooldown(ItemReference ref, double value) {
         mmoData.getCooldownMap().applyCooldown(ref, value);
     }
 
+    /**
+     * @deprecated Deprecated due to cooldown references
+     */
+    @Deprecated
     public double getItemCooldown(ItemReference ref) {
         return mmoData.getCooldownMap().getInfo(ref).getRemaining() / 1000d;
     }
diff --git a/src/main/java/net/Indyuce/mmoitems/listener/ItemUse.java b/src/main/java/net/Indyuce/mmoitems/listener/ItemUse.java
index 0a6ed366..f83a5c86 100644
--- a/src/main/java/net/Indyuce/mmoitems/listener/ItemUse.java
+++ b/src/main/java/net/Indyuce/mmoitems/listener/ItemUse.java
@@ -8,7 +8,6 @@ import io.lumine.mythic.lib.api.player.EquipmentSlot;
 import io.lumine.mythic.lib.comp.target.InteractionType;
 import io.lumine.mythic.lib.damage.MeleeAttackMetadata;
 import net.Indyuce.mmoitems.MMOItems;
-import net.Indyuce.mmoitems.MMOUtils;
 import net.Indyuce.mmoitems.api.ItemAttackMetadata;
 import net.Indyuce.mmoitems.api.Type;
 import net.Indyuce.mmoitems.api.TypeSet;
@@ -18,7 +17,7 @@ import net.Indyuce.mmoitems.api.interaction.weapon.Gauntlet;
 import net.Indyuce.mmoitems.api.interaction.weapon.Weapon;
 import net.Indyuce.mmoitems.api.interaction.weapon.untargeted.Staff;
 import net.Indyuce.mmoitems.api.interaction.weapon.untargeted.UntargetedWeapon;
-import net.Indyuce.mmoitems.api.interaction.weapon.untargeted.UntargetedWeaponType;
+import net.Indyuce.mmoitems.api.item.mmoitem.VolatileMMOItem;
 import net.Indyuce.mmoitems.api.player.PlayerData;
 import net.Indyuce.mmoitems.api.util.message.Message;
 import org.bukkit.Bukkit;
@@ -74,9 +73,10 @@ public class ItemUse implements Listener {
 
         // Commands & consummables
         if (event.getAction().name().contains("RIGHT_CLICK")) {
-            if (useItem.getPlayerData().isOnCooldown(useItem.getMMOItem())) {
+            final String cooldownReference = getCooldownReference(useItem.getMMOItem());
+            if (useItem.getPlayerData().getMMOPlayerData().getCooldownMap().isOnCooldown(cooldownReference)) {
                 Message.ITEM_ON_COOLDOWN
-                        .format(ChatColor.RED, "#left#", DIGIT.format(useItem.getPlayerData().getItemCooldown(useItem.getMMOItem())))
+                        .format(ChatColor.RED, "#left#", DIGIT.format(useItem.getPlayerData().getMMOPlayerData().getCooldownMap().getCooldown(cooldownReference)))
                         .send(player);
                 event.setUseItemInHand(Event.Result.DENY);
                 return;
@@ -92,7 +92,7 @@ public class ItemUse implements Listener {
                     event.getItem().setAmount(event.getItem().getAmount() - 1);
             }
 
-            useItem.getPlayerData().applyItemCooldown(useItem.getMMOItem(), useItem.getNBTItem().getStat("ITEM_COOLDOWN"));
+            useItem.getPlayerData().getMMOPlayerData().getCooldownMap().applyCooldown(cooldownReference, useItem.getNBTItem().getStat("ITEM_COOLDOWN"));
             useItem.executeCommands();
         }
 
@@ -104,6 +104,11 @@ public class ItemUse implements Listener {
         }
     }
 
+    private String getCooldownReference(VolatileMMOItem mmoitem) {
+        final String ref = mmoitem.getNBT().getString("MMOITEMS_COOLDOWN_REFERENCE");
+        return ref != null && !ref.isEmpty() ? ref : mmoitem.getCooldownPath();
+    }
+
     @EventHandler(ignoreCancelled = true)
     public void meleeAttacks(PlayerAttackEvent event) {
 
@@ -322,9 +327,10 @@ public class ItemUse implements Listener {
 
         if (useItem instanceof Consumable) {
 
-            if (useItem.getPlayerData().isOnCooldown(useItem.getMMOItem())) {
+            final String cooldownReference = getCooldownReference(useItem.getMMOItem());
+            if (useItem.getPlayerData().getMMOPlayerData().getCooldownMap().isOnCooldown(cooldownReference)) {
                 Message.ITEM_ON_COOLDOWN
-                        .format(ChatColor.RED, "#left#", DIGIT.format(useItem.getPlayerData().getItemCooldown(useItem.getMMOItem())))
+                        .format(ChatColor.RED, "#left#", DIGIT.format(useItem.getPlayerData().getMMOPlayerData().getCooldownMap().getCooldown(cooldownReference)))
                         .send(player);
                 event.setCancelled(true);
                 return;
@@ -342,7 +348,7 @@ public class ItemUse implements Listener {
             if (result == Consumable.ConsumableConsumeResult.NOT_CONSUME)
                 event.setCancelled(true);
 
-            useItem.getPlayerData().applyItemCooldown(useItem.getMMOItem(), useItem.getNBTItem().getStat("ITEM_COOLDOWN"));
+            useItem.getPlayerData().getMMOPlayerData().getCooldownMap().applyCooldown(cooldownReference, useItem.getNBTItem().getStat("ITEM_COOLDOWN"));
             useItem.executeCommands();
         }
     }