8
8
import emu .grasscutter .scripts .ScriptLoader ;
9
9
import javax .script .Bindings ;
10
10
import org .luaj .vm2 .LuaFunction ;
11
+ import org .luaj .vm2 .LuaValue ;
11
12
12
13
@ AbilityAction (AbilityModifierAction .Type .ServerLuaCall )
13
14
public final class ActionServerLuaCall extends AbilityActionHandler {
@@ -16,12 +17,14 @@ public boolean execute(
16
17
Ability ability , AbilityModifierAction action , ByteString abilityData , GameEntity target ) {
17
18
var scene = target .getScene ();
18
19
var scriptManager = scene .getScriptManager ();
20
+
19
21
var functionName = action .funcName ;
20
22
21
23
// Set the script library's manager.
22
24
var scriptLib = ScriptLoader .getScriptLib ();
23
25
scriptLib .setCurrentEntity (target );
24
26
scriptLib .setSceneScriptManager (scriptManager );
27
+
25
28
// Attempt to call the function.
26
29
return switch (action .luaCallType ) {
27
30
default -> false ;
@@ -33,7 +36,7 @@ public boolean execute(
33
36
// Set the script library's group.
34
37
scriptLib .setCurrentGroup (group );
35
38
36
- yield ActionServerLuaCall .callFunction (script , functionName );
39
+ yield ActionServerLuaCall .callFunction (script , functionName , ability , action );
37
40
}
38
41
case SpecificGroup -> {
39
42
var groupId = action .callParamList [0 ];
@@ -43,7 +46,16 @@ public boolean execute(
43
46
// Set the script library's group.
44
47
scriptLib .setCurrentGroup (group );
45
48
46
- yield ActionServerLuaCall .callFunction (script , functionName );
49
+ yield ActionServerLuaCall .callFunction (script , functionName , ability , action );
50
+ }
51
+ case Gadget -> {
52
+ var controller = target .getEntityController ();
53
+ if (controller == null || functionName .isBlank ()) yield false ;
54
+
55
+ // Hand off the function handling to the controller.
56
+ controller .callControllerScriptFunc (target , functionName , ability , action );
57
+
58
+ yield true ;
47
59
}
48
60
};
49
61
}
@@ -53,17 +65,32 @@ public boolean execute(
53
65
*
54
66
* @param bindings The bindings to fetch the function from.
55
67
* @param functionName The name of the function to call.
68
+ * @param ability The ability data.
69
+ * @param action The ability action data.
56
70
* @return Whether the function was called successfully.
57
71
*/
58
- private static boolean callFunction (Bindings bindings , String functionName ) {
72
+ private static boolean callFunction (
73
+ Bindings bindings , String functionName ,
74
+ Ability ability , AbilityModifierAction action
75
+ ) {
59
76
try {
60
77
// Resolve the function from the script.
61
78
var function = bindings .get (functionName );
62
79
if (!(function instanceof LuaFunction luaFunction ))
63
80
throw new Exception ("Function is not a LuaFunction." );
64
81
65
- // Attempt to invoke the function.
66
- luaFunction .call (ScriptLoader .getScriptLibLua ());
82
+ // Convert parameters to Lua values.
83
+ var lParam1 = LuaValue .valueOf (action .param1 .getInt (ability ));
84
+ var lParam2 = LuaValue .valueOf (action .param2 .getInt (ability ));
85
+ var lParam3 = LuaValue .valueOf (action .param3 .getInt (ability ));
86
+
87
+ // Invoke the function with the parameters.
88
+ switch (action .paramNum ) {
89
+ case 1 -> luaFunction .invoke (new LuaValue [] { lParam1 });
90
+ case 2 -> luaFunction .invoke (new LuaValue [] { lParam1 , lParam2 });
91
+ case 3 -> luaFunction .invoke (new LuaValue [] { lParam1 , lParam2 , lParam3 });
92
+ default -> luaFunction .invoke (new LuaValue [] { ScriptLoader .getScriptLibLua () });
93
+ }
67
94
68
95
return true ;
69
96
} catch (Exception exception ) {
0 commit comments