Merge branch '725-repair-percent-is-not-work' into 'master'

Vanilla & MMOItems items durability % fixed

See merge request phoenix-dvpmt/mmoitems!19
This commit is contained in:
Jules 2022-11-03 16:59:26 +00:00
commit 34f102fd3c
3 changed files with 41 additions and 28 deletions

View File

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

View File

@ -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);
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,27 +24,40 @@ public class RepairUtils {
throw new IllegalStateException("Utility class");
}
public static boolean repairPower(@NotNull PlayerData playerData, @NotNull NBTItem target, @NotNull Consumable consumable, double repairPower) {
/**
* 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();
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);
final boolean percentage = repairPercentage > 0;
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;
ItemMeta meta = target.getItem().getItemMeta();
((Damageable) meta).setDamage(Math.max(0, ((Damageable) meta).getDamage() - called.getRepaired()));
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(called.getRepaired() == -1 ? called.getRepairedPercent() : called.getRepaired()))
String.valueOf(repaired))
.send(player);
CustomSoundListener.playConsumableSound(consumable.getItem(), player);
return true;
}
return false;
}
}