Merge branch 'master' into 1.15

This commit is contained in:
gnembon committed 2020-01-27 22:48:56 -04:00
commit a50b00059e
6 files changed
+88 -25

No files matched your search

@@ -32,6 +32,7 @@ import static java.lang.Math.max;
public class CarpetScriptHost extends ScriptHost
{
private final CarpetScriptServer scriptServer;
ServerCommandSource responsibleSource;
private Tag globalState;
private int saveTimeout;
@@ -66,8 +67,7 @@ public class CarpetScriptHost extends ScriptHost
}
catch (CarpetExpressionException e)
{
e.printStack(source);
Messenger.m(source, "r Exception while evaluating expression at " + new BlockPos(source.getPosition()) + ": " + e.getMessage());
host.handleErrorWithStack("Error while evaluating expression", e);
host.resetErrorSnooper();
return null;
}
@@ -129,16 +129,33 @@ public class CarpetScriptHost extends ScriptHost
public CarpetScriptHost retrieveForExecution(ServerCommandSource source)
{
if (!perUser)
return this;
CarpetScriptHost host = this;
if (perUser)
{
try
{
ServerPlayerEntity player = source.getPlayer();
host = (CarpetScriptHost) retrieveForExecution(player.getName().getString());
}
catch (CommandSyntaxException e)
{
host = (CarpetScriptHost) retrieveForExecution((String) null);
}
}
if (host.errorSnooper == null) host.setChatErrorSnooper(source);
return host;
}
public String handleCommand(ServerCommandSource source, String call, List<Integer> coords, String arg)
{
try
{
ServerPlayerEntity player = source.getPlayer();
return (CarpetScriptHost) retrieveForExecution(player.getName().getString());
return call(source, call, coords, arg);
}
catch (CommandSyntaxException e)
catch (CarpetExpressionException exc)
{
return (CarpetScriptHost) retrieveForExecution((String)null);
handleErrorWithStack("Error while running custom command", exc);
return "";
}
}
@@ -236,7 +253,7 @@ public class CarpetScriptHost extends ScriptHost
}
catch (ExpressionException e)
{
return e.getMessage();
throw new CarpetExpressionException(e.getMessage(), e.stack);
}
}
@@ -259,7 +276,7 @@ public class CarpetScriptHost extends ScriptHost
}
catch (ExpressionException e)
{
CarpetSettings.LOG.error("Callback failed: "+e.getMessage());
handleExpressionException("Callback failed", e);
}
return Value.NULL;
}
@@ -361,6 +378,7 @@ public class CarpetScriptHost extends ScriptHost
public void setChatErrorSnooper(ServerCommandSource source)
{
responsibleSource = source;
errorSnooper = (expr, /*Nullable*/ token, message) ->
{
try
@@ -412,4 +430,31 @@ public class CarpetScriptHost extends ScriptHost
return new ArrayList<>();
};
}
@Override
public void resetErrorSnooper()
{
responsibleSource = null;
super.resetErrorSnooper();
}
public void handleErrorWithStack(String intro, CarpetExpressionException exception)
{
if (responsibleSource != null)
{
exception.printStack(responsibleSource);
String message = exception.getMessage();
Messenger.m(responsibleSource, "r "+intro+(message.isEmpty()?"":": "+message));
}
else
{
CarpetSettings.LOG.error(intro+": "+exception.getMessage());
}
}
@Override
public void handleExpressionException(String message, ExpressionException exc)
{
handleErrorWithStack(message, new CarpetExpressionException(exc.getMessage(), exc.stack));
}
}
@@ -220,7 +220,7 @@ public class CarpetScriptServer
executes( (c) ->
{
String response = modules.get(hostName).retrieveForExecution(c.getSource()).
call(c.getSource(),"__command", null, "");
handleCommand(c.getSource(),"__command", null, "");
if (!response.isEmpty()) Messenger.m(c.getSource(), "gi "+response);
return 1;
});
@@ -232,14 +232,14 @@ public class CarpetScriptServer
requires((player) -> modules.containsKey(hostName) && modules.get(hostName).getFunction(function) != null).
executes( (c) -> {
String response = modules.get(hostName).retrieveForExecution(c.getSource()).
call(c.getSource(), function,null,"");
handleCommand(c.getSource(), function,null,"");
if (!response.isEmpty()) Messenger.m(c.getSource(),"gi "+response);
return 1;
}).
then(argument("args...", StringArgumentType.greedyString()).
executes( (c) -> {
String response = modules.get(hostName).retrieveForExecution(c.getSource()).
call(c.getSource(), function,null, StringArgumentType.getString(c, "args..."));
handleCommand(c.getSource(), function,null, StringArgumentType.getString(c, "args..."));
if (!response.isEmpty()) Messenger.m(c.getSource(), "gi "+response);
return 1;
})));
@@ -1,6 +1,7 @@
package carpet.script;
import carpet.script.bundled.Module;
import carpet.script.exception.ExpressionException;
import carpet.script.exception.InternalExpressionException;
import carpet.script.value.FunctionValue;
import carpet.script.value.Value;
@@ -313,6 +314,12 @@ public abstract class ScriptHost
return userHost;
}
public void handleExpressionException(String msg, ExpressionException exc)
{
System.out.println(msg+": "+exc);
}
protected abstract ScriptHost duplicate();
public void onClose() { }
@@ -68,7 +68,7 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
return (name.equals("_")?"<lambda>":name) +"("+String.join(", ",stringArgs)+")";
}
public String fullName() {return expression.module == null?name:name+"["+expression.module.getName()+"]";}
public String fullName() {return (name.equals("_")?"<lambda>":name)+(expression.module == null?"":"["+expression.module.getName()+"]");}
@Override
public boolean getBoolean()
@@ -4,6 +4,7 @@ import carpet.script.Context;
import carpet.script.Expression;
import carpet.script.LazyValue;
import carpet.script.Tokenizer;
import carpet.script.exception.ExpressionException;
import carpet.script.exception.InternalExpressionException;
import java.util.HashMap;
@@ -29,7 +30,18 @@ public class ThreadValue extends Value
{
id = sequence++;
taskFuture = CompletableFuture.supplyAsync(
() -> function.lazyEval(ctx, Context.NONE, expr, token, args).evalValue(ctx),
() ->
{
try
{
return function.lazyEval(ctx, Context.NONE, expr, token, args).evalValue(ctx);
}
catch (ExpressionException exc)
{
ctx.host.handleExpressionException("Thread failed", exc);
return Value.NULL;
}
},
executorServices.computeIfAbsent(pool, (v) -> (ThreadPoolExecutor)Executors.newCachedThreadPool())
);
Thread.yield();