Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Fixed support for new parser in EvalFunction and changed the version
Browse files Browse the repository at this point in the history
  • Loading branch information
LeandroPA committed Feb 24, 2017
1 parent 338ac9a commit cf052e7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions TuSKe/src/me/tuke/sktuke/effects/EffEvaluateFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public boolean init(Expression<?>[] arg, int arg1, Kleenean arg2, ParseResult ar
func = (Expression<String>) arg[0];
if (arg3.regexes.size() > 0)
exprs = arg3.regexes.get(0).group(0);
EvalFunction.setParserInstance(this);
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions TuSKe/src/me/tuke/sktuke/gui/EffFormatGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 37 additions & 2 deletions TuSKe/src/me/tuke/sktuke/util/EvalFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -33,7 +36,7 @@ public class EvalFunction {
Class<?> parserInstanceClass = ReflectionUtils.getClass("ch.njol.skript.lang.parser.ParserInstance");
if (parserInstanceClass != null){
newParser = (Constructor<SkriptParser>) ReflectionUtils.getConstructor(SkriptParser.class, parserInstanceClass, String.class, int.class, ParseContext.class);
newParserInstance = ReflectionUtils.getField(parserInstanceClass, null, "DUMMY");
//newParserInstance = ReflectionUtils.getField(parserInstanceClass, null, "DUMMY");
}
}

Expand Down Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -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);

}
Expand Down

0 comments on commit cf052e7

Please sign in to comment.