mirror of
https://github.com/Melledy/Grasscutter.git
synced 2024-11-22 07:37:43 +00:00
Implement ServerLuaCall
This commit is contained in:
parent
6a421336df
commit
12ac5b32d3
@ -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,
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user