Carpet

Carpet

2M Downloads

/script run & invoke returns incorrect "success count"

sodiboo opened this issue ยท 1 comments

commented

Description

The /script run and /script invoke commands will always return 1, even if that script snippet shows a syntax error when ran by a player

Here's a video showing this issue

Steps to reproduce and expected/actual result

  • /script run print(1+1
    • shows an error indicating the command was not successful
  • /script run run('script run print(1+1')
    • runs the command above (which doesn't compile) from a scarpet script
    • does not print anything (because it never ran)
    • does not give any indication of any kind of error to the script

Expected result: 0, to indicate the command was unsuccessful (success rate 0)
Actual result: 1, which usually means the command was successful (success rate 1)

Code Analysis

It seems the /script run command will always return 1 no matter what,

private static int compute(CommandContext<ServerCommandSource> context, String expr)
{
ServerCommandSource source = context.getSource();
CarpetScriptHost host = getHost(context);
handleCall(source, host, () -> {
CarpetExpression ex = new CarpetExpression(host.main, expr, source, new BlockPos(0, 0, 0));
return ex.scriptRunCommand(host, new BlockPos(source.getPosition()));
});
return 1;
}

i suggest to make the handleCall function return a bool as to whether the expression actually ran

public static void handleCall(ServerCommandSource source, CarpetScriptHost host, Supplier<String> call)
{
try
{
host.setChatErrorSnooper(source);
long start = System.nanoTime();

or if there was a CarpetExpressionException it would return false

Messenger.m(source, "wi = ", "wb "+result, "gi ("+time+metric+")");
}
catch (CarpetExpressionException e)
{
host.handleErrorWithStack("Error while evaluating expression", e);
}

and then change the /script run command to return that bool, as an int (see first code snippet)

Same thing goes for /script invoke

private static int invoke(CommandContext<ServerCommandSource> context, String call, BlockPos pos1, BlockPos pos2, String args)
{
ServerCommandSource source = context.getSource();
CarpetScriptHost host = getHost(context);
//if (!(args.trim().isEmpty()))
// arguments.addAll(Arrays.asList(args.trim().split("\\s+")));
handleCall(source, host, () -> host.call(source, call, positions, args));
return 1;
}

commented

Then why not propose a fix? Ig just adding a return 0 after:

catch (CarpetExpressionException e) 
 { 
     host.handleErrorWithStack("Error while evaluating expression", e); 
 } 

shoud work?