diff --git a/pom.xml b/pom.xml index c4c20a6c..e304813c 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 net.Indyuce MMOItems - 6.7.1 + 6.7.2 MMOItems A great item solution for your RPG server!! diff --git a/src/main/java/net/Indyuce/mmoitems/api/interaction/util/DurabilityItem.java b/src/main/java/net/Indyuce/mmoitems/api/interaction/util/DurabilityItem.java index 714e821d..f70bdb35 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/interaction/util/DurabilityItem.java +++ b/src/main/java/net/Indyuce/mmoitems/api/interaction/util/DurabilityItem.java @@ -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 false if it is not a valid item + * This will return false 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. + * null 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 diff --git a/src/main/java/net/Indyuce/mmoitems/api/interaction/util/UntargetedDurabilityItem.java b/src/main/java/net/Indyuce/mmoitems/api/interaction/util/UntargetedDurabilityItem.java index a6a26a8e..94f38179 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/interaction/util/UntargetedDurabilityItem.java +++ b/src/main/java/net/Indyuce/mmoitems/api/interaction/util/UntargetedDurabilityItem.java @@ -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 diff --git a/src/main/java/net/Indyuce/mmoitems/listener/DurabilityListener.java b/src/main/java/net/Indyuce/mmoitems/listener/DurabilityListener.java index b1ab9499..9c106405 100644 --- a/src/main/java/net/Indyuce/mmoitems/listener/DurabilityListener.java +++ b/src/main/java/net/Indyuce/mmoitems/listener/DurabilityListener.java @@ -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()); } }