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 // vanilla durability
return RepairUtils.repairPower(playerData, target, consumable, 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); 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,27 +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 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())); * Repairs a vanilla item.
target.getItem().setItemMeta(meta); * It will not work with MMOItems items.
Message.REPAIRED_ITEM.format(ChatColor.YELLOW, *
"#item#", * @param playerData The player data.
MMOUtils.getDisplayName(target.getItem()), * @param target The target item.
"#amount#", * @param consumable The consumable item.
String.valueOf(called.getRepaired() == -1 ? called.getRepairedPercent() : called.getRepaired())) * @param repairPercentage The repair percentage. (set to -1 to disable)
.send(player); * @param repairUses The repair uses. (set to -1 to disable)
CustomSoundListener.playConsumableSound(consumable.getItem(), player); * @return True if the item was repaired.
return true; */
} public static boolean repairVanillaItem(@NotNull PlayerData playerData, @NotNull NBTItem target, @NotNull Consumable consumable, double repairPercentage, int repairUses) {
return false; final Player player = playerData.getPlayer();
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;
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;
} }
} }