-
-
Notifications
You must be signed in to change notification settings - Fork 381
/
Copy pathEffCallFunction.java
144 lines (128 loc) · 5.1 KB
/
EffCallFunction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.effects;
import ch.njol.skript.Skript;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionList;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.function.Function;
import ch.njol.skript.lang.function.Functions;
import ch.njol.util.Kleenean;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.skriptlang.skript.lang.script.Script;
public class EffCallFunction extends Effect {
@Nullable
public static Object[] lastReturnValue;
static {
Skript.registerEffect(EffCallFunction.class,
"(call|execute|run) [the] function[s] %strings% [(using|with) [the] argument[s] %-objects%]",
"(call|execute|run) [the] global function[s] %strings% [(using|with) [the] argument[s] %-objects%]",
"(call|execute|run) [the] local function[s] %strings% (in|from) [script] %-string% [(using|with) [the] argument[s] %-objects%]"
);
}
@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<String> functionNames;
@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<String> functionScript;
@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<Object> functionArguments;
private Script containingScript;
private Kleenean requireLocalFunction;
@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
containingScript = getParser().getCurrentScript();
functionNames = (Expression<String>) exprs[0];
if (matchedPattern == 0) {
requireLocalFunction = Kleenean.UNKNOWN;
functionArguments = (Expression<Object>) exprs[1];
} else if (matchedPattern == 1) {
requireLocalFunction = Kleenean.FALSE;
functionArguments = (Expression<Object>) exprs[1];
} else if (matchedPattern == 2) {
requireLocalFunction = Kleenean.TRUE;
functionArguments = (Expression<Object>) exprs[2];
}
return true;
}
@Override
protected void execute(Event event) {
lastReturnValue = null;
String parentScript = getParentScript(event);
Object[][] arguments = createArgumentArray(functionArguments, event);
for (String functionName : functionNames.getArray(event)) {
Function<?> functionToExecute = getFunction(functionName, parentScript);
if (functionToExecute != null) {
lastReturnValue = functionToExecute.execute(arguments);
functionToExecute.resetReturnValue();
}
}
}
@Override
public String toString(@Nullable Event event, boolean debug) {
String stringRepresentation = "";
switch (requireLocalFunction) {
case UNKNOWN:
stringRepresentation = "call function " + functionNames.toString(event, debug);
break;
case TRUE:
stringRepresentation = "call local function " + functionNames.toString(event, debug) + " from script " + functionScript.toString(event, debug);
break;
case FALSE:
stringRepresentation = "call global function " + functionNames.toString(event, debug);
break;
}
if (functionArguments != null)
stringRepresentation += " with the arguments " + functionArguments.toString(event, debug);
return stringRepresentation;
}
private Object[][] createArgumentArray(Expression<Object> argumentsExpression, Event event) {
if (argumentsExpression instanceof ExpressionList) {
Expression<?>[] argumentExpressions = ((ExpressionList<?>) argumentsExpression).getExpressions();
Object[][] argumentArray = new Object[argumentExpressions.length][];
for (int i = 0; i < argumentExpressions.length; i++) {
argumentArray[i] = argumentExpressions[i].getArray(event);
}
return argumentArray;
} else if (argumentsExpression != null) {
return new Object[][] {argumentsExpression.getArray(event)};
}
return new Object[][] {};
}
@Nullable
private Function<?> getFunction(String functionName, @Nullable String parentScript) {
if (requireLocalFunction == Kleenean.TRUE) {
return Functions.getLocalFunction(functionName, parentScript);
} else if (requireLocalFunction == Kleenean.FALSE) {
return Functions.getGlobalFunction(functionName);
}
return Functions.getFunction(functionName, parentScript);
}
@Nullable
private String getParentScript(Event event) {
if (functionScript != null)
return functionScript.getSingle(event);
if (containingScript != null)
return containingScript.getConfig().getFileName();
return null;
}
}