Merge branch 'master' into 1.17
This commit is contained in:
commit
6108cdce23
17 files changed
+174
-66
No files matched your search
@@ -88,7 +88,7 @@ public class CarpetScriptHost extends ScriptHost
|
||||
|
||||
public static CarpetScriptHost create(CarpetScriptServer scriptServer, Module module, boolean perPlayer, ServerCommandSource source, Function<ServerCommandSource, Boolean> commandValidator, boolean isRuleApp)
|
||||
{
|
||||
CarpetScriptHost host = new CarpetScriptHost(scriptServer, module, perPlayer, null, Collections.emptyMap(), new HashMap<>(), commandValidator, isRuleApp);
|
||||
CarpetScriptHost host = new CarpetScriptHost(scriptServer, module, perPlayer, null, Collections.emptyMap(), new HashMap<>(), commandValidator, isRuleApp);
|
||||
// parse code and convert to expression
|
||||
if (module != null)
|
||||
{
|
||||
@@ -227,11 +227,14 @@ public class CarpetScriptHost extends ScriptHost
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void transferToChild(ScriptHost host)
|
||||
protected void setupUserHost(ScriptHost host)
|
||||
{
|
||||
super.transferToChild(host);
|
||||
super.setupUserHost(host);
|
||||
// transfer Events
|
||||
CarpetEventServer.Event.transferAllHostEventsToChild((CarpetScriptHost) host);
|
||||
CarpetScriptHost child = (CarpetScriptHost) host;
|
||||
CarpetEventServer.Event.transferAllHostEventsToChild(child);
|
||||
FunctionValue onStart = child.getFunction("__on_start");
|
||||
if (onStart != null) child.callNow(onStart, Collections.emptyList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -261,7 +264,9 @@ public class CarpetScriptHost extends ScriptHost
|
||||
{
|
||||
try
|
||||
{
|
||||
Value ret = callUDF(BlockPos.ORIGIN, scriptServer.server.getCommandSource(), getFunction("__config"), Collections.emptyList());
|
||||
FunctionValue configFunction = getFunction("__config");
|
||||
if (configFunction == null) return false;
|
||||
Value ret = callNow(configFunction, Collections.emptyList());
|
||||
if (!(ret instanceof MapValue)) return false;
|
||||
Map<Value, Value> config = ((MapValue) ret).getMap();
|
||||
setPerPlayer(config.getOrDefault(new StringValue("scope"), new StringValue("player")).getString().equalsIgnoreCase("player"));
|
||||
@@ -283,7 +288,7 @@ public class CarpetScriptHost extends ScriptHost
|
||||
}
|
||||
appConfig = config;
|
||||
}
|
||||
catch (NullPointerException | InvalidCallbackException ignored)
|
||||
catch (NullPointerException ignored)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -631,23 +636,31 @@ public class CarpetScriptHost extends ScriptHost
|
||||
return Value.NULL;
|
||||
}
|
||||
|
||||
public Value callNow(FunctionValue fun, List<Value> arguments)
|
||||
{
|
||||
ServerPlayerEntity player = (user==null)?null:scriptServer.server.getPlayerManager().getPlayer(user);
|
||||
ServerCommandSource source = (player != null)?player.getCommandSource():scriptServer.server.getCommandSource();
|
||||
try
|
||||
{
|
||||
return callUDF(BlockPos.ORIGIN, source, fun, arguments);
|
||||
}
|
||||
catch (InvalidCallbackException ignored)
|
||||
{
|
||||
return Value.NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onClose()
|
||||
{
|
||||
super.onClose();
|
||||
FunctionValue closing = getFunction("__on_close");
|
||||
if (closing != null)
|
||||
if (closing != null && (parent != null || !isPerUser()))
|
||||
// either global instance of a global task, or
|
||||
// user host in player scoped app
|
||||
{
|
||||
ServerPlayerEntity player = (user==null)?null:scriptServer.server.getPlayerManager().getPlayer(user);
|
||||
ServerCommandSource source = (player != null)?player.getCommandSource():scriptServer.server.getCommandSource();
|
||||
try
|
||||
{
|
||||
callUDF(BlockPos.ORIGIN, source, closing, Collections.emptyList());
|
||||
}
|
||||
catch (InvalidCallbackException ignored)
|
||||
{
|
||||
}
|
||||
callNow(closing, Collections.emptyList());
|
||||
}
|
||||
if (user == null)
|
||||
{
|
||||
|
||||
@@ -29,6 +29,7 @@ import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@@ -300,6 +301,12 @@ public class CarpetScriptServer
|
||||
// that will provide player hosts right at the startup
|
||||
newHost.retrieveForExecution(source, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
// global app - calling start now.
|
||||
FunctionValue onStart = newHost.getFunction("__on_start");
|
||||
if (onStart != null) newHost.callNow(onStart, Collections.emptyList());
|
||||
}
|
||||
long end = System.nanoTime();
|
||||
CarpetSettings.LOG.info("App "+name+" loaded in "+(end-start)/1000000+" ms");
|
||||
return true;
|
||||
|
||||
@@ -149,7 +149,7 @@ public abstract class Fluff
|
||||
{
|
||||
try
|
||||
{
|
||||
return (c, type_ignored) -> AbstractOperator.this.eval(v1.evalValue(c, type), v2.evalValue(c, type));
|
||||
return (c, type_ignored) -> AbstractOperator.this.eval(v1.evalValue(c, Context.NONE), v2.evalValue(c, Context.NONE));
|
||||
}
|
||||
catch (RuntimeException exc)
|
||||
{
|
||||
|
||||
@@ -322,12 +322,12 @@ public abstract class ScriptHost
|
||||
if (oldUserHost != null) return oldUserHost;
|
||||
ScriptHost userHost = this.duplicate();
|
||||
userHost.user = user;
|
||||
this.transferToChild(userHost);
|
||||
this.setupUserHost(userHost);
|
||||
userHosts.put(user, userHost);
|
||||
return userHost;
|
||||
}
|
||||
|
||||
protected void transferToChild(ScriptHost host)
|
||||
protected void setupUserHost(ScriptHost host)
|
||||
{
|
||||
// adding imports
|
||||
host.modules.putAll(this.modules);
|
||||
|
||||
@@ -100,6 +100,7 @@ public class Loops {
|
||||
expression.addLazyFunction("map", 2, (c, t, lv) ->
|
||||
{
|
||||
Value rval= lv.get(0).evalValue(c, Context.NONE);
|
||||
if (rval.isNull()) return ListValue.lazyEmpty();
|
||||
if (!(rval instanceof AbstractListValue))
|
||||
throw new InternalExpressionException("First argument of 'map' function should be a list or iterator");
|
||||
Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
|
||||
@@ -145,6 +146,7 @@ public class Loops {
|
||||
expression.addLazyFunction("filter", 2, (c, t, lv) ->
|
||||
{
|
||||
Value rval= lv.get(0).evalValue(c, Context.NONE);
|
||||
if (rval.isNull()) return ListValue.lazyEmpty();
|
||||
if (!(rval instanceof AbstractListValue))
|
||||
throw new InternalExpressionException("First argument of 'filter' function should be a list or iterator");
|
||||
Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
|
||||
@@ -192,6 +194,7 @@ public class Loops {
|
||||
{
|
||||
|
||||
Value rval= lv.get(0).evalValue(c, Context.NONE);
|
||||
if (rval.isNull()) return LazyValue.NULL;
|
||||
if (!(rval instanceof AbstractListValue))
|
||||
throw new InternalExpressionException("First argument of 'first' function should be a list or iterator");
|
||||
Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
|
||||
@@ -243,6 +246,7 @@ public class Loops {
|
||||
expression.addLazyFunction("all", 2, (c, t, lv) ->
|
||||
{
|
||||
Value rval= lv.get(0).evalValue(c, Context.NONE);
|
||||
if (rval.isNull()) return LazyValue.TRUE;
|
||||
if (!(rval instanceof AbstractListValue))
|
||||
throw new InternalExpressionException("First argument of 'all' function should be a list or iterator");
|
||||
Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
|
||||
@@ -307,6 +311,7 @@ public class Loops {
|
||||
expression.addLazyFunction("for", 2, (c, t, lv) ->
|
||||
{
|
||||
Value rval= lv.get(0).evalValue(c, Context.NONE);
|
||||
if (rval.isNull()) return LazyValue.ZERO;
|
||||
if (!(rval instanceof AbstractListValue))
|
||||
throw new InternalExpressionException("First argument of 'for' function should be a list or iterator");
|
||||
Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
|
||||
@@ -356,12 +361,13 @@ public class Loops {
|
||||
// returned value is substituted to the accumulator
|
||||
expression.addLazyFunction("reduce", 3, (c, t, lv) ->
|
||||
{
|
||||
LazyValue expr = lv.get(1);
|
||||
|
||||
Value acc = lv.get(2).evalValue(c, Context.NONE);
|
||||
Value rval= lv.get(0).evalValue(c, Context.NONE);
|
||||
if (rval.isNull()) return ListValue.lazyEmpty();
|
||||
if (!(rval instanceof AbstractListValue))
|
||||
throw new InternalExpressionException("First argument of 'reduce' should be a list or iterator");
|
||||
LazyValue expr = lv.get(1);
|
||||
Value acc = lv.get(2).evalValue(c, Context.NONE);
|
||||
Iterator<Value> iterator = ((AbstractListValue) rval).iterator();
|
||||
|
||||
if (!iterator.hasNext())
|
||||
|
||||
Reference in new issue
Block a user