custom args WIP

This commit is contained in:
gnembon committed 2020-11-02 01:32:04 -04:00
1 parent a0fc6b3af1
commit 1d81768c54
4 files changed
+178 -37

No files matched your search

@@ -40,6 +40,8 @@ public class CarpetScriptHost extends ScriptHost
private int saveTimeout;
public boolean persistenceRequired;
public Map<Value, Value> appConfig = Collections.emptyMap();
private CarpetScriptHost(CarpetScriptServer server, Module code, boolean perUser, ScriptHost parent)
{
super(code, perUser, parent);
@@ -128,6 +130,7 @@ public class CarpetScriptHost extends ScriptHost
Map<Value, Value> config = ((MapValue) ret).getMap();
setPerPlayer(config.getOrDefault(new StringValue("scope"), new StringValue("player")).getString().equalsIgnoreCase("player"));
persistenceRequired = config.getOrDefault(new StringValue("stay_loaded"), Value.FALSE).getBoolean();
this.appConfig = config;
}
catch (NullPointerException | InvalidCallbackException ignored)
{
@@ -185,11 +188,11 @@ public class CarpetScriptHost extends ScriptHost
return host;
}
public Value handleCommand(ServerCommandSource source, String call, List<Integer> coords, String arg)
public Value handleCommandLegacy(ServerCommandSource source, String call, List<Integer> coords, String arg)
{
try
{
return call(source, call, coords, arg);
return callLegacy(source, call, coords, arg);
}
catch (CarpetExpressionException exc)
{
@@ -203,7 +206,25 @@ public class CarpetScriptHost extends ScriptHost
}
}
public Value call(ServerCommandSource source, String call, List<Integer> coords, String arg)
public Value handleCommand(ServerCommandSource source, String call, List<Value> args)
{
try
{
return call(source, call, args);
}
catch (CarpetExpressionException exc)
{
handleErrorWithStack("Error while running custom command", exc);
return Value.NULL;
}
catch (ArithmeticException ae)
{
handleErrorWithStack("Math doesn't compute", ae);
return Value.NULL;
}
}
public Value callLegacy(ServerCommandSource source, String call, List<Integer> coords, String arg)
{
if (CarpetServer.scriptServer.stopAll)
throw new CarpetExpressionException("SCARPET PAUSED", null);
@@ -305,6 +326,42 @@ public class CarpetScriptHost extends ScriptHost
}
}
public Value call(ServerCommandSource source, String call, List<Value> suppliedArgs)
{
if (CarpetServer.scriptServer.stopAll)
throw new CarpetExpressionException("SCARPET PAUSED", null);
FunctionValue function = getFunction(call);
if (function == null)
throw new CarpetExpressionException("UNDEFINED", null);
List<LazyValue> argv = FunctionValue.lazify(suppliedArgs);
List<String> args = function.getArguments();
if (argv.size() != args.size())
{
String error = "Fail: stored function "+call+" takes "+args.size()+" arguments, not "+argv.size()+ ":\n";
for (int i = 0; i < max(argv.size(), args.size()); i++)
{
error += (i<args.size()?args.get(i):"??")+" => "+(i<argv.size()?argv.get(i).evalValue(null).getString():"??")+"\n";
}
throw new CarpetExpressionException(error, null);
}
try
{
// TODO: this is just for now - invoke would be able to invoke other hosts scripts
Context context = new CarpetContext(this, source, BlockPos.ORIGIN);
return function.getExpression().evalValue(
() -> function.lazyEval(context, Context.VOID, function.getExpression(), function.getToken(), argv),
context,
Context.VOID
);
}
catch (ExpressionException e)
{
throw new CarpetExpressionException(e.getMessage(), e.stack);
}
}
public Value callUDF(BlockPos pos, ServerCommandSource source, FunctionValue fun, List<Value> argv) throws InvalidCallbackException
{
if (CarpetServer.scriptServer.stopAll)