From 04c2756a8c9887bdd6a8f41dd0bbc0a9d18cc955 Mon Sep 17 00:00:00 2001 From: Roch Date: Mon, 24 Oct 2022 12:45:55 +0200 Subject: [PATCH] Repair power placeholder fixed --- .../api/event/item/RepairItemEvent.java | 96 ++++++++------- .../Indyuce/mmoitems/stat/RepairPower.java | 109 ++++++++---------- .../mmoitems/stat/RepairPowerPercent.java | 22 +--- .../Indyuce/mmoitems/util/RepairUtils.java | 52 +++++++++ 4 files changed, 145 insertions(+), 134 deletions(-) create mode 100644 MMOItems-API/src/main/java/net/Indyuce/mmoitems/util/RepairUtils.java diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/event/item/RepairItemEvent.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/event/item/RepairItemEvent.java index 6ff40a96..3cc45af7 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/event/item/RepairItemEvent.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/event/item/RepairItemEvent.java @@ -8,67 +8,63 @@ import net.Indyuce.mmoitems.api.player.PlayerData; import io.lumine.mythic.lib.api.item.NBTItem; public class RepairItemEvent extends PlayerDataEvent { - private static final HandlerList handlers = new HandlerList(); + private static final HandlerList handlers = new HandlerList(); - private final VolatileMMOItem consumable; - private final NBTItem target; + private final VolatileMMOItem consumable; + private final NBTItem target; - private int repaired; - private double repairedPercent; + private int repaired = -1; + private double repairedPercent = -1; - /** - * Called when a player repairs an item using a consumable - * - * @param playerData - * Player repairing the item - * @param consumable - * Consumable used to repair the item - * @param target - * Item being repaired - * @param repaired - * Amount of durability being repaired - */ - public RepairItemEvent(PlayerData playerData, VolatileMMOItem consumable, NBTItem target, int repaired) { - super(playerData); + /** + * Called when a player repairs an item using a consumable + * + * @param playerData Player repairing the item + * @param consumable Consumable used to repair the item + * @param target Item being repaired + * @param repaired Amount of durability being repaired + */ + public RepairItemEvent(PlayerData playerData, VolatileMMOItem consumable, NBTItem target, int repaired) { + super(playerData); - this.consumable = consumable; - this.target = target; - this.repaired = repaired; - } + this.consumable = consumable; + this.target = target; + this.repaired = repaired; + } - public RepairItemEvent(PlayerData playerData, VolatileMMOItem consumable, NBTItem target, double repaired) { - super(playerData); + public RepairItemEvent(PlayerData playerData, VolatileMMOItem consumable, NBTItem target, double repaired) { + super(playerData); - this.consumable = consumable; - this.target = target; - this.repairedPercent = repaired; - } + this.consumable = consumable; + this.target = target; + this.repairedPercent = repaired; + } - public VolatileMMOItem getConsumable() { - return consumable; - } + public VolatileMMOItem getConsumable() { + return consumable; + } - public NBTItem getTargetItem() { - return target; - } + public NBTItem getTargetItem() { + return target; + } - public int getRepaired() { - return repaired; - } + public int getRepaired() { + return repaired; + } - public double getRepairedPercent(){ - return repairedPercent; - } + public double getRepairedPercent() { + return repairedPercent; + } - public void setRepaired(int repaired) { - this.repaired = repaired; - } + public void setRepaired(int repaired) { + this.repaired = repaired; + } - public HandlerList getHandlers() { - return handlers; - } + public HandlerList getHandlers() { + return handlers; + } - public static HandlerList getHandlerList() { - return handlers; - } + public static HandlerList getHandlerList() { + return handlers; + } } diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/RepairPower.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/RepairPower.java index 24b2e140..94b6c4b9 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/RepairPower.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/RepairPower.java @@ -1,15 +1,6 @@ package net.Indyuce.mmoitems.stat; -import net.Indyuce.mmoitems.listener.CustomSoundListener; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Material; -import org.bukkit.Sound; -import org.bukkit.entity.Player; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.inventory.meta.Damageable; -import org.bukkit.inventory.meta.ItemMeta; - +import io.lumine.mythic.lib.api.item.NBTItem; import net.Indyuce.mmoitems.ItemStats; import net.Indyuce.mmoitems.MMOUtils; import net.Indyuce.mmoitems.api.Type; @@ -18,69 +9,59 @@ import net.Indyuce.mmoitems.api.interaction.Consumable; import net.Indyuce.mmoitems.api.interaction.util.DurabilityItem; import net.Indyuce.mmoitems.api.player.PlayerData; import net.Indyuce.mmoitems.api.util.message.Message; +import net.Indyuce.mmoitems.listener.CustomSoundListener; import net.Indyuce.mmoitems.stat.type.ConsumableItemInteraction; import net.Indyuce.mmoitems.stat.type.DoubleStat; -import io.lumine.mythic.lib.api.item.NBTItem; +import net.Indyuce.mmoitems.util.RepairUtils; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryClickEvent; import org.jetbrains.annotations.NotNull; public class RepairPower extends DoubleStat implements ConsumableItemInteraction { - public RepairPower() { - super("REPAIR", Material.ANVIL, "Repair", new String[] { "The amount of durability your item", "can repair when set an item." }, - new String[] { "consumable" }); - } + public RepairPower() { + super("REPAIR", Material.ANVIL, "Repair", new String[]{"The amount of durability your item", "can repair when set an item."}, + new String[]{"consumable"}); + } - @Override - public boolean handleConsumableEffect(@NotNull InventoryClickEvent event, @NotNull PlayerData playerData, @NotNull Consumable consumable, @NotNull NBTItem target, Type targetType) { - int repairPower = (int) consumable.getNBTItem().getStat(ItemStats.REPAIR.getId()); - if (repairPower <= 0) - return false; + @Override + public boolean handleConsumableEffect(@NotNull InventoryClickEvent event, @NotNull PlayerData playerData, @NotNull Consumable consumable, @NotNull NBTItem target, Type targetType) { + int repairPower = (int) consumable.getNBTItem().getStat(ItemStats.REPAIR.getId()); + if (repairPower <= 0) + return false; - // custom durability - Player player = playerData.getPlayer(); + // custom durability + Player player = playerData.getPlayer(); - final String type = "MMOITEMS_REPAIR_TYPE"; - if((target.hasTag(type) || consumable.getNBTItem().hasTag(type)) && - !target.getString(type).equals(consumable.getNBTItem().getString(type))) { - Message.UNABLE_TO_REPAIR.format(ChatColor.RED, "#item#", MMOUtils.getDisplayName(target.getItem())).send(player); - player.getPlayer().playSound(player.getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f); - return false; - } + final String type = "MMOITEMS_REPAIR_TYPE"; + if ((target.hasTag(type) || consumable.getNBTItem().hasTag(type)) && + !target.getString(type).equals(consumable.getNBTItem().getString(type))) { + Message.UNABLE_TO_REPAIR.format(ChatColor.RED, "#item#", MMOUtils.getDisplayName(target.getItem())).send(player); + player.getPlayer().playSound(player.getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f); + return false; + } - if (target.hasTag("MMOITEMS_DURABILITY")) { - RepairItemEvent called = new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairPower); - Bukkit.getPluginManager().callEvent(called); - if (called.isCancelled()) - return false; + if (target.hasTag("MMOITEMS_DURABILITY")) { + RepairItemEvent called = new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairPower); + Bukkit.getPluginManager().callEvent(called); + if (called.isCancelled()) + return false; - DurabilityItem durItem = new DurabilityItem(player, target); - if (durItem.getDurability() < durItem.getMaxDurability()) { - target.getItem().setItemMeta(durItem.addDurability(called.getRepaired()).toItem().getItemMeta()); - Message.REPAIRED_ITEM - .format(ChatColor.YELLOW, "#item#", MMOUtils.getDisplayName(target.getItem()), "#amount#", String.valueOf(called.getRepaired())) - .send(player); - CustomSoundListener.playConsumableSound(consumable.getItem(), player); - } - return true; - } + DurabilityItem durItem = new DurabilityItem(player, target); + if (durItem.getDurability() < durItem.getMaxDurability()) { + target.getItem().setItemMeta(durItem.addDurability(called.getRepaired()).toItem().getItemMeta()); + Message.REPAIRED_ITEM + .format(ChatColor.YELLOW, "#item#", MMOUtils.getDisplayName(target.getItem()), "#amount#", String.valueOf(called.getRepaired())) + .send(player); + CustomSoundListener.playConsumableSound(consumable.getItem(), player); + } + return true; + } - // vanilla durability - if (!target.getBoolean("Unbreakable") && target.getItem().hasItemMeta() && target.getItem().getItemMeta() instanceof Damageable - && ((Damageable) target.getItem().getItemMeta()).getDamage() > 0) { - - RepairItemEvent called = new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairPower); - Bukkit.getPluginManager().callEvent(called); - if (called.isCancelled()) - return false; - - ItemMeta meta = target.getItem().getItemMeta(); - ((Damageable) meta).setDamage(Math.max(0, ((Damageable) meta).getDamage() - called.getRepaired())); - target.getItem().setItemMeta(meta); - Message.REPAIRED_ITEM.format(ChatColor.YELLOW, "#item#", MMOUtils.getDisplayName(target.getItem()), "#amount#", "" + called.getRepaired()) - .send(player); - CustomSoundListener.playConsumableSound(consumable.getItem(), player); - return true; - } - - return false; - } + // vanilla durability + return RepairUtils.repairPower(playerData, target, consumable, repairPower); + } } diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/RepairPowerPercent.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/RepairPowerPercent.java index 30bd264c..05cd1225 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/RepairPowerPercent.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/RepairPowerPercent.java @@ -12,14 +12,13 @@ import net.Indyuce.mmoitems.api.util.message.Message; import net.Indyuce.mmoitems.listener.CustomSoundListener; import net.Indyuce.mmoitems.stat.type.ConsumableItemInteraction; import net.Indyuce.mmoitems.stat.type.DoubleStat; +import net.Indyuce.mmoitems.util.RepairUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.inventory.meta.Damageable; -import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -65,23 +64,6 @@ public class RepairPowerPercent extends DoubleStat implements ConsumableItemInte } // vanilla durability - if (!target.getBoolean("Unbreakable") && target.getItem().hasItemMeta() && target.getItem().getItemMeta() instanceof Damageable - && ((Damageable) target.getItem().getItemMeta()).getDamage() > 0) { - - RepairItemEvent called = new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairPower); - Bukkit.getPluginManager().callEvent(called); - if (called.isCancelled()) - return false; - - ItemMeta meta = target.getItem().getItemMeta(); - ((Damageable) meta).setDamage(Math.max(0, ((Damageable) meta).getDamage() - called.getRepaired())); - target.getItem().setItemMeta(meta); - Message.REPAIRED_ITEM.format(ChatColor.YELLOW, "#item#", MMOUtils.getDisplayName(target.getItem()), "#amount#", "" + called.getRepaired()) - .send(player); - CustomSoundListener.playConsumableSound(consumable.getItem(), player); - return true; - } - - return false; + return RepairUtils.repairPower(playerData, target, consumable, repairPower); } } diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/util/RepairUtils.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/util/RepairUtils.java new file mode 100644 index 00000000..68f017c8 --- /dev/null +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/util/RepairUtils.java @@ -0,0 +1,52 @@ +package net.Indyuce.mmoitems.util; + +import io.lumine.mythic.lib.api.item.NBTItem; +import net.Indyuce.mmoitems.MMOUtils; +import net.Indyuce.mmoitems.api.event.item.RepairItemEvent; +import net.Indyuce.mmoitems.api.interaction.Consumable; +import net.Indyuce.mmoitems.api.player.PlayerData; +import net.Indyuce.mmoitems.api.util.message.Message; +import net.Indyuce.mmoitems.listener.CustomSoundListener; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.bukkit.inventory.meta.Damageable; +import org.bukkit.inventory.meta.ItemMeta; +import org.jetbrains.annotations.NotNull; + +/** + * mmoitems + * + * @author Roch Blondiaux + * @date 24/10/2022 + */ +public class RepairUtils { + + public RepairUtils() { + throw new IllegalStateException("Utility class"); + } + + public static boolean repairPower(@NotNull PlayerData playerData, @NotNull NBTItem target, @NotNull Consumable consumable, double repairPower) { + final Player player = playerData.getPlayer(); + if (!target.getBoolean("Unbreakable") && target.getItem().hasItemMeta() && target.getItem().getItemMeta() instanceof Damageable + && ((Damageable) target.getItem().getItemMeta()).getDamage() > 0) { + RepairItemEvent called = new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairPower); + Bukkit.getPluginManager().callEvent(called); + if (called.isCancelled()) + return false; + + ItemMeta meta = target.getItem().getItemMeta(); + ((Damageable) meta).setDamage(Math.max(0, ((Damageable) meta).getDamage() - called.getRepaired())); + target.getItem().setItemMeta(meta); + Message.REPAIRED_ITEM.format(ChatColor.YELLOW, + "#item#", + MMOUtils.getDisplayName(target.getItem()), + "#amount#", + String.valueOf(called.getRepaired() == -1 ? called.getRepairedPercent() : called.getRepaired())) + .send(player); + CustomSoundListener.playConsumableSound(consumable.getItem(), player); + return true; + } + return false; + } +}