Implement ServerLuaCall

This commit is contained in:
KingRainbow44 2023-08-13 19:12:39 -04:00
parent 6a421336df
commit 12ac5b32d3
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
2 changed files with 82 additions and 1 deletions

View File

@ -3,9 +3,10 @@ package emu.grasscutter.data.binout;
import com.google.gson.annotations.SerializedName;
import emu.grasscutter.data.common.DynamicFloat;
import emu.grasscutter.game.props.ElementType;
import java.io.Serializable;
import lombok.ToString;
import java.io.Serializable;
public class AbilityModifier implements Serializable {
private static final long serialVersionUID = -2001232313615923575L;
@ -340,6 +341,20 @@ public class AbilityModifier implements Serializable {
public int param2;
public int param3;
public LuaCallType luaCallType;
@SerializedName("CallParamList")
public int[] callParamList;
public String funcName;
public enum LuaCallType {
FromGroup,
CurGalleryControlGroup,
CurChallengeGroup,
SpecificGroup,
AbilityGroupSourceGroup,
CurScenePlay
}
public enum DropType {
LevelControl,
BigWorldOnly,

View File

@ -0,0 +1,66 @@
package emu.grasscutter.game.ability.actions;
import com.google.protobuf.ByteString;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.binout.AbilityModifier.AbilityModifierAction;
import emu.grasscutter.game.ability.Ability;
import emu.grasscutter.game.entity.GameEntity;
import org.luaj.vm2.LuaFunction;
import javax.script.Bindings;
@AbilityAction(AbilityModifierAction.Type.ServerLuaCall)
public final class ActionServerLuaCall extends AbilityActionHandler {
@Override
public boolean execute(
Ability ability, AbilityModifierAction action,
ByteString abilityData, GameEntity target
) {
var scene = target.getScene();
var scriptManager = scene.getScriptManager();
var functionName = action.funcName;
return switch (action.luaCallType) {
default -> false;
case FromGroup -> {
var groupId = target.getGroupId();
var group = scriptManager.getGroupById(groupId);
var script = group.getBindings();
yield ActionServerLuaCall.callFunction(script, functionName);
}
case SpecificGroup -> {
var groupId = action.callParamList[0];
var group = scriptManager.getGroupById(groupId);
var script = group.getBindings();
yield ActionServerLuaCall.callFunction(script, functionName);
}
};
}
/**
* Handles fetching and calling a function.
*
* @param bindings The bindings to fetch the function from.
* @param functionName The name of the function to call.
* @return Whether the function was called successfully.
*/
private static boolean callFunction(Bindings bindings, String functionName) {
try {
// Resolve the function from the script.
var function = bindings.get(functionName);
if (!(function instanceof LuaFunction luaFunction))
throw new Exception("Function is not a LuaFunction.");
// Attempt to invoke the function.
luaFunction.call();
return true;
} catch (Exception exception) {
Grasscutter.getLogger().warn("Unable to invoke {}.",
functionName, exception);
return false;
}
}
}