Better definitions for MMOItem class

This commit is contained in:
Indyuce 2022-08-18 11:05:06 +02:00
parent 6d94bee39b
commit df6e916d4a
4 changed files with 68 additions and 60 deletions

View File

@ -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

View File

@ -25,6 +25,7 @@ public abstract class ReadMMOItem extends MMOItem {
}
@Override
@Deprecated
public int getDamage() {
// Does it use custom durability?

View File

@ -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.
* <p></p>
* Basically, use this to <b>quickly read Stat Data values</b> from an ItemStack.
* <p></p>
* 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.
* <p>
* Basically, use this to <b>quickly read Stat Data values</b> from an ItemStack.
* Since 6.7.5 you can no longer build a volatile MMOItem. It also does NOT load
* stat histories anymore.
* <p>
* 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");
}
}

View File

@ -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);
}
}