- Added MATH(equation) to the stats.yml file.

I got this idea because of the Movement Speed system. The default Minecraft Movement Speed is 0.1, but IMO having 0.1 appear on Items, doesn't seem like a significant increase, even though you're actually doubling the Player's speed.

So now you can add to your stats.yml `movement-speed: 'Speed: +MATH(#*10)%'`
Now if I set the movement speed stat on an item to 0.1, the item will display in its lore "Speed: +100%"

This is purely "cosmetic" meaning this will only affect how the specific Item Stat is displayed in the Lore, not the stat itself!
This commit is contained in:
Dancull47 2021-10-06 13:02:34 -04:00
parent 97e50b5dc6
commit ad2536a9d2

View File

@ -2,9 +2,17 @@ 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 org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.*;
import java.util.logging.Level;
/**
* There are three types of lore placeholders.
@ -145,6 +153,29 @@ public class LoreBuilder {
j++;
}
/*
*
* Allows math to be done within the stats.yml file
*
* */
int index = 0;
for (String string : lore) {
if (string.contains("MATH(")) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
String result = "MATH ERROR";
try {
result = String.valueOf(engine.eval(StringUtils.substringBetween(string, "(", ")")));
} catch (ScriptException e) {
MMOItems.plugin.getLogger().log(Level.WARNING, FriendlyFeedbackProvider.quickForConsole(FFPMMOItems.get(),
"An Item Stat which uses MATH is incorrectly setup. Check your stats.yml file and verify any MATH(X) usage is a valid equation!"));
}
lore.set(index, string.replaceAll("MATH\\([^)]*\\)", result));
}
index++;
}
/*
* Clear bar codes and parse chat colors only ONCE the bars have been
* successfully calculated. Also breaks lines containing \n (like breaks)