From df6e916d4a62049c4e156e49e65685ca2732c463 Mon Sep 17 00:00:00 2001 From: Indyuce Date: Thu, 18 Aug 2022 11:05:06 +0200 Subject: [PATCH] Better definitions for MMOItem class --- .../mmoitems/api/item/mmoitem/MMOItem.java | 2 + .../api/item/mmoitem/ReadMMOItem.java | 1 + .../api/item/mmoitem/VolatileMMOItem.java | 103 ++++++++---------- .../listener/reforging/RFGKeepDurability.java | 22 +++- 4 files changed, 68 insertions(+), 60 deletions(-) diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/mmoitem/MMOItem.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/mmoitem/MMOItem.java index 15464388..3a5a1f8c 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/mmoitem/MMOItem.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/mmoitem/MMOItem.java @@ -225,6 +225,7 @@ public class MMOItem implements ItemReference { * * @return The damage suffered by this item */ + @Deprecated public int getDamage() { // Does it use MMO Durability? @@ -254,6 +255,7 @@ public class MMOItem implements ItemReference { * * @param damage The damage suffered by this item */ + @Deprecated public void setDamage(int damage) { // Too powerful diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/mmoitem/ReadMMOItem.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/mmoitem/ReadMMOItem.java index a4918da9..41736fad 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/mmoitem/ReadMMOItem.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/mmoitem/ReadMMOItem.java @@ -25,6 +25,7 @@ public abstract class ReadMMOItem extends MMOItem { } @Override + @Deprecated public int getDamage() { // Does it use custom durability? diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/mmoitem/VolatileMMOItem.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/mmoitem/VolatileMMOItem.java index c943120c..b8f7087e 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/mmoitem/VolatileMMOItem.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/mmoitem/VolatileMMOItem.java @@ -1,12 +1,9 @@ package net.Indyuce.mmoitems.api.item.mmoitem; -import io.lumine.mythic.lib.api.item.ItemTag; import io.lumine.mythic.lib.api.item.NBTItem; -import io.lumine.mythic.lib.api.item.SupportedNBTTagValues; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder; import net.Indyuce.mmoitems.stat.type.ItemStat; -import net.Indyuce.mmoitems.stat.type.StatHistory; import org.bukkit.ChatColor; import org.jetbrains.annotations.NotNull; @@ -14,64 +11,56 @@ import java.util.logging.Level; public class VolatileMMOItem extends ReadMMOItem { - /** - * VolatileMMOItems only loads the item data it needs instantly. The item - * data is only read when using hasData(ItemStat) for the first time. - * LiveMMOItems read everything on the constructor. VolativeMMOItems are - * used in player inventory updates. - *

- * Basically, use this to quickly read Stat Data values from an ItemStack. - *

- * If you are editing the stats, and then building a new item stack, - * you must use {@link LiveMMOItem}. - * - * @param item - * The item to read from - */ - public VolatileMMOItem(NBTItem item) { - super(item); - } + /** + * VolatileMMOItems only loads the item data it needs instantly. The item + * data is only read when using hasData(ItemStat) for the first time. + * LiveMMOItems read everything on the constructor. VolativeMMOItems are + * used in player inventory updates. + *

+ * Basically, use this to quickly read Stat Data values from an ItemStack. + * Since 6.7.5 you can no longer build a volatile MMOItem. It also does NOT load + * stat histories anymore. + *

+ * If you are editing the stats, and then building a new item stack, + * you must use {@link LiveMMOItem}. + * + * @param item The item to read from + */ + public VolatileMMOItem(NBTItem item) { + super(item); + } - /** - * This should only be used once if we want the best performance. This - * method both checks for stat data, and loads it if it did found one - * - * @return If the item has some stat data - */ - @Override - public boolean hasData(@NotNull ItemStat stat) { - if (!super.hasData(stat)) + /** + * This should only be used once if we want the best performance. This + * method both checks for stat data, and loads it if it did found one + * + * @return If the item has some stat data + */ + @Override + public boolean hasData(@NotNull ItemStat stat) { + if (!super.hasData(stat)) - // Attempt to lad this stat data - try { - stat.whenLoaded(this); + // Attempt to load this stat data + try { + stat.whenLoaded(this); - // History not prematurely loaded? - if (getStatHistory(stat) == null) { + // Nope + } catch (RuntimeException exception) { - // Also load history :think ing: - ItemTag hisTag = ItemTag.getTagAtPath(ItemStackBuilder.history_keyword + stat.getId(), getNBT(), SupportedNBTTagValues.STRING); + // Log a warning + MMOItems.plugin.getLogger().log(Level.WARNING, + ChatColor.GRAY + "Could not load stat '" + + ChatColor.GOLD + stat.getId() + ChatColor.GRAY + "'item data from '" + + ChatColor.RED + getId() + ChatColor.GRAY + "': " + + ChatColor.YELLOW + exception.getMessage()); + } - if (hisTag != null) { - // Aye - StatHistory hist = StatHistory.fromNBTString(this, (String) hisTag.getValue()); + return super.hasData(stat); + } - // History valid? Record - if (hist != null) { this.setStatHistory(stat, hist); } - } - } - - // Nope - } catch (IllegalArgumentException exception) { - - // Log a warning - MMOItems.plugin.getLogger().log(Level.WARNING, - ChatColor.GRAY + "Could not load stat '" - + ChatColor.GOLD + stat.getId() + ChatColor.GRAY + "'item data from '" - + ChatColor.RED + getId() + ChatColor.GRAY + "': " - + ChatColor.YELLOW + exception.getMessage()); - } - - return super.hasData(stat); - } + @NotNull + @Override + public ItemStackBuilder newBuilder() { + throw new UnsupportedOperationException("Cannot build a VolatileMMOItem"); + } } diff --git a/MMOItems-Dist/src/main/java/net/Indyuce/mmoitems/listener/reforging/RFGKeepDurability.java b/MMOItems-Dist/src/main/java/net/Indyuce/mmoitems/listener/reforging/RFGKeepDurability.java index 2b42b26a..11283970 100644 --- a/MMOItems-Dist/src/main/java/net/Indyuce/mmoitems/listener/reforging/RFGKeepDurability.java +++ b/MMOItems-Dist/src/main/java/net/Indyuce/mmoitems/listener/reforging/RFGKeepDurability.java @@ -1,9 +1,13 @@ package net.Indyuce.mmoitems.listener.reforging; -import net.Indyuce.mmoitems.MMOItems; +import net.Indyuce.mmoitems.ItemStats; import net.Indyuce.mmoitems.api.event.MMOItemReforgeEvent; +import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem; +import net.Indyuce.mmoitems.stat.data.BooleanData; +import net.Indyuce.mmoitems.stat.data.type.StatData; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.jetbrains.annotations.Nullable; /** * Restores the old damage taken by the item, apparently @@ -15,8 +19,20 @@ public class RFGKeepDurability implements Listener { @EventHandler public void onReforge(MMOItemReforgeEvent event) { + final MMOItem newItem = event.getNewMMOItem(), oldItem = event.getOldMMOItem(); - // What was its durability? Transfer it - event.getNewMMOItem().setDamage(event.getOldMMOItem().getDamage()); + // Unbreakable item + if (event.getNewMMOItem().hasData(ItemStats.UNBREAKABLE) && ((BooleanData) event.getNewMMOItem().getData(ItemStats.UNBREAKABLE)).isEnabled()) + return; + + // Custom durability + final @Nullable StatData customDurabilityData = oldItem.getData(ItemStats.CUSTOM_DURABILITY); + if (customDurabilityData != null) + newItem.setData(ItemStats.CUSTOM_DURABILITY, customDurabilityData); + + // Vanilla durability + final @Nullable StatData vanillaDurabilityData = oldItem.getData(ItemStats.ITEM_DAMAGE); + if (vanillaDurabilityData != null) + newItem.setData(ItemStats.ITEM_DAMAGE, vanillaDurabilityData); } }