package de.slikey.effectlib.math;

import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;
import net.objecthunter.exp4j.function.Function;
import org.bukkit.configuration.ConfigurationSection;

import java.util.Random;
import java.util.logging.Level;

public class EquationTransform implements Transform {
    private Expression expression;
    private static Function randFunction;

    @Override
    public void load(ConfigurationSection parameters) {
        setEquation(parameters.getString("equation", ""));
    }

    public EquationTransform() {

    }

    public EquationTransform(String equation) {
        setEquation(equation);
    }

    public void setEquation(String equation) {
        try {
            if (randFunction == null) {
                randFunction = new Function("rand", 2) {
                    private Random random = new Random();
                    
                    @Override
                    public double apply(double... args) {
                        return random.nextDouble() * (args[1] - args[0]) + args[0];
                    }
                };
            }
            expression = new ExpressionBuilder(equation)
                .function(randFunction)
                .variables("t")
                .build();
        } catch (Exception ex) {
            expression = null;
            org.bukkit.Bukkit.getLogger().log(Level.WARNING, "Error parsing equation " + equation, ex);
        }
    }

    @Override
    public double get(double t) {
        if (expression == null) {
            return 0;
        }
        expression.setVariable("t", t);
        return expression.evaluate();
    }
}

@@ -411,16 +411,56 @@
 unction;
+%0A    private final String inputVariable;
 %0A%0A    @O
@@ -622,16 +622,44 @@
 orm() %7B%0A
+        inputVariable = %22t%22;
 %0A    %7D%0A%0A
@@ -698,32 +698,200 @@
 ing equation) %7B%0A
+        this();%0A        setEquation(equation);%0A    %7D%0A%0A    public EquationTransform(String equation, String inputVariable) %7B%0A        this.inputVariable = inputVariable;%0A
         setEquat
@@ -1500,19 +1500,29 @@
 riables(
-%22t%22
+inputVariable
 )%0A      
@@ -1871,19 +1871,29 @@
 ariable(
-%22t%22
+inputVariable
 , t);%0A  

Allow EquationTransform with a custom input variable name