!Refactor stat API, moved StoredTags to InternalStat, fixed bugs.

This commit is contained in:
Indyuce 2020-03-29 19:10:19 +02:00
parent 94e4bc599c
commit 0d3abd3df0
43 changed files with 250 additions and 271 deletions

Binary file not shown.

View File

@ -234,8 +234,7 @@ public class MMOItems extends JavaPlugin {
// advanced recipes
getLogger().log(Level.INFO, "Loading recipes, please wait...");
recipeManager = MMOLib.plugin.getVersion().isStrictlyHigher(1, 12) ?
new RecipeManagerDefault() : new RecipeManagerLegacy();
recipeManager = MMOLib.plugin.getVersion().isStrictlyHigher(1, 12) ? new RecipeManagerDefault() : new RecipeManagerLegacy();
// commands
getCommand("mmoitems").setExecutor(new MMOItemsCommand());

View File

@ -62,6 +62,13 @@ public class MMOUtils {
player.getWorld().dropItem(player.getLocation(), drop);
}
public static PotionEffectType valueOfPotionEffectType(String effect) {
for (PotionEffectType checked : PotionEffectType.values())
if (checked.getName().equals(effect.toUpperCase().replace("-", "_")))
return checked;
return null;
}
public static LivingEntity getDamager(EntityDamageByEntityEvent event) {
/*

View File

@ -16,7 +16,6 @@ import net.Indyuce.mmoitems.stat.data.Mergeable;
import net.Indyuce.mmoitems.stat.type.DoubleStat.DoubleData;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.mmogroup.mmolib.api.item.NBTItem;
import net.mmogroup.mmolib.api.item.StoredTags;
public class GemStone extends UseItem {
@ -25,7 +24,7 @@ public class GemStone extends UseItem {
}
public ApplyResult applyOntoItem(NBTItem target, Type targetType) {
StoredTags storedTags = new StoredTags(target);
/*
* loads all stats and calculates EVERY piece of the lore again.
*/
@ -44,8 +43,7 @@ public class GemStone extends UseItem {
* weapon
*/
String appliableTypes = getNBTItem().getString("MMOITEMS_ITEM_TYPE_RESTRICTION");
if (!appliableTypes.equals(""))
if ((!targetType.isWeapon() || !appliableTypes.contains("WEAPON")) && !appliableTypes.contains(targetType.getItemSet().name()) && !appliableTypes.contains(targetType.getId()))
if (!appliableTypes.equals("") && (!targetType.isWeapon() || !appliableTypes.contains("WEAPON")) && !appliableTypes.contains(targetType.getItemSet().name()) && !appliableTypes.contains(targetType.getId()))
return new ApplyResult(ResultType.NONE);
// check for success rate
@ -84,7 +82,7 @@ public class GemStone extends UseItem {
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 2);
Message.GEM_STONE_APPLIED.format(ChatColor.YELLOW, "#gem#", MMOUtils.getDisplayName(getItem()), "#item#", MMOUtils.getDisplayName(target.getItem())).send(player);
return new ApplyResult(storedTags.reapply(targetMMO.newBuilder().build()).toItem());
return new ApplyResult(targetMMO.newBuilder().build());
}
public class ApplyResult {

View File

@ -87,8 +87,8 @@ public class ItemSkin extends UseItem {
}
public class ApplyResult {
private ResultType type;
private ItemStack result;
private final ResultType type;
private final ItemStack result;
public ApplyResult(ResultType type) {
this(null, type);

View File

@ -15,30 +15,34 @@ import org.bukkit.inventory.meta.ItemMeta;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.item.MMOItem;
import net.Indyuce.mmoitems.stat.MaterialStat.MaterialData;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.Indyuce.mmoitems.stat.type.StringStat.StringData;
import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.api.item.ItemTag;
import net.mmogroup.mmolib.api.item.NBTItem;
import net.mmogroup.mmolib.version.VersionMaterial;
public class MMOItemBuilder {
private final MMOItem mmoitem;
private ItemStack item = new ItemStack(VersionMaterial.NETHER_WART.toMaterial());
private ItemMeta meta;
private MMOItemLore lore = new MMOItemLore();
private List<ItemTag> tags = new ArrayList<>();
private final ItemStack item;
private final ItemMeta meta;
private final MMOItemLore lore = new MMOItemLore();
private final List<ItemTag> tags = new ArrayList<>();
private static final AttributeModifier fakeModifier = new AttributeModifier(UUID.fromString("87851e28-af12-43f6-898e-c62bde6bd0ec"), "generic.attackSpeed", 0, Operation.ADD_NUMBER);
public MMOItemBuilder(MMOItem item) {
mmoitem = item;
public MMOItemBuilder(MMOItem mmoitem) {
this.mmoitem = mmoitem;
tags.add(new ItemTag("MMOITEMS_ITEM_TYPE", item.getType().getId()));
tags.add(new ItemTag("MMOITEMS_ITEM_ID", item.getId()));
item = new ItemStack(mmoitem.hasData(ItemStat.MATERIAL) ? ((MaterialData) mmoitem.getData(ItemStat.MATERIAL)).getMaterial() : Material.DIAMOND_SWORD);
meta = item.getItemMeta();
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
String path = item.getType().getId() + "." + item.getId();
tags.add(new ItemTag("MMOITEMS_ITEM_TYPE", mmoitem.getType().getId()));
tags.add(new ItemTag("MMOITEMS_ITEM_ID", mmoitem.getId()));
String path = mmoitem.getType().getId() + "." + mmoitem.getId();
if (MMOItems.plugin.getUpdater().hasData(path))
tags.add(new ItemTag("MMOITEMS_ITEM_UUID", MMOItems.plugin.getUpdater().getData(path).getUniqueId().toString()));
}
@ -64,15 +68,6 @@ public class MMOItemBuilder {
tags.add(itemTag);
}
public Material getMaterial() {
return item.getType();
}
public void setMaterial(Material material) {
item.setType(material);
(meta = item.getItemMeta()).addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
}
public ItemStack build() {
for (ItemStat stat : mmoitem.getStats())
@ -91,6 +86,7 @@ public class MMOItemBuilder {
* holder. since 4.7 attributes are handled via custom calculations
*/
try {
meta.addAttributeModifier(Attribute.GENERIC_ATTACK_SPEED, fakeModifier);
item.setItemMeta(meta);
return MMOLib.plugin.getNMS().getNBTItem(item).addTag(tags).toItem();

View File

@ -16,7 +16,6 @@ import net.Indyuce.mmoitems.api.ConfigFile;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.util.AltChar;
import net.Indyuce.mmoitems.gui.PluginInventory;
import net.mmogroup.mmolib.version.VersionMaterial;
public abstract class EditionInventory extends PluginInventory {
protected final Type type;
@ -31,6 +30,7 @@ public abstract class EditionInventory extends PluginInventory {
public EditionInventory(Player player, Type type, String id, ItemStack cached) {
super(player);
this.type = type;
this.id = id == null ? null : id.toUpperCase().replace("-", "_").replace(" ", "_");
this.cached = player.getOpenInventory() != null && player.getOpenInventory().getTopInventory().getHolder() instanceof EditionInventory ? ((EditionInventory) player.getOpenInventory().getTopInventory().getHolder()).cached : cached;
@ -75,7 +75,7 @@ public abstract class EditionInventory extends PluginInventory {
}
public void addEditionInventoryItems(Inventory inv, boolean backBool) {
ItemStack get = new ItemStack(VersionMaterial.GUNPOWDER.toMaterial());
ItemStack get = new ItemStack(Material.CHEST);
ItemMeta getMeta = get.getItemMeta();
getMeta.addItemFlags(ItemFlag.values());
getMeta.setDisplayName(ChatColor.GREEN + AltChar.fourEdgedClub + " Get the Item! " + AltChar.fourEdgedClub);

View File

@ -3,6 +3,7 @@ package net.Indyuce.mmoitems.manager;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
@ -23,7 +24,6 @@ public class ItemManager {
Bukkit.getScheduler().runTaskTimerAsynchronously(MMOItems.plugin, () -> clearCache(), 60 * 20, 2 * 60 * 20);
}
public MMOItem getMMOItem(Type type, String id) {
id = id.toUpperCase().replace("-", "_").replace(" ", "_");
@ -43,8 +43,12 @@ public class ItemManager {
ConfigurationSection section = items.getConfigurationSection(id);
for (ItemStat stat : type.getAvailableStats())
if (section.contains(stat.getPath()) && !stat.whenLoaded(mmoitem, section))
return null;
if (section.contains(stat.getPath()))
try {
stat.whenLoaded(mmoitem, section);
} catch (IllegalArgumentException exception) {
MMOItems.plugin.getLogger().log(Level.WARNING, "Error while generating " + type.getId() + "." + id + ": " + exception.getMessage());
}
if (useCache)
cache(mmoitem);

View File

@ -42,7 +42,7 @@ public class Abilities extends ItemStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
AbilityListData list = new AbilityListData();
for (String key : config.getConfigurationSection("ability").getKeys(false)) {
@ -52,7 +52,6 @@ public class Abilities extends ItemStat {
}
item.setData(ItemStat.ABILITIES, list);
return true;
}
@Override
@ -167,10 +166,7 @@ public class Abilities extends ItemStat {
}
public class AbilityListData extends StatData implements Mergeable {
private Set<AbilityData> abilities = new LinkedHashSet<>();
public AbilityListData() {
}
private final Set<AbilityData> abilities = new LinkedHashSet<>();
public AbilityListData(AbilityData... abilities) {
add(abilities);

View File

@ -1,8 +1,8 @@
package net.Indyuce.mmoitems.stat;
import java.util.List;
import java.util.logging.Level;
import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor;
import org.bukkit.Particle;
import org.bukkit.configuration.ConfigurationSection;
@ -35,18 +35,12 @@ public class Arrow_Particles extends StringStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
if (!config.getConfigurationSection("arrow-particles").contains("particle"))
return true;
ArrowParticlesData data = new ArrowParticlesData();
public void whenLoaded(MMOItem item, ConfigurationSection config) {
Validate.isTrue(config.getConfigurationSection("arrow-particles").contains("particle"), "Could not find arrow particle");
ArrowParticlesData data = new ArrowParticlesData();
String particleFormat = config.getString("arrow-particles.particle").toUpperCase().replace("-", "_").replace(" ", "_");
try {
data.particle = Particle.valueOf(particleFormat);
} catch (IllegalArgumentException exception) {
data.getMMOItem().log(Level.WARNING, particleFormat + " is not a valid particle name.");
return true;
}
data.amount = config.getInt("arrow-particles.amount");
data.offset = config.getDouble("arrow-particles.offset");
@ -60,7 +54,6 @@ public class Arrow_Particles extends StringStat {
data.speed = config.getDouble("arrow-particles.speed");
item.setData(ItemStat.ARROW_PARTICLES, data);
return true;
}
@Override

View File

@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor;
@ -114,23 +113,15 @@ public class Commands extends ItemStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
if (!config.contains("commands"))
return true;
public void whenLoaded(MMOItem item, ConfigurationSection config) {
CommandListData list = new CommandListData();
for (String key : config.getConfigurationSection("commands").getKeys(false)) {
ConfigurationSection section = config.getConfigurationSection("commands." + key);
try {
list.add(list.newCommandData(section.getString("format"), section.getDouble("delay"), section.getBoolean("console"), section.getBoolean("op")));
} catch (IllegalArgumentException exception) {
item.log(Level.WARNING, "Couldn't load command ID " + section.getName());
}
}
item.setData(ItemStat.COMMANDS, list);
return true;
}
@Override

View File

@ -83,9 +83,8 @@ public class Compatible_Types extends StringStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
item.setData(ItemStat.COMPATIBLE_TYPES, new StringListData(config.getStringList("compatible-types")));
return true;
}
@Override

View File

@ -16,8 +16,7 @@ public class Craft_Permission extends StringStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
return true;
public void whenLoaded(MMOItem item, ConfigurationSection config) {
}
@Override

View File

@ -107,8 +107,7 @@ public class Crafting extends ItemStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
return true;
public void whenLoaded(MMOItem item, ConfigurationSection config) {
}
@Override

View File

@ -105,7 +105,7 @@ public class CustomSounds extends ItemStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
SoundListData sounds = new SoundListData();
for (CustomSound sound : CustomSound.values()) {
@ -121,7 +121,6 @@ public class CustomSounds extends ItemStat {
}
item.setData(ItemStat.CUSTOM_SOUNDS, sounds);
return true;
}
@Override

View File

@ -83,9 +83,8 @@ public class Dye_Color extends StringStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
item.setData(ItemStat.DYE_COLOR, new ColorData(item, config.getString("dye-color")));
return true;
}
@Override

View File

@ -5,12 +5,12 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.craftbukkit.libs.org.apache.commons.lang3.Validate;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
@ -150,45 +150,21 @@ public class Effects extends ItemStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
EffectListData effects = new EffectListData();
for (String effect : config.getConfigurationSection("effects").getKeys(false)) {
PotionEffectType type = null;
for (PotionEffectType type1 : PotionEffectType.values())
if (type1 != null && type1.getName().equals(effect.toUpperCase().replace("-", "_"))) {
type = type1;
break;
}
if (type == null) {
item.log(Level.WARNING, "[Potion Effects] " + effect + " is not a valid potion effect name.");
continue;
}
PotionEffectType type = MMOUtils.valueOfPotionEffectType(effect);
Validate.isTrue(type != null, "Could not find potion effect type named '" + effect + "'");
String[] split = config.getString("effects." + effect).split("\\,");
double duration = 0;
try {
duration = Double.parseDouble(split[0]);
} catch (Exception e) {
item.log(Level.WARNING, "[Potion Effects] " + split[0] + " is not a valid number.");
continue;
}
int amplifier = 0;
if (split.length > 1)
try {
amplifier = Integer.parseInt(split[1]);
} catch (Exception e) {
item.log(Level.WARNING, "[Potion Effects] " + split[1] + " is not a valid integer.");
continue;
}
double duration = Double.parseDouble(split[0]);
int amplifier = Integer.parseInt(split[1]);
effects.add(new PotionEffectData(type, duration, amplifier));
}
item.setData(ItemStat.EFFECTS, effects);
return true;
}
@Override

View File

@ -99,7 +99,7 @@ public class Elements extends ItemStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
ElementListData elements = new ElementListData();
for (Element element : Element.values()) {
@ -116,7 +116,6 @@ public class Elements extends ItemStat {
}
item.setData(ItemStat.ELEMENTS, elements);
return true;
}
@Override

View File

@ -116,7 +116,7 @@ public class Enchants extends ItemStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
EnchantListData enchants = new EnchantListData();
for (String format : config.getConfigurationSection("enchants").getKeys(false)) {
@ -136,7 +136,6 @@ public class Enchants extends ItemStat {
}
item.setData(ItemStat.ENCHANTS, enchants);
return true;
}
@Override

View File

@ -35,9 +35,9 @@ public class Gem_Sockets extends ItemStat {
super(new ItemStack(Material.EMERALD), "Gem Sockets", new String[] { "The amount of gem", "sockets your weapon has." }, "gem-sockets", new String[] { "piercing", "slashing", "blunt", "offhand", "range", "tool", "armor", "accessory" });
}
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
@Override
public void whenLoaded(MMOItem item, ConfigurationSection config) {
item.setData(this, new GemSocketsData(new StringListData(config.getStringList("gem-sockets"))));
return true;
}
@Override

View File

@ -40,14 +40,10 @@ public class Item_Particles extends ItemStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
if (!config.contains("item-particles"))
return true;
public void whenLoaded(MMOItem item, ConfigurationSection config) {
ParticleData data = new ParticleData(item, config.getConfigurationSection("item-particles"));
if (data.isValid())
item.setData(ItemStat.ITEM_PARTICLES, data);
return true;
}
@Override

View File

@ -117,9 +117,8 @@ public class Item_Type_Restriction extends StringStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
item.setData(ItemStat.ITEM_TYPE_RESTRICTION, new StringListData(config.getStringList(getPath())));
return true;
}
@Override

View File

@ -83,9 +83,8 @@ public class Lore extends StringStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
item.setData(ItemStat.LORE, new StringListData(config.getStringList("lore")));
return true;
}
@Override

View File

@ -1,7 +1,6 @@
package net.Indyuce.mmoitems.stat;
import java.util.logging.Level;
import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
@ -51,23 +50,17 @@ public class MaterialStat extends StringStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
MaterialData material = new MaterialData();
try {
material.setMaterial(Material.valueOf(config.getString("material").toUpperCase().replace("-", "_").replace(" ", "_")));
} catch (Exception e) {
material.setMaterial(VersionMaterial.NETHER_WART.toMaterial());
item.log(Level.WARNING, "Could not read material from item, using default");
}
public void whenLoaded(MMOItem item, ConfigurationSection config) {
MaterialData material = new MaterialData(Material.valueOf(config.getString("material").toUpperCase().replace("-", "_").replace(" ", "_")));
item.setData(ItemStat.MATERIAL, material);
return true;
}
@Override
public boolean whenApplied(MMOItemBuilder item, StatData data) {
item.setMaterial(((MaterialData) data).getMaterial());
/*
* material is set handled directly in the MMOBuilder constructor
* therefore nothing needs to be done here
*/
return true;
}
@ -79,15 +72,18 @@ public class MaterialStat extends StringStat {
public class MaterialData extends StatData {
private Material material;
public MaterialData() {
}
/*
* material must not be null because it is called directly in the
* MMOBuilder constructor.
*/
public MaterialData(Material material) {
Validate.notNull(material, "Material must not be null");
this.material = material;
}
public void setMaterial(Material value) {
material = value;
public void setMaterial(Material material) {
Validate.notNull(material, "Material must not be null");
this.material = material;
}
public Material getMaterial() {

View File

@ -89,9 +89,8 @@ public class NBT_Tags extends StringStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
item.setData(ItemStat.NBT_TAGS, new StringListData(config.getStringList("custom-nbt")));
return true;
}
@Override

View File

@ -3,12 +3,12 @@ package net.Indyuce.mmoitems.stat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.craftbukkit.libs.org.apache.commons.lang3.Validate;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
@ -118,27 +118,16 @@ public class Perm_Effects extends ItemStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
EffectListData effects = new EffectListData();
for (String effect : config.getConfigurationSection("perm-effects").getKeys(false)) {
PotionEffectType type = null;
for (PotionEffectType type1 : PotionEffectType.values())
if (type1 != null && type1.getName().equals(effect.toUpperCase().replace("-", "_"))) {
type = type1;
break;
}
if (type == null) {
item.log(Level.WARNING, "[Potion Effects] " + effect + " is not a valid potion effect name.");
continue;
}
PotionEffectType type = MMOUtils.valueOfPotionEffectType(effect);
Validate.isTrue(type != null, "Could not find potion effect type named '" + effect + "'");
effects.add(new PotionEffectData(type, config.getInt("perm-effects." + effect)));
}
item.setData(ItemStat.PERM_EFFECTS, effects);
return true;
}
@Override

View File

@ -87,9 +87,8 @@ public class Permission extends ItemStat implements Conditional {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
item.setData(ItemStat.PERMISSION, new StringListData(config.getStringList("permission")));
return true;
}
@Override

View File

@ -82,14 +82,13 @@ public class Potion_Color extends StringStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
item.setData(ItemStat.POTION_COLOR, new ColorData(item, config.getString("potion-color")));
return true;
}
@Override
public boolean whenApplied(MMOItemBuilder item, StatData data) {
if (item.getMaterial().name().contains("POTION") || item.getMaterial() == Material.TIPPED_ARROW)
if (item.getItemStack().getType().name().contains("POTION") || item.getItemStack().getType() == Material.TIPPED_ARROW)
((PotionMeta) item.getMeta()).setColor(((ColorData) data).getColor());
return true;
}

View File

@ -141,16 +141,11 @@ public class Potion_Effects extends StringStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
EffectListData effects = new EffectListData();
for (String effect : config.getConfigurationSection("potion-effects").getKeys(false)) {
PotionEffectType type = null;
for (PotionEffectType type1 : PotionEffectType.values())
if (type1 != null && type1.getName().equals(effect.toUpperCase().replace("-", "_"))) {
type = type1;
break;
}
PotionEffectType type = MMOUtils.valueOfPotionEffectType(effect);
if (type == null) {
item.log(Level.WARNING, "[Potion Effects] " + effect + " is not a valid potion effect name.");
@ -179,12 +174,11 @@ public class Potion_Effects extends StringStat {
}
item.setData(ItemStat.POTION_EFFECTS, effects);
return true;
}
@Override
public boolean whenApplied(MMOItemBuilder item, StatData data) {
if (item.getMaterial().name().contains("POTION") || item.getMaterial() == Material.TIPPED_ARROW)
if (item.getItemStack().getType().name().contains("POTION") || item.getItemStack().getType() == Material.TIPPED_ARROW)
for(PotionEffectData effect : ((EffectListData) data).getEffects())
((PotionMeta) item.getMeta()).addCustomEffect(effect.toEffect(), false);
return true;

View File

@ -90,9 +90,8 @@ public class Required_Class extends StringStat implements Conditional {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
item.setData(ItemStat.REQUIRED_CLASS, new StringListData(config.getStringList("required-class")));
return true;
}
@Override

View File

@ -94,9 +94,8 @@ public class Restore extends StringStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
item.setData(ItemStat.RESTORE, new RestoreData(config.getDouble("restore.health"), config.getDouble("restore.food"), config.getDouble("restore.saturation")));
return true;
}
@Override

View File

@ -3,7 +3,6 @@ package net.Indyuce.mmoitems.stat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.DyeColor;
@ -39,48 +38,29 @@ public class Shield_Pattern extends StringStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
ShieldPatternData shieldPattern = new ShieldPatternData();
// dye color
if (config.getConfigurationSection("shield-pattern").contains("color")) {
String colorFormat = config.getString("shield-pattern.color").toUpperCase().replace("-", "_").replace(" ", "_");
try {
shieldPattern.setBaseColor(DyeColor.valueOf(colorFormat));
} catch (Exception e1) {
item.log(Level.WARNING, "Could read dye color from " + colorFormat);
return true;
}
ConfigurationSection section = config.getConfigurationSection("shield-pattern");
if (section.contains("color")) {
String format = config.getString("shield-pattern.color").toUpperCase().replace("-", "_").replace(" ", "_");
shieldPattern.setBaseColor(DyeColor.valueOf(format));
}
// apply patterns
for (String key : config.getConfigurationSection("shield-pattern").getKeys(false)) {
if (key.equalsIgnoreCase("color"))
continue;
for (String key : config.getConfigurationSection("shield-pattern").getKeys(false))
if (!key.equalsIgnoreCase("color")) {
String format = config.getString("shield-pattern." + key + ".pattern").toUpperCase().replace("-", "_").replace(" ", "_");
PatternType type;
try {
type = PatternType.valueOf(format);
} catch (Exception e1) {
item.log(Level.WARNING, "[Shield Pattern] " + format + " is not a valid pattern type!");
continue;
}
PatternType type = PatternType.valueOf(format);
format = config.getString("shield-pattern." + key + ".color").toUpperCase().replace("-", "_").replace(" ", "_");
DyeColor color;
try {
color = DyeColor.valueOf(format);
} catch (Exception e1) {
item.log(Level.WARNING, "Could not read dye color from " + format);
continue;
}
DyeColor color = DyeColor.valueOf(format);
shieldPattern.add(new Pattern(color, type));
}
item.setData(ItemStat.SHIELD_PATTERN, shieldPattern);
return true;
}
@Override
@ -90,7 +70,7 @@ public class Shield_Pattern extends StringStat {
ShieldPatternData pattern = (ShieldPatternData) data;
banner.setBaseColor(pattern.getBaseColor());
banner.setPatterns(pattern.getPatterns());
meta.setBlockState(banner);
((BlockStateMeta) item.getMeta()).setBlockState(banner);
item.getMeta().addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
return true;
}
@ -245,7 +225,7 @@ public class Shield_Pattern extends StringStat {
public class ShieldPatternData extends StatData {
private DyeColor base;
private List<Pattern> patterns = new ArrayList<>();
private final List<Pattern> patterns = new ArrayList<>();
public ShieldPatternData() {
}

View File

@ -5,6 +5,7 @@ import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
@ -28,21 +29,18 @@ public class Skull_Texture extends StringStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
String value = config.getString("skull-texture.value");
if (value == null)
return true;
Validate.notNull(value, "Could not load skull texture value");
SkullTextureData skullTexture = new SkullTextureData(new GameProfile(safeParse(item, config.getString("skull-texture.uuid")), null));
skullTexture.getGameProfile().getProperties().put("textures", new Property("textures", value));
item.setData(ItemStat.SKULL_TEXTURE, skullTexture);
return true;
}
@Override
public void whenDisplayed(List<String> lore, FileConfiguration config, String id) {
}
@Override
@ -57,7 +55,7 @@ public class Skull_Texture extends StringStat {
@Override
public boolean whenApplied(MMOItemBuilder item, StatData data) {
if (item.getMaterial() != VersionMaterial.PLAYER_HEAD.toMaterial())
if (item.getItemStack().getType() != VersionMaterial.PLAYER_HEAD.toMaterial())
return true;
try {
@ -91,7 +89,7 @@ public class Skull_Texture extends StringStat {
throw new IllegalArgumentException();
return UUID.fromString(str);
} catch (IllegalArgumentException exception) {
item.log(Level.WARNING, "Warning: the skull texture UUID could not be loaded! You must re-enter the skull texture value. If you don't fix it, heads will not be able to be stacked.");
item.log(Level.WARNING, "Warning: the skull texture UUID could not be loaded! You must re-enter it otherwise heads will not be able to stack.");
return UUID.randomUUID();
}
}

View File

@ -0,0 +1,106 @@
package net.Indyuce.mmoitems.stat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bukkit.inventory.ItemStack;
import net.Indyuce.mmoitems.api.item.MMOItem;
import net.Indyuce.mmoitems.api.item.build.MMOItemBuilder;
import net.Indyuce.mmoitems.stat.data.StatData;
import net.Indyuce.mmoitems.stat.type.InternalStat;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.mmogroup.mmolib.api.item.ItemTag;
import net.mmogroup.mmolib.api.item.NBTItem;
import net.mmogroup.mmolib.version.VersionMaterial;
public class StoredTags extends InternalStat {
private static final List<String> ignoreList = Arrays.asList("Unbreakable", "BlockEntityTag", "display", "Enchantments", "HideFlags", "Damage", "AttributeModifiers", "SkullOwner", "CanDestroy", "PickupDelay", "Age");
public StoredTags() {
super(VersionMaterial.OAK_SIGN.toItem(), "Stored Tags", new String[0], "stored-tags", new String[] { "all" });
}
@Override
public boolean whenApplied(MMOItemBuilder item, StatData data) {
for (ItemTag tag : ((StoredTagsData) data).getTags())
item.addItemTag(tag);
return false;
}
@Override
public void whenLoaded(MMOItem mmoitem, NBTItem item) {
mmoitem.setData(ItemStat.STORED_TAGS, new StoredTagsData(item));
}
public class StoredTagsData extends StatData {
private final List<ItemTag> tags = new ArrayList<>();
public StoredTagsData(ItemStack stack) {
this(NBTItem.get(stack));
}
public StoredTagsData(NBTItem nbt) {
for (String tag : nbt.getTags()) {
// Any vanilla or MMOItem tag should be ignored as those are
// automatically handled
if (ignoreList.contains(tag) || tag.startsWith("MMOITEMS_"))
continue;
// As more methods are added we can add more types here
switch (getTagType(nbt.getTypeId(tag))) {
case "double":
tags.add(new ItemTag(tag, nbt.getDouble(tag)));
break;
case "int":
tags.add(new ItemTag(tag, nbt.getInteger(tag)));
break;
case "byte":
tags.add(new ItemTag(tag, nbt.getBoolean(tag)));
break;
case "string":
tags.add(new ItemTag(tag, nbt.getString(tag)));
break;
// default:
// tags.add(new ItemTag(tag, "UNSUPPORTED TAG TYPE!"));
}
}
}
public List<ItemTag> getTags() {
return tags;
}
private String getTagType(int id) {
switch (id) {
case 0:
return "end";
case 1:
return "byte";
case 2:
return "short";
case 3:
return "int";
case 4:
return "long";
case 5:
return "float";
case 6:
return "double";
case 7:
return "bytearray";
case 8:
return "string";
case 9:
return "list";
case 10:
return "compound";
case 11:
return "intarray";
default:
return "unknown";
}
}
}
}

View File

@ -34,9 +34,8 @@ public class Upgrade_Stat extends ItemStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
item.setData(this, new UpgradeData(item, config.getConfigurationSection("upgrade")));
return true;
}
@Override

View File

@ -1,5 +1,6 @@
package net.Indyuce.mmoitems.stat.data;
import org.apache.commons.lang.Validate;
import org.bukkit.Color;
import net.Indyuce.mmoitems.api.item.MMOItem;
@ -13,14 +14,11 @@ public class ColorData extends StatData {
public ColorData(MMOItem mmoitem, String string) {
setMMOItem(mmoitem);
try {
String[] split = string.split("\\ ");
Validate.isTrue(split.length > 2, "Must specify 3 numbers for red, green and blue");
red = Math.min(255, Math.max(0, Integer.parseInt(split[0])));
green = Math.min(255, Math.max(0, Integer.parseInt(split[1])));
blue = Math.min(255, Math.max(0, Integer.parseInt(split[2])));
} catch (IndexOutOfBoundsException | NumberFormatException exception) {
throwError("Could not read color from " + string);
}
}
public ColorData(Color color) {

View File

@ -24,9 +24,8 @@ public class BooleanStat extends ItemStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
item.setData(this, new BooleanData(config.getBoolean(getPath())));
return true;
}
@Override

View File

@ -28,9 +28,8 @@ public class DisableStat extends BooleanStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
item.setData(this, new BooleanData(config.getBoolean("disable-" + tag.toLowerCase().replace("_", "-"))));
return true;
}
@Override

View File

@ -33,9 +33,8 @@ public class DoubleStat extends ItemStat implements Upgradable {
super(item, name, lore, path, types, materials);
}
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
item.setData(this, new DoubleData(config.getString(getPath())));
return true;
}
public boolean whenApplied(MMOItemBuilder item, StatData data) {

View File

@ -24,9 +24,8 @@ public abstract class InternalStat extends ItemStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
// not supported
return true;
}
@Override

View File

@ -1,6 +1,5 @@
package net.Indyuce.mmoitems.stat.type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -68,6 +67,7 @@ import net.Indyuce.mmoitems.stat.Skull_Texture;
import net.Indyuce.mmoitems.stat.Soulbound;
import net.Indyuce.mmoitems.stat.Soulbound_Level;
import net.Indyuce.mmoitems.stat.Staff_Spirit;
import net.Indyuce.mmoitems.stat.StoredTags;
import net.Indyuce.mmoitems.stat.Unbreakable;
import net.Indyuce.mmoitems.stat.Upgrade_Stat;
import net.Indyuce.mmoitems.stat.Vanilla_Eating_Animation;
@ -168,6 +168,7 @@ public abstract class ItemStat {
* internal stats
*/
public static final Soulbound SOULBOUND = new Soulbound();
public static final ItemStat STORED_TAGS = new StoredTags();
private String id;
private final String name, path;
@ -192,14 +193,14 @@ public abstract class ItemStat {
this.compatibleTypes = types == null ? new String[0] : types;
this.path = path;
this.name = name;
this.compatibleMaterials = materials == null ? new ArrayList<>() : Arrays.asList(materials);
this.compatibleMaterials = Arrays.asList(materials);
}
/*
* reads stat data from a configuration section and applies it to the item
* stack after having generated the corresponding stat data class instance
*/
public abstract boolean whenLoaded(MMOItem item, ConfigurationSection config);
public abstract void whenLoaded(MMOItem item, ConfigurationSection config);
/*
* applies a stat onto an mmoitem builder instance
@ -278,28 +279,13 @@ public abstract class ItemStat {
}
public boolean hasValidMaterial(ItemStack item) {
if (compatibleMaterials.size() == 0)
return true;
for (Material dm : compatibleMaterials)
if (item.getType() == dm)
return true;
return false;
}
public void setItemType(Material material) {
item.setType(material);
return compatibleMaterials.size() == 0 || compatibleMaterials.contains(item.getType());
}
public void disable() {
enabled = false;
}
public void addCompatibleMaterial(Material... values) {
for (Material dm : values)
compatibleMaterials.add(dm);
}
public String format(double value, String... replace) {
String format = translate().replace("<plus>", value > 0 ? "+" : "");
for (int j = 0; j < replace.length; j += 2)

View File

@ -27,9 +27,8 @@ public class StringStat extends ItemStat {
}
@Override
public boolean whenLoaded(MMOItem item, ConfigurationSection config) {
public void whenLoaded(MMOItem item, ConfigurationSection config) {
item.setData(this, new StringData(config.getString(getPath())));
return true;
}
@Override

View File

@ -12,16 +12,12 @@ update-notify: true
# The plugin takes a few milliseconds to read the item data from the item
# config and a few hundred MICROseconds to actually generate it. Caching the
# item allows to skip the first step which reduces ~6 times the delay needed
# to generate any mmoitem.
#
# However, it does take up much more RAM space. Toggle it on if you havee
# too many items which makes item generation time consuming. Toggle it off
# to get some spare memory.
# to generate any mmoitem. Takes up more RAM.
use-item-caching: true
# Enable/disable the plugin iterating over the whole player inventory
# instead of just the players armor and held items.
# This option is required for the Ornamnet item type, however
# This option is required for the Ornament item type, however
# it CAN cause lag and/or take a lot of memory.
iterate-whole-inventory: false