From a76bc04386cdd54648f460fb5084ae8f614c96d9 Mon Sep 17 00:00:00 2001 From: Roch Date: Mon, 24 Oct 2022 16:02:50 +0200 Subject: [PATCH 1/2] Issue fixed --- .../net/Indyuce/mmoitems/stat/RepairPower.java | 2 +- .../Indyuce/mmoitems/stat/RepairPowerPercent.java | 2 +- .../net/Indyuce/mmoitems/util/RepairUtils.java | 14 +++++++++----- 3 files changed, 11 insertions(+), 7 deletions(-) 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 aa2994d5..062056dd 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 @@ -62,6 +62,6 @@ public class RepairPower extends DoubleStat implements ConsumableItemInteraction } // vanilla durability - return RepairUtils.repairPower(playerData, target, consumable, repairPower); + return RepairUtils.repairPower(playerData, target, consumable, -1, 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 fe8c0dfb..bc5c69d7 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 @@ -64,6 +64,6 @@ public class RepairPowerPercent extends DoubleStat implements ConsumableItemInte } // vanilla durability - return RepairUtils.repairPower(playerData, target, consumable, repairPower); + return RepairUtils.repairPower(playerData, target, consumable, repairPower, -1); } } 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 index e1648fff..99c8e7d1 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/util/RepairUtils.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/util/RepairUtils.java @@ -25,23 +25,27 @@ public class RepairUtils { throw new IllegalStateException("Utility class"); } - public static boolean repairPower(@NotNull PlayerData playerData, @NotNull NBTItem target, @NotNull Consumable consumable, double repairPower) { + public static boolean repairPower(@NotNull PlayerData playerData, @NotNull NBTItem target, @NotNull Consumable consumable, double repairPercentage, int repairUses) { final Player player = playerData.getPlayer(); - if (!target.getBoolean("Unbreakable") && target.getItem().hasItemMeta() && target.getItem().getItemMeta() instanceof Damageable + final boolean percentage = repairPercentage > 0; + 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); + RepairItemEvent called = percentage ? new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairPercentage) : new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairUses); Bukkit.getPluginManager().callEvent(called); if (called.isCancelled()) return false; + int uses = percentage ? (int) (target.getItem().getType().getMaxDurability() * (repairPercentage / 100)) : repairUses; ItemMeta meta = target.getItem().getItemMeta(); - ((Damageable) meta).setDamage(Math.max(0, ((Damageable) meta).getDamage() - called.getRepaired())); + ((Damageable) meta).setDamage(Math.max(0, ((Damageable) meta).getDamage() - uses)); 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())) + String.valueOf(uses)) .send(player); CustomSoundListener.playConsumableSound(consumable.getItem(), player); return true; From 47ff6fba4298ecb4c3056acbf5d3d090caf56345 Mon Sep 17 00:00:00 2001 From: Roch Date: Mon, 24 Oct 2022 16:49:32 +0200 Subject: [PATCH 2/2] MMOItems durability % fixed --- .../Indyuce/mmoitems/stat/RepairPower.java | 2 +- .../mmoitems/stat/RepairPowerPercent.java | 11 ++-- .../Indyuce/mmoitems/util/RepairUtils.java | 58 +++++++++++-------- 3 files changed, 40 insertions(+), 31 deletions(-) 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 062056dd..78fb04c7 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 @@ -62,6 +62,6 @@ public class RepairPower extends DoubleStat implements ConsumableItemInteraction } // vanilla durability - return RepairUtils.repairPower(playerData, target, consumable, -1, repairPower); + return RepairUtils.repairVanillaItem(playerData, target, consumable, -1, 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 bc5c69d7..d7cf0bb7 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 @@ -25,7 +25,7 @@ import org.jetbrains.annotations.Nullable; public class RepairPowerPercent extends DoubleStat implements ConsumableItemInteraction { public RepairPowerPercent() { super("REPAIR_PERCENT", Material.DAMAGED_ANVIL, "Repair Percentage", - new String[] { "The percentage of total durability to repair", "When dropped onto an item." }, + new String[]{"The percentage of total durability to repair", "When dropped onto an item."}, new String[]{"consumable"}); } @@ -39,7 +39,7 @@ public class RepairPowerPercent extends DoubleStat implements ConsumableItemInte Player player = playerData.getPlayer(); final String type = "MMOITEMS_REPAIR_TYPE"; - if((target.hasTag(type) || consumable.getNBTItem().hasTag(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); @@ -54,9 +54,10 @@ public class RepairPowerPercent extends DoubleStat implements ConsumableItemInte DurabilityItem durItem = new DurabilityItem(player, target); if (durItem.getDurability() < durItem.getMaxDurability()) { - target.getItem().setItemMeta(durItem.addDurability((int) (durItem.getMaxDurability()*called.getRepairedPercent())).toItem().getItemMeta()); + int repaired = (int) (durItem.getMaxDurability() * repairPower / 100); + target.getItem().setItemMeta(durItem.addDurability(repaired).toItem().getItemMeta()); Message.REPAIRED_ITEM - .format(ChatColor.YELLOW, "#item#", MMOUtils.getDisplayName(target.getItem()), "#amount#", "" + called.getRepairedPercent()) + .format(ChatColor.YELLOW, "#item#", MMOUtils.getDisplayName(target.getItem()), "#amount#", String.valueOf(repaired)) .send(player); CustomSoundListener.playConsumableSound(consumable.getItem(), player); } @@ -64,6 +65,6 @@ public class RepairPowerPercent extends DoubleStat implements ConsumableItemInte } // vanilla durability - return RepairUtils.repairPower(playerData, target, consumable, repairPower, -1); + return RepairUtils.repairVanillaItem(playerData, target, consumable, repairPower, -1); } } 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 index 99c8e7d1..e4c9c9c4 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/util/RepairUtils.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/util/RepairUtils.java @@ -10,7 +10,6 @@ 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; /** @@ -25,31 +24,40 @@ public class RepairUtils { throw new IllegalStateException("Utility class"); } - public static boolean repairPower(@NotNull PlayerData playerData, @NotNull NBTItem target, @NotNull Consumable consumable, double repairPercentage, int repairUses) { + + /** + * Repairs a vanilla item. + * It will not work with MMOItems items. + * + * @param playerData The player data. + * @param target The target item. + * @param consumable The consumable item. + * @param repairPercentage The repair percentage. (set to -1 to disable) + * @param repairUses The repair uses. (set to -1 to disable) + * @return True if the item was repaired. + */ + public static boolean repairVanillaItem(@NotNull PlayerData playerData, @NotNull NBTItem target, @NotNull Consumable consumable, double repairPercentage, int repairUses) { final Player player = playerData.getPlayer(); final boolean percentage = repairPercentage > 0; - if (!target.getBoolean("Unbreakable") - && target.getItem().hasItemMeta() - && target.getItem().getItemMeta() instanceof Damageable - && ((Damageable) target.getItem().getItemMeta()).getDamage() > 0) { - RepairItemEvent called = percentage ? new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairPercentage) : new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairUses); - Bukkit.getPluginManager().callEvent(called); - if (called.isCancelled()) - return false; - int uses = percentage ? (int) (target.getItem().getType().getMaxDurability() * (repairPercentage / 100)) : repairUses; - - ItemMeta meta = target.getItem().getItemMeta(); - ((Damageable) meta).setDamage(Math.max(0, ((Damageable) meta).getDamage() - uses)); - target.getItem().setItemMeta(meta); - Message.REPAIRED_ITEM.format(ChatColor.YELLOW, - "#item#", - MMOUtils.getDisplayName(target.getItem()), - "#amount#", - String.valueOf(uses)) - .send(player); - CustomSoundListener.playConsumableSound(consumable.getItem(), player); - return true; - } - return false; + if (target.getBoolean("Unbreakable") + || !target.getItem().hasItemMeta() + || !(target.getItem().getItemMeta() instanceof Damageable meta) + || ((Damageable) target.getItem().getItemMeta()).getDamage() <= 0) + return false; + RepairItemEvent called = percentage ? new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairPercentage) : new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairUses); + Bukkit.getPluginManager().callEvent(called); + if (called.isCancelled()) + return false; + int repaired = percentage ? (int) ((repairPercentage / 100) * target.getItem().getType().getMaxDurability()) : repairUses; + meta.setDamage(Math.max(0, meta.getDamage() - repaired)); + target.getItem().setItemMeta(meta); + Message.REPAIRED_ITEM.format(ChatColor.YELLOW, + "#item#", + MMOUtils.getDisplayName(target.getItem()), + "#amount#", + String.valueOf(repaired)) + .send(player); + CustomSoundListener.playConsumableSound(consumable.getItem(), player); + return true; } }