fixed MATH%...% issue

This commit is contained in:
Indyuce 2022-01-09 00:26:00 +01:00
parent b5b3d8d430
commit e2a667ab78
17 changed files with 23 additions and 196 deletions

View File

@ -24,7 +24,7 @@ public class BackwardsCompatibleAbility extends Ability<BackwardsCompatibleAbili
@Nullable
@Override
public BackwardsCompatibleAbilityMetadata canBeCast(AttackMetadata attack, LivingEntity target, AbilityData ability) {
SkillMetadata skillMeta = new SkillMetadata(ability, attack, new VariableList(VariableScope.SKILL), attack, attack.getPlayer().getLocation(), null, target);
SkillMetadata skillMeta = new SkillMetadata(ability, attack, new VariableList(VariableScope.SKILL), attack, attack.getPlayer().getLocation(), null, target, null);
return new BackwardsCompatibleAbilityMetadata(skillMeta, registeredSkill.getHandler().getResult(skillMeta), ability);
}

View File

@ -2,18 +2,11 @@ package net.Indyuce.mmoitems.api.item.build;
import com.google.common.collect.Lists;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.util.ui.FriendlyFeedbackProvider;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.util.message.FFPMMOItems;
import io.lumine.mythic.lib.api.math.EvaluatedFormula;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.text.DecimalFormat;
import java.util.*;
import java.util.logging.Level;
/**
* There are three types of lore placeholders.
@ -132,8 +125,6 @@ public class LoreBuilder {
* have been inserted in the lore. It cleans all unused static placeholders
* as well as lore bars. The dynamic placeholders still remain however.
*/
DecimalFormat df = new DecimalFormat("#.####");
public List<String> build() {
/*
@ -157,18 +148,17 @@ public class LoreBuilder {
}
/*
*
* Allows math to be done within the stats.yml file
*
* */
int index = 0;
* Allows math to be done within the stats.yml file
*/
int index = -1;
for (String string : lore) {
if (string.contains("MATH%")) {
String result = String.valueOf(df.format(eval(StringUtils.substringBetween(string, "%", "%"))));
lore.set(index, string.replaceAll("MATH\\%[^%]*\\%", result));
}
index++;
String match = StringUtils.substringBetween(string, "%", "%");
if (match == null)
continue;
String result = MythicLib.plugin.getMMOConfig().decimals.format(new EvaluatedFormula(match).evaluate());
lore.set(index, string.replaceAll("MATH\\%[^%]*\\%", result));
}
/*
@ -207,91 +197,4 @@ public class LoreBuilder {
private boolean isBar(String str) {
return str.startsWith("{bar}") || str.startsWith("{sbar}");
}
/*
*
* Math methods
*
* */
public static double eval(final String str) {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() {
nextChar();
double x = parseExpression();
if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char) ch);
return x;
}
// Grammar:
// expression = term | expression `+` term | expression `-` term
// term = factor | term `*` factor | term `/` factor
// factor = `+` factor | `-` factor | `(` expression `)`
// | number | functionName factor | factor `^` factor
double parseExpression() {
double x = parseTerm();
for (; ; ) {
if (eat('+')) x += parseTerm(); // addition
else if (eat('-')) x -= parseTerm(); // subtraction
else return x;
}
}
double parseTerm() {
double x = parseFactor();
for (; ; ) {
if (eat('*')) x *= parseFactor(); // multiplication
else if (eat('/')) x /= parseFactor(); // division
else return x;
}
}
double parseFactor() {
if (eat('+')) return parseFactor(); // unary plus
if (eat('-')) return -parseFactor(); // unary minus
double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = Double.parseDouble(str.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') { // functions
while (ch >= 'a' && ch <= 'z') nextChar();
String func = str.substring(startPos, this.pos);
x = parseFactor();
if (func.equals("sqrt")) x = Math.sqrt(x);
else if (func.equals("sin")) x = Math.sin(Math.toRadians(x));
else if (func.equals("cos")) x = Math.cos(Math.toRadians(x));
else if (func.equals("tan")) x = Math.tan(Math.toRadians(x));
else throw new RuntimeException("Unknown function: " + func);
} else {
throw new RuntimeException("Unexpected: " + (char) ch);
}
if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation
return x;
}
}.parse();
}
}

View File

@ -1,11 +0,0 @@
package net.Indyuce.mmoitems.api.util;
import java.text.DecimalFormat;
public class StatFormat extends DecimalFormat {
private static final long serialVersionUID = -2880611307522719877L;
public StatFormat(String format) {
super("0." + format);
}
}

View File

