Fixed an issue with custom dur

This commit is contained in:
Jules 2022-03-01 13:10:41 +01:00
parent a8210b8e3a
commit dccfcbb2b8
4 changed files with 52 additions and 51 deletions

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.Indyuce</groupId>
<artifactId>MMOItems</artifactId>
<version>6.7.1</version>
<version>6.7.2</version>
<name>MMOItems</name>
<description>A great item solution for your RPG server!!</description>

View File

@ -13,6 +13,7 @@ import net.Indyuce.mmoitems.api.event.item.CustomDurabilityRepair;
import net.Indyuce.mmoitems.api.item.mmoitem.LiveMMOItem;
import net.Indyuce.mmoitems.api.item.util.LoreUpdate;
import net.Indyuce.mmoitems.api.player.PlayerData;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.data.UpgradeData;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
@ -22,6 +23,7 @@ import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
import java.util.Random;
@ -102,7 +104,7 @@ public class DurabilityItem {
/**
* @return If both this is a VALID custom durability item and if the item is broken.
* This will return <code>false</code> if it is not a valid item
* This will return <code>false</code> if it is not a valid item
*/
public boolean isBroken() {
return maxDurability > 0 && durability <= 0;
@ -118,7 +120,7 @@ public class DurabilityItem {
/**
* @return If the item actually supports custom durability. It is completely
* disabled when the player is in creative mode just like vanilla durability.
* disabled when the player is in creative mode just like vanilla durability.
*/
public boolean isValid() {
return maxDurability > 0 && player.getGameMode() != GameMode.CREATIVE;
@ -170,35 +172,46 @@ public class DurabilityItem {
* 2) item breaking
* 3) item downgrade
*
* @return Newest version of the damaged item
* @return Newest version of the damaged item.
* <code>null</code> if the item breaks. That method CANNOT
* return a null value if the item has no decreased its durability.
*/
@Nullable
public ItemStack toItem() {
if (isBroken()) {
// Lost when broken
if (isLostWhenBroken())
return null;
// Checks for possible downgrade
if (isDowngradedWhenBroken()) {
ItemTag uTag = ItemTag.getTagAtPath(ItemStats.UPGRADE.getNBTPath(), getNBTItem(), SupportedNBTTagValues.STRING);
if (uTag != null)
try {
UpgradeData data = new UpgradeData(new JsonParser().parse((String) uTag.getValue()).getAsJsonObject());
// If it cannot be downgraded (reached min), DEATH
if (data.getLevel() > data.getMin())
return null;
// Remove one level and FULLY repair item
LiveMMOItem mmo = new LiveMMOItem(getNBTItem());
mmo.setData(ItemStats.CUSTOM_DURABILITY, new DoubleData(maxDurability));
mmo.getUpgradeTemplate().upgradeTo(mmo, data.getLevel() - 1);
return mmo.newBuilder().buildNBT().toItem();
} catch (JsonSyntaxException | IllegalStateException ignored) {
// Nothing
}
}
}
// No modification needs to be done
if (durability == initialDurability)
return nbtItem.getItem();
// Checks for possible downgrade
ItemTag uTag = ItemTag.getTagAtPath(ItemStats.UPGRADE.getNBTPath(), getNBTItem(), SupportedNBTTagValues.STRING);
if (uTag != null)
try {
UpgradeData data = new UpgradeData(new JsonParser().parse((String) uTag.getValue()).getAsJsonObject());
// If it cannot be downgraded (reached min), DEATH
if (data.getLevel() <= data.getMin())
return null;
// Remove one level and FULLY repair item
LiveMMOItem mmo = new LiveMMOItem(getNBTItem());
mmo.getUpgradeTemplate().upgradeTo(mmo, data.getLevel() - 1);
NBTItem preRet = mmo.newBuilder().buildNBT();
preRet.addTag(new ItemTag("MMOITEMS_DURABILITY", maxDurability));
return preRet.toItem();
} catch (JsonSyntaxException | IllegalStateException ignored) {
// Nothing
}
/*
* Cross multiplication to display the current item durability on the
* item durability bar. (1 - ratio) because minecraft works with item

View File

@ -3,6 +3,7 @@ package net.Indyuce.mmoitems.api.interaction.util;
import io.lumine.mythic.lib.api.item.NBTItem;
import io.lumine.mythic.lib.api.player.EquipmentSlot;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class UntargetedDurabilityItem extends DurabilityItem {
private final EquipmentSlot slot;
@ -25,7 +26,8 @@ public class UntargetedDurabilityItem extends DurabilityItem {
public void inventoryUpdate() {
if (isBroken() && isLostWhenBroken()) {
ItemStack newVersion = toItem();
if (newVersion == null) {
if (slot == EquipmentSlot.OFF_HAND)
getPlayer().getInventory().setItemInOffHand(null);
else

View File

@ -75,21 +75,14 @@ public class DurabilityListener implements Listener {
* If the item is broken and if it is meant to be lost when broken,
* do NOT cancel the event and make sure the item is destroyed
*/
if (item.isBroken()) {
// Still here? Remove if lost when broken
if (item.isLostWhenBroken()) {
// Delete item
event.setDamage(999);
// Allow event to proceed
return;
}
ItemStack newVersion = item.toItem();
if (newVersion == null) {
event.setDamage(999);
return;
}
event.setCancelled(true);
event.getItem().setItemMeta(item.toItem().getItemMeta());
event.getItem().setItemMeta(newVersion.getItemMeta());
}
}
@ -112,21 +105,14 @@ public class DurabilityListener implements Listener {
if (item.isValid() && stack.getType().getMaxDurability() == 0) {
item.decreaseDurability(damage);
if (item.isBroken()) {
// Still here? Remove if lost when broken
if (item.isLostWhenBroken()) {
// Delete item
player.getInventory().setItem(slot, null);
player.getWorld().playSound(player.getLocation(), Sound.ENTITY_ITEM_BREAK, 1.0f, 1.0f);
// No more
return;
}
ItemStack newVersion = item.toItem();
if (newVersion == null) {
player.getInventory().setItem(slot, null);
player.getWorld().playSound(player.getLocation(), Sound.ENTITY_ITEM_BREAK, 1.0f, 1.0f);
return;
}
player.getInventory().getItem(slot).setItemMeta(item.toItem().getItemMeta());
player.getInventory().getItem(slot).setItemMeta(newVersion.getItemMeta());
}
}