Upgrading now actually compatible with Item Stat MAX_DURABILITY

(In the process, it was made that items fully repair when upgraded, does anyone mind?)

Fixes upgrade consumables saying "You would not be able to use this item" if the upgrade template has this stat, and it had no max durability originally.
This commit is contained in:
Gunging 2021-02-25 10:25:14 -06:00
parent 98ffcc5775
commit 17a8ef46f0
7 changed files with 137 additions and 28 deletions

View File

@ -11,6 +11,7 @@ import io.lumine.mythic.lib.api.util.ui.FriendlyFeedbackProvider;
import net.Indyuce.mmoitems.ItemStats;
import net.Indyuce.mmoitems.api.util.message.FriendlyFeedbackPalette_MMOItems;
import net.Indyuce.mmoitems.stat.Enchants;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.data.UpgradeData;
import net.Indyuce.mmoitems.stat.data.type.Mergeable;
import net.Indyuce.mmoitems.stat.data.type.StatData;
@ -125,11 +126,20 @@ public class UpgradeTemplate {
// For every Stat-UpgradeInfo pair
for (ItemStat stat : perStatUpgradeInfos.keySet()) {
// Preprocess
((Upgradable) stat).preprocess(mmoitem);
// Initializes Stat History
StatHistory<StatData> hist = StatHistory.From(mmoitem, stat);
// Midprocess
((Upgradable) stat).midprocess(mmoitem);
// The Stat History now manages applying upgrades.
mmoitem.setData(stat, hist.Recalculate());
// Postprocess
((Upgradable) stat).postprocess(mmoitem);
}
}

View File