@ -1,14 +1,8 @@
package net.Indyuce.mmoitems.stat;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.util.StatFormat;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.type.AttributeStat;
import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.version.VersionMaterial;
import org.bukkit.attribute.Attribute;
import org.jetbrains.annotations.NotNull;
public class Armor extends AttributeStat {
public Armor() {

View File

@ -1,11 +1,6 @@
package net.Indyuce.mmoitems.stat;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.util.StatFormat;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.type.AttributeStat;
import io.lumine.mythic.lib.api.item.ItemTag;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;

View File

@ -1,11 +1,6 @@
package net.Indyuce.mmoitems.stat;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.util.StatFormat;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.type.AttributeStat;
import io.lumine.mythic.lib.api.item.ItemTag;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;

View File

@ -1,11 +1,6 @@
package net.Indyuce.mmoitems.stat;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.util.StatFormat;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.type.AttributeStat;
import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.version.VersionMaterial;
import org.bukkit.attribute.Attribute;

View File

@ -10,10 +10,8 @@ import net.Indyuce.mmoitems.api.Element;
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.api.util.StatFormat;
import net.Indyuce.mmoitems.gui.edition.EditionInventory;
import net.Indyuce.mmoitems.gui.edition.ElementsEdition;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.data.ElementListData;
import net.Indyuce.mmoitems.stat.data.random.RandomElementListData;
import net.Indyuce.mmoitems.stat.data.random.RandomStatData;

View File

@ -4,7 +4,6 @@ import io.lumine.mythic.lib.api.item.SupportedNBTTagValues;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.item.mmoitem.ReadMMOItem;
import net.Indyuce.mmoitems.api.util.StatFormat;
import net.Indyuce.mmoitems.gui.edition.EditionInventory;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.data.random.RandomStatData;

View File

@ -3,12 +3,7 @@ package net.Indyuce.mmoitems.stat;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.util.StatFormat;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.type.AttributeStat;
import io.lumine.mythic.lib.api.item.ItemTag;
public class KnockbackResistance extends AttributeStat {
public KnockbackResistance() {

View File

@ -3,12 +3,7 @@ package net.Indyuce.mmoitems.stat;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.util.StatFormat;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.type.AttributeStat;
import io.lumine.mythic.lib.api.item.ItemTag;
public class MaxHealth extends AttributeStat {
public MaxHealth() {

View File

@ -3,12 +3,7 @@ package net.Indyuce.mmoitems.stat;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.util.StatFormat;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.type.AttributeStat;
import io.lumine.mythic.lib.api.item.ItemTag;
public class MovementSpeed extends AttributeStat {
public MovementSpeed() {

View File

@ -1,8 +1,8 @@
package net.Indyuce.mmoitems.stat;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.util.ui.SilentNumbers;
import net.Indyuce.mmoitems.api.util.NumericStatFormula;
import net.Indyuce.mmoitems.api.util.StatFormat;
import net.Indyuce.mmoitems.stat.data.random.RandomStatData;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
@ -57,8 +57,8 @@ public class PickaxePower extends DoubleStat {
if (techMinimum != 0 || techMaximum != 0) {
String builtRange;
if (SilentNumbers.round(techMinimum, 2) == SilentNumbers.round(techMaximum, 2)) { builtRange = new StatFormat("##").format(techMinimum); }
else { builtRange = new StatFormat("##").format(techMinimum) + "-" + new StatFormat("##").format(techMaximum); }
if (SilentNumbers.round(techMinimum, 2) == SilentNumbers.round(techMaximum, 2)) { builtRange = MythicLib.plugin.getMMOConfig().decimals.format(techMinimum); }
else { builtRange = MythicLib.plugin.getMMOConfig().decimals.format(techMinimum) + "-" + MythicLib.plugin.getMMOConfig().decimals.format(techMaximum); }
// Just display normally
item.getLore().insert("pickaxe-power", formatNumericStat(techMinimum, "#", builtRange));

View File

@ -3,7 +3,6 @@ package net.Indyuce.mmoitems.stat;
import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.api.item.NBTItem;
import io.lumine.mythic.lib.api.item.SupportedNBTTagValues;
import io.lumine.mythic.lib.api.util.ui.PlusMinusPercent;
import io.lumine.mythic.lib.api.util.ui.SilentNumbers;
import io.lumine.mythic.lib.version.VersionMaterial;
import net.Indyuce.mmoitems.ItemStats;
@ -11,14 +10,12 @@ import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.item.mmoitem.ReadMMOItem;
import net.Indyuce.mmoitems.api.player.RPGPlayer;
import net.Indyuce.mmoitems.api.util.NumericStatFormula;
import net.Indyuce.mmoitems.api.util.StatFormat;
import net.Indyuce.mmoitems.api.util.message.Message;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.data.RequiredLevelData;
import net.Indyuce.mmoitems.stat.data.random.RandomRequiredLevelData;
import net.Indyuce.mmoitems.stat.data.random.RandomStatData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.data.type.UpgradeInfo;
import net.Indyuce.mmoitems.stat.type.DoubleStat;
import net.Indyuce.mmoitems.stat.type.ItemRestriction;
import org.apache.commons.lang.Validate;

View File

@ -1,15 +1,11 @@
package net.Indyuce.mmoitems.stat.block;
import io.lumine.mythic.lib.api.util.ui.SilentNumbers;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.util.NumericStatFormula;
import net.Indyuce.mmoitems.api.util.StatFormat;
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 io.lumine.mythic.lib.api.item.ItemTag;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.jetbrains.annotations.NotNull;

View File

@ -1,12 +1,7 @@
package net.Indyuce.mmoitems.stat.type;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.util.StatFormat;
import net.Indyuce.mmoitems.stat.data.DoubleData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.jetbrains.annotations.NotNull;
/**
* Attribute stats are also collected when registered in the StatManager because

View File

@ -15,7 +15,6 @@ import net.Indyuce.mmoitems.api.edition.StatEdition;
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.api.util.StatFormat;
import net.Indyuce.mmoitems.api.util.message.FFPMMOItems;
import net.Indyuce.mmoitems.gui.edition.EditionInventory;
import net.Indyuce.mmoitems.stat.data.DoubleData;
@ -108,26 +107,13 @@ public class DoubleStat extends ItemStat implements Upgradable, Previewable {
// Calculate Difference
upgradeShift = value - uData.getValue(); } }
// Display if not ZERO
// Display in lore
if (value != 0 || upgradeShift != 0) {
// Get path and modify
String pathFormat = MMOItems.plugin.getLanguage().getStatFormat(getPath());
// Displaying upgrades?
if (upgradeShift != 0) {
item.getLore().insert(getPath(), formatPath(MMOItems.plugin.getLanguage().getStatFormat(getPath()), moreIsBetter(), value * multiplyWhenDisplaying())
// Add upgrade format
+ MythicLib.plugin.parseColors(UpgradeTemplate.getUpgradeChangeSuffix(plus(upgradeShift * multiplyWhenDisplaying()) + (new StatFormat("##").format(upgradeShift * multiplyWhenDisplaying())), !isGood(upgradeShift * multiplyWhenDisplaying()))));
} else {
// Just display normally
item.getLore().insert(getPath(), formatPath(MMOItems.plugin.getLanguage().getStatFormat(getPath()), moreIsBetter(), value * multiplyWhenDisplaying()));
} }
String loreInsert = formatPath(MMOItems.plugin.getLanguage().getStatFormat(getPath()), moreIsBetter(), value * multiplyWhenDisplaying());
if (upgradeShift != 0)
loreInsert += MythicLib.plugin.parseColors(UpgradeTemplate.getUpgradeChangeSuffix(plus(upgradeShift * multiplyWhenDisplaying()) + (MythicLib.plugin.getMMOConfig().decimals.format(upgradeShift * multiplyWhenDisplaying())), !isGood(upgradeShift * multiplyWhenDisplaying())));
item.getLore().insert(getPath(), loreInsert);
}
/*
* Add NBT Data if it is not equal to ZERO, in which case it will just get removed.
@ -144,10 +130,10 @@ public class DoubleStat extends ItemStat implements Upgradable, Previewable {
return format
// Replace conditional pluses with +value
.replace("<plus>#",getColorPrefix(value < 0 && moreIsBetter) + (value > 0 ? "+" : "") + new StatFormat("##").format(value))
.replace("<plus>#",getColorPrefix(value < 0 && moreIsBetter) + (value > 0 ? "+" : "") + MythicLib.plugin.getMMOConfig().decimals.format(value))
// Replace loose pounds with the value
.replace("#",getColorPrefix(value < 0 && moreIsBetter) + new StatFormat("##").format(value))
.replace("#",getColorPrefix(value < 0 && moreIsBetter) + MythicLib.plugin.getMMOConfig().decimals.format(value))
// Replace loose <plus>es
.replace("<plus>", (value > 0 ? "+" : ""));
@ -163,9 +149,9 @@ public class DoubleStat extends ItemStat implements Upgradable, Previewable {
// Replace loose pounds with the value
.replace("#", getColorPrefix(min < 0 && moreIsBetter) +
(min > 0 ? "+" : "") + new StatFormat("##").format(min)
(min > 0 ? "+" : "") + MythicLib.plugin.getMMOConfig().decimals.format(min)
+ MMOItems.plugin.getConfig().getString("stats-displaying.range-dash", "") + getColorPrefix(max < 0 && moreIsBetter) +
(min < 0 && max > 0 ? "+" : "") + new StatFormat("##").format(max)); }
(min < 0 && max > 0 ? "+" : "") + MythicLib.plugin.getMMOConfig().decimals.format(max)); }
@Override
public void whenPreviewed(@NotNull ItemStackBuilder item, @NotNull StatData currentData, @NotNull RandomStatData templateData) throws IllegalArgumentException {