!Small cleanup

This commit is contained in:
Indyuce 2020-08-12 13:38:05 +02:00
parent a40e3164fe
commit f1647d4be6
6 changed files with 78 additions and 46 deletions

View File

@ -22,6 +22,7 @@ import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.data.MaterialData;
import net.Indyuce.mmoitems.stat.data.StoredTagsData;
import net.Indyuce.mmoitems.stat.data.StringData;
import net.Indyuce.mmoitems.stat.data.StringListData;
import net.Indyuce.mmoitems.stat.data.UpgradeData;
import net.Indyuce.mmoitems.stat.data.type.UpgradeInfo;
import net.Indyuce.mmoitems.stat.type.DoubleStat;
@ -35,7 +36,7 @@ public class ItemStackBuilder {
private final ItemStack item;
private final ItemMeta meta;
private final MMOItemLore lore = new MMOItemLore();
private final LoreBuilder lore = new LoreBuilder();
private final List<ItemTag> tags = new ArrayList<>();
private static final AttributeModifier fakeModifier = new AttributeModifier(UUID.fromString("87851e28-af12-43f6-898e-c62bde6bd0ec"),
@ -63,7 +64,7 @@ public class ItemStackBuilder {
MMOItems.plugin.getUpdater().getData(mmoitem.getType(), mmoitem.getId()).getUniqueId().toString()));
}
public MMOItemLore getLore() {
public LoreBuilder getLore() {
return lore;
}
@ -95,14 +96,23 @@ public class ItemStackBuilder {
+ stat.getId() + "': " + exception.getMessage());
}
// lore
// Display gem stone lore
if (mmoitem.getType() == Type.GEM_STONE)
lore.insert("gem-stone-lore", ItemStat.translate("gem-stone-lore"));
// Display item type
lore.insert("item-type",
ItemStat.translate("item-type").replace("#",
mmoitem.getStats().contains(ItemStat.DISPLAYED_TYPE) ? ((StringData) mmoitem.getData(ItemStat.DISPLAYED_TYPE)).toString()
: mmoitem.getType().getName()));
// Calculate lore with placeholders
if (mmoitem.hasData(ItemStat.LORE)) {
List<String> parsed = new ArrayList<>();
((StringListData) mmoitem.getData(ItemStat.LORE)).getList().forEach(str -> parsed.add(lore.applyLorePlaceholders(str)));
lore.insert("lore", parsed);
}
meta.setLore(lore.build().toStringList());
/*
@ -110,23 +120,9 @@ public class ItemStackBuilder {
* modifiers, this way armor gives no ARMOR or ARMOR TOUGHNESS to the
* holder. Since 4.7 attributes are handled via custom calculations
*/
try {
meta.addAttributeModifier(Attribute.GENERIC_ATTACK_SPEED, fakeModifier);
item.setItemMeta(meta);
return MMOLib.plugin.getVersion().getWrapper().getNBTItem(item).addTag(tags);
/*
* On legacy spigot, it is not required to add a fake modifier to
* the modifier list, so just override the string tag and it works
* fine.
*/
} catch (NoSuchMethodError exception) {
item.setItemMeta(meta);
@SuppressWarnings("deprecation")
NBTItem nbt = MMOLib.plugin.getVersion().getWrapper().getNBTItem(item).cancelVanillaAttributeModifiers();
return nbt.addTag(tags);
}
meta.addAttributeModifier(Attribute.GENERIC_ATTACK_SPEED, fakeModifier);
item.setItemMeta(meta);
return MMOLib.plugin.getVersion().getWrapper().getNBTItem(item).addTag(tags);
}
/**
@ -141,14 +137,15 @@ public class ItemStackBuilder {
private final UpgradeData upgradeData;
/***
* @deprecated will be improved with mmoitems 6
*
* @param mmoitem
/**
* @deprecated Will be improved with MMOItems 6. Currently used to
* display upgrade stats in the lore. Should be improved to
* be more OOP friendly. NO MODIFICATIONS ALLOWED BEFORE A
* REWRITE
*/
public StatLore(MMOItem mmoitem) {
this.mmoitem = mmoitem.clone();
this.upgradeData = ((UpgradeData) mmoitem.getData(ItemStat.UPGRADE));
this.upgradeData = (UpgradeData) mmoitem.getData(ItemStat.UPGRADE);
}
public MMOItem getMMOItem() {
@ -156,9 +153,7 @@ public class ItemStackBuilder {
}
public boolean isUpgradable() {
if (upgradeData != null)
return upgradeData.getTemplate() != null;
return false;
return upgradeData != null && upgradeData.getTemplate() != null;
}
public MMOItem generateNewItem() {

View File

@ -1,14 +1,50 @@
package net.Indyuce.mmoitems.api.item.build;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.common.collect.Lists;
import net.Indyuce.mmoitems.MMOItems;
import net.mmogroup.mmolib.MMOLib;
public class MMOItemLore {
public class LoreBuilder {
private final List<String> lore = MMOItems.plugin.getLanguage().getDefaultLoreFormat();
private final Map<String, String> placeholders = new HashMap<>();
/**
* Registers a placeholder. All placeholders registered will be parsed when
* using applyLorePlaceholders(String)
*
* @param path
* The placeholder path (CASE SENSITIVE)
* @param value
* The placeholder value which is instantly saved as a string
* when registered
*/
public void registerPlaceholder(String path, Object value) {
placeholders.put(path, value.toString());
}
/**
* Parses a string with registered placeholders
*
* @param str
* String with {..} unformatted placeholders
* @return Same string with replaced placeholders. Placeholders which
* couldn't be found are marked with PHE which means
* PlaceHolderError
*/
public String applyLorePlaceholders(String str) {
while (str.contains("{") && str.substring(str.indexOf("{")).contains("}")) {
String holder = str.substring(str.indexOf("{") + 1, str.indexOf("}"));
str = str.replace("{" + holder + "}", placeholders.containsKey(holder) ? placeholders.get(holder) : "PHE");
}
return MMOLib.plugin.parseColors(str);
}
/**
* Inserts a list of strings in the item lore. The lines are added only if a
@ -54,7 +90,7 @@ public class MMOItemLore {
* have been inserted in the lore. It cleans all unused lore format
* # lines as well as lore bars
*/
public MMOItemLore build() {
public LoreBuilder build() {
/*
* loops backwards to remove all unused bars in one iteration only,

View File

@ -67,9 +67,12 @@ public class Abilities extends ItemStat {
MMOItems.plugin.getLanguage().getAbilityName(ability.getAbility())));
jsonArray.add(ability.toJson());
ability.getModifiers()
.forEach(modifier -> abilityLore.add(modifierFormat.replace("#m", MMOItems.plugin.getLanguage().getModifierName(modifier))
.replace("#v", this.modifierFormat.format(ability.getModifier(modifier)))));
for (String modifier : ability.getModifiers()) {
item.getLore().registerPlaceholder("ability_" + ability.getAbility().getID().toLowerCase() + "_" + modifier,
this.modifierFormat.format(ability.getModifier(modifier)));
abilityLore.add(modifierFormat.replace("#m", MMOItems.plugin.getLanguage().getModifierName(modifier)).replace("#v",
this.modifierFormat.format(ability.getModifier(modifier))));
}
if (splitter)
abilityLore.add(MMOItems.plugin.getLanguage().abilitySplitter);

View File

@ -36,7 +36,7 @@ public class ArrowParticles extends ItemStat {
@Override
public ArrowParticlesData whenInitialized(Object object) {
Validate.isTrue(object instanceof ConfigurationSection, "Must specifiy a valid config section");
Validate.isTrue(object instanceof ConfigurationSection, "Must specify a valid config section");
ConfigurationSection config = (ConfigurationSection) object;
Validate.isTrue(config.contains("particle"), "Could not find arrow particle");

View File

@ -88,14 +88,16 @@ public class Lore extends ItemStat implements ProperStat {
@Override
public void whenApplied(ItemStackBuilder item, StatData data) {
List<String> lore = new ArrayList<>();
/*
* The lore is not directly inserted into the final itemStack lore
* because all stats have not registered all their lore placeholders
* yet. The lore is only saved in a JSon array so that it can be
* recalculated LATER on with right placeholders
*/
JsonArray array = new JsonArray();
((StringListData) data).getList().forEach(line -> {
line = MMOLib.plugin.parseColors(line);
array.add(line);
lore.add(line);
});
item.getLore().insert("lore", lore);
((StringListData) data).getList().forEach(line -> array.add(line));
item.addItemTag(new ItemTag("MMOITEMS_LORE", array.toString()));
}

View File

@ -6,7 +6,7 @@ import org.bukkit.inventory.ItemStack;
public abstract class AttributeStat extends DoubleStat {
/*
* attribute offset for e.g attack speed must be lowered when holding a
* Йttribute offset for e.g attack speed must be lowered when holding a
* weapon by 4 because this is the default base attribute value.
*/
private final double offset;
@ -23,10 +23,6 @@ public abstract class AttributeStat extends DoubleStat {
this.attribute = attribute;
}
// public boolean isWeaponSpecific() {
// return offset > 0;
// }
public Attribute getAttribute() {
return attribute;
}