From cf052e77302108267dda4b0540151f9d2159d5f0 Mon Sep 17 00:00:00 2001 From: Tuke-Nuke Date: Fri, 24 Feb 2017 16:06:28 -0300 Subject: [PATCH] Fixed support for new parser in EvalFunction and changed the version --- .../sktuke/effects/EffEvaluateFunction.java | 1 + .../expressions/ExprEvaluateFunction.java | 1 + .../src/me/tuke/sktuke/gui/EffFormatGUI.java | 2 + .../src/me/tuke/sktuke/util/EvalFunction.java | 39 ++++++++++++++++++- 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/TuSKe/src/me/tuke/sktuke/effects/EffEvaluateFunction.java b/TuSKe/src/me/tuke/sktuke/effects/EffEvaluateFunction.java index d847170..298c784 100644 --- a/TuSKe/src/me/tuke/sktuke/effects/EffEvaluateFunction.java +++ b/TuSKe/src/me/tuke/sktuke/effects/EffEvaluateFunction.java @@ -36,6 +36,7 @@ protected void execute(Event e) { for (int x = 0; x < funcs.length; x++) if (funcs[x] != null){ Expression[] params = null; + EvalFunction.setParserInstance(this); EvalFunction func; if (params == null){ func = new EvalFunction(funcs[x], exprs); diff --git a/TuSKe/src/me/tuke/sktuke/expressions/ExprEvaluateFunction.java b/TuSKe/src/me/tuke/sktuke/expressions/ExprEvaluateFunction.java index c792961..bf91fd9 100644 --- a/TuSKe/src/me/tuke/sktuke/expressions/ExprEvaluateFunction.java +++ b/TuSKe/src/me/tuke/sktuke/expressions/ExprEvaluateFunction.java @@ -32,6 +32,7 @@ public boolean init(Expression[] arg, int arg1, Kleenean arg2, ParseResult ar func = (Expression) arg[0]; if (arg3.regexes.size() > 0) exprs = arg3.regexes.get(0).group(0); + EvalFunction.setParserInstance(this); return true; } diff --git a/TuSKe/src/me/tuke/sktuke/gui/EffFormatGUI.java b/TuSKe/src/me/tuke/sktuke/gui/EffFormatGUI.java index 1eb0ff7..e3a4b14 100644 --- a/TuSKe/src/me/tuke/sktuke/gui/EffFormatGUI.java +++ b/TuSKe/src/me/tuke/sktuke/gui/EffFormatGUI.java @@ -62,10 +62,12 @@ public boolean init(Expression[] arg, int arg1, Kleenean arg2, ParseResult ar String name = arg3.regexes.get(0).group(0).replaceAll(" ",""); String exprs = arg3.regexes.size() > 1 ? arg3.regexes.get(1).group(0) : ""; Function f = Functions.getFunction(name); + EvalFunction.setParserInstance(this); if (f != null) func = new EvalFunction(f, exprs); else func = new EvalFunction(name, exprs); + } if (arg1 > 1 && arg1 != 4){ ct = arg[max - 2] != null ? arg[max - 2].getConvertedExpression(Object.class): null; diff --git a/TuSKe/src/me/tuke/sktuke/util/EvalFunction.java b/TuSKe/src/me/tuke/sktuke/util/EvalFunction.java index 58a0771..2b37c60 100644 --- a/TuSKe/src/me/tuke/sktuke/util/EvalFunction.java +++ b/TuSKe/src/me/tuke/sktuke/util/EvalFunction.java @@ -9,13 +9,16 @@ import ch.njol.skript.lang.ExpressionList; import ch.njol.skript.lang.ParseContext; import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.Statement; import ch.njol.skript.lang.function.Function; import ch.njol.skript.lang.function.Functions; import ch.njol.skript.lang.function.Parameter; +import ch.njol.skript.lang.util.SimpleExpression; /** * A util class to evaluate functions. It's useful when you want to get a function ready, * and run the function in another event but using values of the caller event. + * It requires {@link ReflectionUtils} * * @author Tuke_Nuke * @@ -33,7 +36,7 @@ public class EvalFunction { Class parserInstanceClass = ReflectionUtils.getClass("ch.njol.skript.lang.parser.ParserInstance"); if (parserInstanceClass != null){ newParser = (Constructor) ReflectionUtils.getConstructor(SkriptParser.class, parserInstanceClass, String.class, int.class, ParseContext.class); - newParserInstance = ReflectionUtils.getField(parserInstanceClass, null, "DUMMY"); + //newParserInstance = ReflectionUtils.getField(parserInstanceClass, null, "DUMMY"); } } @@ -89,6 +92,7 @@ public EvalFunction getParemetersValues(Event e){ return this; } + /** * Run the function. You need to {@link #getParemetersValues(Event)} before running this. * @return The return value of function, null if a void function. @@ -99,10 +103,38 @@ public Object[] run(){ return null; } + /** + * Returns the parameters of a function. + * @return Array of Expression + */ public Expression[] getParameters(){ return parameters; } + /** + * It gets a ParserInstance from a effect/condition/expression. + * It is necessary in case it is running the new parser and it can't + * parse some objects with dummy ParserInstance (e.g loop-value). + * + * It won't throw any exeception in case it isn't running the new parser. + * @param from - An instance of calling effect/condition/expression. + */ + public static void setParserInstance(Statement from){ + if (newParserInstance == null) + newParserInstance = ReflectionUtils.getField(Statement.class, from, "pi"); + } + /** + * It gets a ParserInstance from a effect/condition/expression. + * It is necessary in case it is running the new parser and it can't + * parse some objects with dummy ParserInstance (e.g loop-value). + * + * It won't throw any exeception in case it isn't running the new parser. + * @param from - An instance of calling effect/condition/expression.{@} + */ + public static void setParserInstance(SimpleExpression from){ + if (newParserInstance == null) + newParserInstance = ReflectionUtils.getField(SimpleExpression.class, from, "pi"); + } private void parseParemeters(String expr){ SkriptParser parser = getSkriptParser(expr); ReflectionUtils.setField(SkriptParser.class, parser, "suppressMissingAndOrWarnings", true); @@ -118,8 +150,11 @@ private void parseParemeters(String expr){ } private SkriptParser getSkriptParser(String expr){ - if (newParser != null) + if (newParser != null){ + if (newParserInstance == null) + newParserInstance = ReflectionUtils.newInstance(ReflectionUtils.getClass("ch.njol.skript.lang.parser.ParserInstance")); return ReflectionUtils.newInstance(newParser, newParserInstance, expr, SkriptParser.ALL_FLAGS, ParseContext.DEFAULT); + } return new SkriptParser(expr, SkriptParser.ALL_FLAGS, ParseContext.DEFAULT); }