@ -91,7 +91,7 @@ public class DurabilityItem {
public DurabilityItem addDurability(int gain) {
CustomDurabilityRepair evG = new CustomDurabilityRepair(this, gain);
Bukkit.getPluginManager().callEvent(evG);
if (!evG.isCancelled()) { return this; }
if (evG.isCancelled()) { return this; }
Validate.isTrue(gain > 0, "Durability gain must be greater than 0");
durability = Math.max(0, Math.min(durability + gain, maxDurability));
@ -101,7 +101,7 @@ public class DurabilityItem {
public DurabilityItem decreaseDurability(int loss) {
CustomDurabilityDamage evG = new CustomDurabilityDamage(this, loss);
Bukkit.getPluginManager().callEvent(evG);
if (!evG.isCancelled()) { return this; }
if (evG.isCancelled()) { return this; }
/*
* Calculate the chance of the item not losing any durability because of

View File

@ -24,6 +24,7 @@ import org.bukkit.attribute.AttributeModifier.Operation;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
@ -32,7 +33,7 @@ import java.util.UUID;
import java.util.logging.Level;
public class ItemStackBuilder {
private MMOItem mmoitem;
@NotNull private final MMOItem mmoitem;
private final ItemStack item;
private final ItemMeta meta;
@ -47,7 +48,7 @@ public class ItemStackBuilder {
*
* @param mmoitem The mmoitem you want to build
*/
public ItemStackBuilder(MMOItem mmoitem) {
public ItemStackBuilder(@NotNull MMOItem mmoitem) {
// Reference to source MMOItem
this.mmoitem = mmoitem;
@ -77,13 +78,9 @@ public class ItemStackBuilder {
MMOItems.plugin.getUpdater().getData(mmoitem.getType(), mmoitem.getId()).getUniqueId().toString()));*/
}
public LoreBuilder getLore() {
return lore;
}
public LoreBuilder getLore() { return lore; }
public MMOItem getMMOItem() {
return mmoitem;
}
@NotNull public MMOItem getMMOItem() { return mmoitem; }
/**
* @return Does NOT return the built item stack. It returns only returns the
@ -92,13 +89,9 @@ public class ItemStackBuilder {
* of a specific material (like the Shield Pattern stat which checks if
* the item is a shield)
*/
public ItemStack getItemStack() {
return item;
}
public ItemStack getItemStack() { return item; }
public ItemMeta getMeta() {
return meta;
}
public ItemMeta getMeta() { return meta; }
public void addItemTag(List<ItemTag> newTags) { tags.addAll(newTags); }
@ -200,7 +193,5 @@ public class ItemStackBuilder {
/**
* @return Builds the item
*/
public ItemStack build() {
return new DynamicLore(buildNBT()).build();
}
public ItemStack build() { return new DynamicLore(buildNBT()).build(); }
}

View File

@ -2,6 +2,8 @@ package net.Indyuce.mmoitems.api.player;
import java.text.DecimalFormat;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
@ -106,11 +108,16 @@ public abstract class RPGPlayer {
return false;
}
//REQ//MMOItems. Log("Checking REQS");
for (ItemRestriction condition : MMOItems.plugin.getStats().getItemRestrictionStats()) {
//REQ//MMOItems. Log(" \u00a7a> \u00a77" + ((ItemStat) condition).getNBTPath());
if (!condition.isDynamic() || !allowDynamic) {
//REQ//MMOItems. Log(" \u00a78> \u00a77Nondynamic / Dynamic Unallowed");
if (!condition.canUse(this, item, message)) {
//REQ//MMOItems. Log(" \u00a7c> Cant use");
return false; } } }
//REQ//MMOItems. Log(" \u00a7a> Success use");
return true;
}

View File

@ -1,5 +1,17 @@
package net.Indyuce.mmoitems.stat;
import io.lumine.mythic.lib.api.util.ui.FriendlyFeedbackCategory;
import io.lumine.mythic.lib.api.util.ui.FriendlyFeedbackProvider;
import io.lumine.mythic.lib.api.util.ui.PlusMinusPercent;
import net.Indyuce.mmoitems.ItemStats;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.api.util.message.FriendlyFeedbackPalette_MMOItems;
import net.Indyuce.mmoitems.stat.data.MaterialData;
import net.Indyuce.mmoitems.stat.data.type.UpgradeInfo;
import net.Indyuce.mmoitems.stat.type.Upgradable;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
@ -14,9 +26,14 @@ import net.Indyuce.mmoitems.stat.type.ItemRestriction;
import net.Indyuce.mmoitems.stat.type.GemStoneStat;
import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.api.item.NBTItem;
import org.bukkit.entity.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class MaximumDurability extends DoubleStat implements ItemRestriction, GemStoneStat {
import java.util.ArrayList;
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"});
}
@ -26,19 +43,80 @@ public class MaximumDurability extends DoubleStat implements ItemRestriction, Ge
*/
@Override
public void whenApplied(@NotNull ItemStackBuilder item, @NotNull StatData data) {
int value = (int) ((DoubleData) data).getValue();
item.addItemTag(new ItemTag("MMOITEMS_MAX_DURABILITY", value), new ItemTag("MMOITEMS_DURABILITY", value));
super.whenApplied(item, data);
}
@Override
public @NotNull
ArrayList<ItemTag> getAppliedNBT(@NotNull StatData data) {
// Create Fresh
ArrayList<ItemTag> ret = new ArrayList<>();
// Add sole tag
ret.add(new ItemTag(getNBTPath(), ((DoubleData) data).getValue()));
ret.add(new ItemTag(ItemStats.DURABILITY.getNBTPath(), ((DoubleData) data).getValue()));
// Return thay
return ret;
}
@Override
public void preprocess(@NotNull MMOItem item) {
// If this has no Max Upgrade Data
if (!item.hasData(ItemStats.MAX_DURABILITY)) {
// What durability will it have?
int base = 400;
// I mean bruh
if (item.hasData(ItemStats.MATERIAL)) {
// Use vanilla max durability
MaterialData data = (MaterialData) item.getData(ItemStats.MATERIAL);
// Get mat
Material mat = data.getMaterial();
base = mat.getMaxDurability();
}
// Yea no
if (base < 8) { base = 400; }
// Set max dura
item.setData(ItemStats.MAX_DURABILITY, new DoubleData(base));
item.setData(ItemStats.DURABILITY, new DoubleData(base));
}
}
@Override
public boolean canUse(RPGPlayer player, NBTItem item, boolean message) {
if (item.hasTag("MMOITEMS_DURABILITY") && item.getDouble("MMOITEMS_DURABILITY") <= 0) {
// No max durability not MMOItems' problem
if (!item.hasTag(ItemStats.MAX_DURABILITY.getNBTPath())) { return true; }
// No durability? Uuuuh that's weird but ok
if (!item.hasTag(ItemStats.DURABILITY.getNBTPath())) {
// Initialize to max durability and roll
item.addTag(new ItemTag(ItemStats.DURABILITY.getNBTPath(), item.getDouble(ItemStats.MAX_DURABILITY.getNBTPath())));
}
if (item.getDouble(ItemStats.DURABILITY.getNBTPath()) <= 0) {
if (message) {
Message.ZERO_DURABILITY.format(ChatColor.RED).send(player.getPlayer(), "cant-use-item");
player.getPlayer().playSound(player.getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f);
}
player.getPlayer().playSound(player.getPlayer().getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1.5f); }
return false;
}
return true;
}
@NotNull
@Override
public UpgradeInfo loadUpgradeInfo(@Nullable Object obj) throws IllegalArgumentException {
// Return result of thay
return DoubleStat.DoubleUpgradeInfo.GetFrom(obj);
}
}

View File

@ -19,9 +19,6 @@ import org.bukkit.potion.PotionEffectType;
public class RandomPotionEffectListData implements RandomStatData {
private final List<RandomPotionEffectData> effects = new ArrayList<>();
boolean containsObsoleteConfigurations = false;
public void MarkForSaveAll() { containsObsoleteConfigurations = true; }
public boolean isMarkedForSaveAll() { return containsObsoleteConfigurations; }
public RandomPotionEffectListData(ConfigurationSection config) {
Validate.notNull(config, "Config cannot be null");

View File

@ -1,5 +1,6 @@
package net.Indyuce.mmoitems.stat.type;
import net.Indyuce.mmoitems.api.item.mmoitem.MMOItem;
import net.Indyuce.mmoitems.stat.data.type.Mergeable;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.data.type.UpgradeInfo;
@ -47,4 +48,29 @@ public interface Upgradable {
* May be negative if the stat supports it.
*/
@NotNull StatData apply(@NotNull StatData original, @NotNull UpgradeInfo info, int level);
/**
* Does your stat need to do anything to a MMOItem prior to recalculating upgrades?
* This happens even before generating the {@link StatHistory} in case you need
* to check the default values.
* <p></p>
* <b>Only use if you know what you're doing.</b>
*/
default void preprocess(@NotNull MMOItem item) { }
/**
* Does your stat need to do anything to a MMOItem prior to recalculating upgrades?
* This happens even after generating the {@link StatHistory}.
* <p></p>
* <b>Only use if you know what you're doing.</b>
*/
default void midprocess(@NotNull MMOItem item) { }
/**
* Does your stat need to do anything to a MMOItem right after recalculating upgrades?
* <p></p>
* <b>Only use if you know what you're doing.</b>
*/
default void postprocess(@NotNull MMOItem item) {}
}