Added 'Max Vanilla Durability'

This commit is contained in:
Jules 2024-06-20 22:56:11 -07:00
parent 6e0491d978
commit 90520347fb
5 changed files with 99 additions and 5 deletions

View File

@ -21,6 +21,7 @@ public class ItemStats {
ITEM_DAMAGE = new ItemDamage(),
CUSTOM_MODEL_DATA = new CustomModelData(),
MAX_DURABILITY = new MaximumDurability(),
MAX_ITEM_DAMAGE = new MaxItemDamage(),
WILL_BREAK = new LostWhenBroken(),
NAME = new DisplayName(),
LORE = new Lore(),

View File

@ -6,8 +6,6 @@ import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.item.mmoitem.ReadMMOItem;
import net.Indyuce.mmoitems.api.util.NumericStatFormula;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.data.random.RandomStatData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.type.DoubleStat;
import net.Indyuce.mmoitems.stat.type.GemStoneStat;
import org.bukkit.Material;
@ -28,7 +26,7 @@ import java.util.ArrayList;
*/
public class ItemDamage extends DoubleStat implements GemStoneStat {
public ItemDamage() {
super("ITEM_DAMAGE", Material.FISHING_ROD, "Item Damage",
super("ITEM_DAMAGE", Material.FISHING_ROD, "Base Item Damage",
new String[]{"Default item damage. This does &cNOT", "impact the item's max durability."}, new String[]{"!block", "all"});
}

View File

@ -0,0 +1,95 @@
package net.Indyuce.mmoitems.stat;
import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.api.item.SupportedNBTTagValues;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.item.mmoitem.ReadMMOItem;
import net.Indyuce.mmoitems.api.util.NumericStatFormula;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.type.DoubleStat;
import net.Indyuce.mmoitems.stat.type.GemStoneStat;
import net.Indyuce.mmoitems.util.VersionDependant;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.inventory.meta.Damageable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
@VersionDependant(version = {1, 20, 5})
public class MaxItemDamage extends DoubleStat implements GemStoneStat {
public MaxItemDamage() {
super("MAX_ITEM_DAMAGE", Material.DAMAGED_ANVIL, "Maximum Vanilla Durability", new String[]{"Only available in 1.20.5+", "Maximum amount of durabiliy on your item.", "This works using vanilla durability and is", "much more stable than Custom Durability."}, new String[]{"all"});
}
@Override
public void whenApplied(@NotNull ItemStackBuilder item, @NotNull DoubleData data) {
Validate.isTrue(item.getMeta() instanceof Damageable, "Item is not damageable");
final int value = (int) data.getValue();
Validate.isTrue(value > 0, "Maximum durability must be positive");
// Edit meta
((Damageable) item.getMeta()).setMaxDamage((int) data.getValue());
// Save in NBT
item.addItemTag(getAppliedNBT(data));
}
@Override
public void whenPreviewed(@NotNull ItemStackBuilder item, @NotNull DoubleData
currentData, @NotNull NumericStatFormula templateData) throws IllegalArgumentException {
whenApplied(item, currentData);
}
@NotNull
@Override
public ArrayList<ItemTag> getAppliedNBT(@NotNull DoubleData data) {
// Make new ArrayList
ArrayList<ItemTag> ret = new ArrayList<>();
// Add Integer
ret.add(new ItemTag(getNBTPath(), (int) data.getValue()));
// Return thay
return ret;
}
@Override
public void whenLoaded(@NotNull ReadMMOItem mmoitem) {
// Get Relevant tags
ArrayList<ItemTag> relevantTags = new ArrayList<>();
if (mmoitem.getNBT().hasTag(getNBTPath()))
relevantTags.add(ItemTag.getTagAtPath(getNBTPath(), mmoitem.getNBT(), SupportedNBTTagValues.INTEGER));
// Attempt to build data
StatData data = getLoadedNBT(relevantTags);
// Success?
if (data != null) {
mmoitem.setData(this, data);
}
}
@Nullable
@Override
public DoubleData getLoadedNBT(@NotNull ArrayList<ItemTag> storedTags) {
// Find Tag
ItemTag cmd = ItemTag.getTagAtPath(getNBTPath(), storedTags);
// Found?
if (cmd != null) {
// Well thats it
return new DoubleData((Integer) cmd.getValue());
}
return null;
}
}

View File

@ -28,7 +28,7 @@ public class MaxStackSize extends DoubleStat implements GemStoneStat {
// Edit meta
item.getMeta().setMaxStackSize((int) Math.max(1, Math.min(99, data.getValue())));
// Apply Custom Model Data
// Save in NBT
item.addItemTag(getAppliedNBT(data));
}

View File

@ -27,7 +27,7 @@ import org.jetbrains.annotations.NotNull;
*/
public class MaximumDurability extends DoubleStat implements ItemRestriction, GemStoneStat, Upgradable {
public MaximumDurability() {
super("MAX_DURABILITY", Material.SHEARS, "Maximum Durability", new String[]{"The amount of uses before your", "item becomes unusable/breaks."}, new String[]{"!block", "all"});
super("MAX_DURABILITY", Material.SHEARS, "Maximum Custom Durability", new String[]{"The amount of uses before your", "item becomes unusable/breaks."}, new String[]{"!block", "all"});
}
@Override