MMOItems durability % fixed

This commit is contained in:
Roch 2022-10-24 16:49:32 +02:00
parent a76bc04386
commit 47ff6fba42
3 changed files with 40 additions and 31 deletions

View File

@ -62,6 +62,6 @@ public class RepairPower extends DoubleStat implements ConsumableItemInteraction
} }
// vanilla durability // vanilla durability
return RepairUtils.repairPower(playerData, target, consumable, -1, repairPower); return RepairUtils.repairVanillaItem(playerData, target, consumable, -1, repairPower);
} }
} }

View File

@ -25,7 +25,7 @@ import org.jetbrains.annotations.Nullable;
public class RepairPowerPercent extends DoubleStat implements ConsumableItemInteraction { public class RepairPowerPercent extends DoubleStat implements ConsumableItemInteraction {
public RepairPowerPercent() { public RepairPowerPercent() {
super("REPAIR_PERCENT", Material.DAMAGED_ANVIL, "Repair Percentage", 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"}); new String[]{"consumable"});
} }
@ -39,7 +39,7 @@ public class RepairPowerPercent extends DoubleStat implements ConsumableItemInte
Player player = playerData.getPlayer(); Player player = playerData.getPlayer();
final String type = "MMOITEMS_REPAIR_TYPE"; 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))) { !target.getString(type).equals(consumable.getNBTItem().getString(type))) {
Message.UNABLE_TO_REPAIR.format(ChatColor.RED, "#item#", MMOUtils.getDisplayName(target.getItem())).send(player); 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); 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); DurabilityItem durItem = new DurabilityItem(player, target);
if (durItem.getDurability() < durItem.getMaxDurability()) { 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 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); .send(player);
CustomSoundListener.playConsumableSound(consumable.getItem(), player); CustomSoundListener.playConsumableSound(consumable.getItem(), player);
} }
@ -64,6 +65,6 @@ public class RepairPowerPercent extends DoubleStat implements ConsumableItemInte
} }
// vanilla durability // vanilla durability
return RepairUtils.repairPower(playerData, target, consumable, repairPower, -1); return RepairUtils.repairVanillaItem(playerData, target, consumable, repairPower, -1);
} }
} }

View File

@ -10,7 +10,6 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@ -25,31 +24,40 @@ public class RepairUtils {
throw new IllegalStateException("Utility class"); 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 Player player = playerData.getPlayer();
final boolean percentage = repairPercentage > 0; final boolean percentage = repairPercentage > 0;
if (!target.getBoolean("Unbreakable") if (target.getBoolean("Unbreakable")
&& target.getItem().hasItemMeta() || !target.getItem().hasItemMeta()
&& target.getItem().getItemMeta() instanceof Damageable || !(target.getItem().getItemMeta() instanceof Damageable meta)
&& ((Damageable) target.getItem().getItemMeta()).getDamage() > 0) { || ((Damageable) target.getItem().getItemMeta()).getDamage() <= 0)
RepairItemEvent called = percentage ? new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairPercentage) : new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairUses); return false;
Bukkit.getPluginManager().callEvent(called); RepairItemEvent called = percentage ? new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairPercentage) : new RepairItemEvent(playerData, consumable.getMMOItem(), target, repairUses);
if (called.isCancelled()) Bukkit.getPluginManager().callEvent(called);
return false; if (called.isCancelled())
int uses = percentage ? (int) (target.getItem().getType().getMaxDurability() * (repairPercentage / 100)) : repairUses; return false;
int repaired = percentage ? (int) ((repairPercentage / 100) * target.getItem().getType().getMaxDurability()) : repairUses;
ItemMeta meta = target.getItem().getItemMeta(); meta.setDamage(Math.max(0, meta.getDamage() - repaired));
((Damageable) meta).setDamage(Math.max(0, ((Damageable) meta).getDamage() - uses)); target.getItem().setItemMeta(meta);
target.getItem().setItemMeta(meta); Message.REPAIRED_ITEM.format(ChatColor.YELLOW,
Message.REPAIRED_ITEM.format(ChatColor.YELLOW, "#item#",
"#item#", MMOUtils.getDisplayName(target.getItem()),
MMOUtils.getDisplayName(target.getItem()), "#amount#",
"#amount#", String.valueOf(repaired))
String.valueOf(uses)) .send(player);
.send(player); CustomSoundListener.playConsumableSound(consumable.getItem(), player);
CustomSoundListener.playConsumableSound(consumable.getItem(), player); return true;
return true;
}
return false;
} }
} }