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
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 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);
}
}

View File

@ -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;
}
}