diff --git a/src/main/java/net/Indyuce/mmocore/api/skill/Skill.java b/src/main/java/net/Indyuce/mmocore/api/skill/Skill.java index 377e60c2..796386ed 100644 --- a/src/main/java/net/Indyuce/mmocore/api/skill/Skill.java +++ b/src/main/java/net/Indyuce/mmocore/api/skill/Skill.java @@ -1,225 +1,229 @@ package net.Indyuce.mmocore.api.skill; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.Set; - +import net.Indyuce.mmocore.api.player.PlayerData; +import net.Indyuce.mmocore.api.util.MMOCoreUtils; +import net.Indyuce.mmocore.api.util.math.formula.IntegerLinearValue; +import net.Indyuce.mmocore.api.util.math.formula.LinearValue; import org.apache.commons.lang.Validate; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.inventory.ItemStack; -import net.Indyuce.mmocore.api.player.PlayerData; -import net.Indyuce.mmocore.api.util.MMOCoreUtils; -import net.Indyuce.mmocore.api.util.math.formula.IntegerLinearValue; -import net.Indyuce.mmocore.api.util.math.formula.LinearValue; +import java.util.*; public abstract class Skill { - private final String id; + private final String id; - private String name; - private ItemStack icon = new ItemStack(Material.BOOK); - private List lore; - private boolean passive; + private String name; + private ItemStack icon = new ItemStack(Material.BOOK); + private List lore; + private boolean passive; - private final Map modifiers = new HashMap<>(); - private final Skill skill = this; + private final Map modifiers = new HashMap<>(); + private final Skill skill = this; - protected static final Random random = new Random(); + protected static final Random random = new Random(); - public Skill() { - id = getClass().getSimpleName().toUpperCase(); - name = getClass().getSimpleName().replace("_", " "); + public Skill() { + id = getClass().getSimpleName().toUpperCase(); + name = getClass().getSimpleName().replace("_", " "); - Validate.notNull(id, "ID cannot be null"); - Validate.notNull(id, "Name cannot be null"); - } + Validate.notNull(id, "ID cannot be null"); + Validate.notNull(id, "Name cannot be null"); - public Skill(String id) { - this.id = id.toUpperCase().replace(" ", "_").replace("-", "_"); - } + // Default modifiers + addModifierIfNone("mana", LinearValue.ZERO); + addModifierIfNone("stamina", LinearValue.ZERO); + } - public void setName(String name) { - this.name = name; - } + public Skill(String id) { + this.id = id.toUpperCase().replace(" ", "_").replace("-", "_"); + } - public void setMaterial(Material material) { - setIcon(new ItemStack(material)); - } + public void setName(String name) { + this.name = name; + } - public void setIcon(ItemStack item) { - this.icon = item; - } + public void setMaterial(Material material) { + setIcon(new ItemStack(material)); + } - public void setLore(String... lore) { - setLore(Arrays.asList(lore)); - } + public void setIcon(ItemStack item) { + this.icon = item; + } - public void setLore(List lore) { - this.lore = lore; - } + public void setLore(String... lore) { + setLore(Arrays.asList(lore)); + } - public void setPassive() { - passive = true; - } + public void setLore(List lore) { + this.lore = lore; + } - public String getName() { - return name; - } + public void setPassive() { + passive = true; + } - public String getId() { - return id; - } + public String getName() { + return name; + } - public String getLowerCaseId() { - return id.replace("_", "-").toLowerCase(); - } + public String getId() { + return id; + } - public List getLore() { - return lore; - } + public String getLowerCaseId() { + return id.replace("_", "-").toLowerCase(); + } - public ItemStack getIcon() { - return icon.clone(); - } + public List getLore() { + return lore; + } - /* - * passive skills do not display any message - */ - public boolean isPassive() { - return passive; - } + public ItemStack getIcon() { + return icon.clone(); + } - public boolean hasModifier(String modifier) { - return modifiers.containsKey(modifier); - } + /** + * Passive skills do not display any message when trying to cast them + * + * @return If the skill is passive + */ + public boolean isPassive() { + return passive; + } - public void addModifier(String modifier, LinearValue linear) { - modifiers.put(modifier, linear); - } + public boolean hasModifier(String modifier) { + return modifiers.containsKey(modifier); + } - public LinearValue getModifierInfo(String modifier) { - return modifiers.get(modifier); - } + public void addModifier(String modifier, LinearValue linear) { + modifiers.put(modifier, linear); + } - public double getModifier(String modifier, int level) { - return modifiers.get(modifier).calculate(level); - } + public void addModifierIfNone(String mod, LinearValue defaultValue) { + if (!hasModifier(mod)) + addModifier(mod, defaultValue); + } - public Set getModifiers() { - return modifiers.keySet(); - } + public LinearValue getModifierInfo(String modifier) { + return modifiers.get(modifier); + } - public void update(FileConfiguration config) { - name = config.getString("name", "INVALID NAME"); - lore = config.getStringList("lore"); - icon = MMOCoreUtils.readIcon(config.getString("material", "DIRT")); + public double getModifier(String modifier, int level) { + return modifiers.get(modifier).calculate(level); + } - for (String modifier : modifiers.keySet()) - if (config.contains(modifier)) - modifiers.put(modifier, readLinearValue(modifiers.get(modifier), config.getConfigurationSection(modifier))); - } + public Set getModifiers() { + return modifiers.keySet(); + } - private LinearValue readLinearValue(LinearValue current, ConfigurationSection config) { - return current instanceof IntegerLinearValue ? new IntegerLinearValue(config) : new LinearValue(config); - } + public void update(FileConfiguration config) { + name = config.getString("name", "INVALID NAME"); + lore = config.getStringList("lore"); + icon = MMOCoreUtils.readIcon(config.getString("material", "DIRT")); - /* - * not overriden for passive skills therefore not abstract. - */ - public SkillResult whenCast(PlayerData data, SkillInfo skill) { - return new SkillResult(data, skill); - } + for (String modifier : modifiers.keySet()) + if (config.contains(modifier)) + modifiers.put(modifier, readLinearValue(modifiers.get(modifier), config.getConfigurationSection(modifier))); + } - public SkillInfo newSkillInfo(ConfigurationSection config) { - return new SkillInfo(config); - } + private LinearValue readLinearValue(LinearValue current, ConfigurationSection config) { + return current instanceof IntegerLinearValue ? new IntegerLinearValue(config) : new LinearValue(config); + } - public SkillInfo newSkillInfo(int level) { - return new SkillInfo(level); - } + /* + * not overriden for passive skills therefore not abstract. + */ + public SkillResult whenCast(PlayerData data, SkillInfo skill) { + return new SkillResult(data, skill); + } - public class SkillInfo { - private final int level, max; - private final Map modifiers = new HashMap<>(skill.modifiers); + public SkillInfo newSkillInfo(ConfigurationSection config) { + return new SkillInfo(config); + } - /* - * class used to save information about skills IN A CLASS CONTEXT i.e at - * which level the skill can be unlocked, etc. - */ - public SkillInfo(int level) { - this.level = level; - this.max = 0; - } + public SkillInfo newSkillInfo(int level) { + return new SkillInfo(level); + } - public SkillInfo(ConfigurationSection config) { - level = config.getInt("level"); - max = config.getInt("max-level"); + public class SkillInfo { + private final int level, max; + private final Map modifiers = new HashMap<>(skill.modifiers); - for (String key : config.getKeys(false)) - if (config.get(key) instanceof ConfigurationSection && modifiers.containsKey(key)) - modifiers.put(key, readLinearValue(modifiers.get(key), config.getConfigurationSection(key))); - } + /* + * class used to save information about skills IN A CLASS CONTEXT i.e at + * which level the skill can be unlocked, etc. + */ + public SkillInfo(int level) { + this.level = level; + this.max = 0; + } - public Skill getSkill() { - return skill; - } + public SkillInfo(ConfigurationSection config) { + level = config.getInt("level"); + max = config.getInt("max-level"); - public int getUnlockLevel() { - return level; - } + for (String key : config.getKeys(false)) + if (config.get(key) instanceof ConfigurationSection && modifiers.containsKey(key)) + modifiers.put(key, readLinearValue(modifiers.get(key), config.getConfigurationSection(key))); + } - public boolean hasMaxLevel() { - return max > 0; - } + public Skill getSkill() { + return skill; + } - public int getMaxLevel() { - return max; - } + public int getUnlockLevel() { + return level; + } - /* - * this method can only OVERRIDE default modifiers - */ - public void addModifier(String modifier, LinearValue linear) { - if (modifiers.containsKey(modifier)) - modifiers.put(modifier, linear); - } + public boolean hasMaxLevel() { + return max > 0; + } - public double getModifier(String modifier, int level) { - return modifiers.get(modifier).calculate(level); - } + public int getMaxLevel() { + return max; + } - public List calculateLore(PlayerData data) { - return calculateLore(data, data.getSkillLevel(skill)); - } + /* + * this method can only OVERRIDE default modifiers + */ + public void addModifier(String modifier, LinearValue linear) { + if (modifiers.containsKey(modifier)) + modifiers.put(modifier, linear); + } - public List calculateLore(PlayerData data, int x) { - List list = new ArrayList<>(); + public double getModifier(String modifier, int level) { + return modifiers.get(modifier).calculate(level); + } - Map placeholders = calculateModifiers(x); - placeholders.put("mana_name", data.getProfess().getManaDisplay().getName()); - lore.forEach(str -> list.add(applyPlaceholders(placeholders, str))); + public List calculateLore(PlayerData data) { + return calculateLore(data, data.getSkillLevel(skill)); + } - return list; - } + public List calculateLore(PlayerData data, int x) { + List list = new ArrayList<>(); - private String applyPlaceholders(Map placeholders, String str) { - while (str.contains("{") && str.substring(str.indexOf("{")).contains("}")) { - String holder = str.substring(str.indexOf("{") + 1, str.indexOf("}")); - str = str.replace("{" + holder + "}", placeholders.getOrDefault(holder, "PHE")); - } - return str; - } + Map placeholders = calculateModifiers(x); + placeholders.put("mana_name", data.getProfess().getManaDisplay().getName()); + lore.forEach(str -> list.add(applyPlaceholders(placeholders, str))); - private Map calculateModifiers(int x) { - Map map = new HashMap<>(); - modifiers.keySet().forEach(modifier -> map.put(modifier, modifiers.get(modifier).getDisplay(x))); - return map; - } - } + return list; + } + + private String applyPlaceholders(Map placeholders, String str) { + while (str.contains("{") && str.substring(str.indexOf("{")).contains("}")) { + String holder = str.substring(str.indexOf("{") + 1, str.indexOf("}")); + str = str.replace("{" + holder + "}", placeholders.getOrDefault(holder, "PHE")); + } + return str; + } + + private Map calculateModifiers(int x) { + Map map = new HashMap<>(); + modifiers.keySet().forEach(modifier -> map.put(modifier, modifiers.get(modifier).getDisplay(x))); + return map; + } + } } diff --git a/src/main/java/net/Indyuce/mmocore/api/util/math/formula/LinearValue.java b/src/main/java/net/Indyuce/mmocore/api/util/math/formula/LinearValue.java index 57292b2c..5bd0d5ef 100644 --- a/src/main/java/net/Indyuce/mmocore/api/util/math/formula/LinearValue.java +++ b/src/main/java/net/Indyuce/mmocore/api/util/math/formula/LinearValue.java @@ -4,86 +4,110 @@ import io.lumine.mythic.lib.MythicLib; import org.bukkit.configuration.ConfigurationSection; public class LinearValue { - private final double base, perLevel, min, max; - private final boolean hasmin, hasmax; + private final double base, perLevel, min, max; + private final boolean hasmin, hasmax; + public static final LinearValue ZERO = new LinearValue(0, 0, 0, 0); - /* - * a number which depends on the player level. it can be used as a skill - * modifier to make the ability better depending on the player level or as - * an attrribute value to make attributes increase with the player level - */ - public LinearValue(double base, double perLevel) { - this.base = base; - this.perLevel = perLevel; - hasmin = false; - hasmax = false; - min = 0; - max = 0; - } + /** + * A number formula which depends on the player level. It can be used + * to handle skill modifiers so that the ability gets better with the + * skill level, or as an attribute value to make them scale with the class level. + * + * @param base Base value + * @param perLevel Every level, final value is increased by X + */ + public LinearValue(double base, double perLevel) { + this.base = base; + this.perLevel = perLevel; + hasmin = false; + hasmax = false; + min = 0; + max = 0; + } - public LinearValue(double base, double perLevel, double min, double max) { - this.base = base; - this.perLevel = perLevel; - hasmin = true; - hasmax = true; - this.min = min; - this.max = max; - } + /** + * A number formula which depends on the player level. It can be used + * to handle skill modifiers so that the ability gets better with the + * skill level, or as an attribute value to make them scale with the class level. + * + * @param base Base value + * @param perLevel Every level, final value is increased by X + * @param min Minimum final value + * @param max Maximum final value + */ + public LinearValue(double base, double perLevel, double min, double max) { + this.base = base; + this.perLevel = perLevel; + hasmin = true; + hasmax = true; + this.min = min; + this.max = max; + } - public LinearValue(LinearValue value) { - base = value.base; - perLevel = value.perLevel; - hasmin = value.hasmin; - hasmax = value.hasmax; - min = value.min; - max = value.max; - } + /** + * Copies a linear formula + * + * @param value Formula to copy + */ + public LinearValue(LinearValue value) { + base = value.base; + perLevel = value.perLevel; + hasmin = value.hasmin; + hasmax = value.hasmax; + min = value.min; + max = value.max; + } - public LinearValue(ConfigurationSection config) { - base = config.getDouble("base"); - perLevel = config.getDouble("per-level"); - hasmin = config.contains("min"); - hasmax = config.contains("max"); - min = hasmin ? config.getDouble("min") : 0; - max = hasmax ? config.getDouble("max") : 0; - } + /** + * Loads a linear formula from a config section + * + * @param config Config to load the formula from + */ + public LinearValue(ConfigurationSection config) { + base = config.getDouble("base"); + perLevel = config.getDouble("per-level"); + hasmin = config.contains("min"); + hasmax = config.contains("max"); + min = hasmin ? config.getDouble("min") : 0; + max = hasmax ? config.getDouble("max") : 0; + } - public double getBaseValue() { - return base; - } + public double getBaseValue() { + return base; + } - public double getPerLevel() { - return perLevel; - } + public double getPerLevel() { + return perLevel; + } - public double getMax() { - return max; - } + public double getMax() { + return max; + } - public double getMin() { - return min; - } + public double getMin() { + return min; + } - public boolean hasMax() { - return hasmax; - } + public boolean hasMax() { + return hasmax; + } - public boolean hasMin() { - return hasmin; - } + public boolean hasMin() { + return hasmin; + } - public String getDisplay(int level) { - return MythicLib.plugin.getMMOConfig().decimals.format(calculate(level)); - } + public String getDisplay(int level) { + return MythicLib.plugin.getMMOConfig().decimals.format(calculate(level)); + } - public double calculate(int level) { - double value = base + perLevel * (level - 1); + public double calculate(int level) { + double value = base + perLevel * (level - 1); - if (hasmin) value = Math.max(min, value); + if (hasmin) value = Math.max(min, value); - if (hasmax) value = Math.min(max, value); + if (hasmax) value = Math.min(max, value); - return value; - } + return value; + } }