diff --git a/src/main/java/carpet/commands/ScriptCommand.java b/src/main/java/carpet/commands/ScriptCommand.java index 6dce1037..80531c5e 100644 --- a/src/main/java/carpet/commands/ScriptCommand.java +++ b/src/main/java/carpet/commands/ScriptCommand.java @@ -239,6 +239,7 @@ public class ScriptCommand then(argument("call", StringArgumentType.word()). suggests( (cc, bb) -> suggestMatching(suggestFunctionCalls(cc), bb)). executes( (cc) -> CarpetServer.scriptServer.events.addEvent( + cc.getSource(), StringArgumentType.getString(cc, "event"), null, StringArgumentType.getString(cc, "call") @@ -249,6 +250,7 @@ public class ScriptCommand then(argument("call", StringArgumentType.word()). suggests( (cc, bb) -> suggestMatching(suggestFunctionCalls(cc), bb)). executes( (cc) -> CarpetServer.scriptServer.events.addEvent( + cc.getSource(), StringArgumentType.getString(cc, "event"), StringArgumentType.getString(cc, "app"), StringArgumentType.getString(cc, "call") @@ -259,6 +261,7 @@ public class ScriptCommand then(argument("call", StringArgumentType.greedyString()). suggests( (cc, bb) -> suggestMatching(CarpetEventServer.Event.byName.get(StringArgumentType.getString(cc, "event")).handler.callList.stream().map(CarpetEventServer.Callback::toString), bb)). executes( (cc) -> CarpetServer.scriptServer.events.removeEvent( + cc.getSource(), StringArgumentType.getString(cc, "event"), StringArgumentType.getString(cc, "call") )?1:0)))); @@ -308,7 +311,7 @@ public class ScriptCommand Messenger.m(source, "w Handlers for "+event.name+": "); shownEvent = true; } - Messenger.m(source, "w - "+c.function +(c.host==null?"":" (from "+c.host+")")); + Messenger.m(source, "w - "+c.function.getString() +(c.host==null?"":" (from "+c.host+")")); } } return 1; diff --git a/src/main/java/carpet/script/CarpetEventServer.java b/src/main/java/carpet/script/CarpetEventServer.java index 1c08bfc8..b56be2b4 100644 --- a/src/main/java/carpet/script/CarpetEventServer.java +++ b/src/main/java/carpet/script/CarpetEventServer.java @@ -1,6 +1,7 @@ package carpet.script; import carpet.CarpetServer; +import carpet.script.exception.InternalExpressionException; import carpet.script.value.BlockValue; import carpet.script.value.EntityValue; import carpet.script.value.FunctionValue; @@ -9,6 +10,7 @@ import carpet.script.value.NBTSerializableValue; import carpet.script.value.NumericValue; import carpet.script.value.StringValue; import carpet.script.value.Value; +import carpet.utils.Messenger; import net.minecraft.block.BlockState; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.entity.Entity; @@ -126,27 +128,30 @@ public class CarpetEventServer callList.removeIf(call -> !CarpetServer.scriptServer.runas(source, call.host, call.function, argv)); // this actually does the calls } } - public boolean addEventCall(String hostName, String funName, Function verifier) + public boolean addEventCall(ServerCommandSource source, String hostName, String funName, Function verifier) { ScriptHost host = CarpetServer.scriptServer.getHostByName(hostName); if (host == null) { // impossible call to add + Messenger.m(source, "r Unknown app "+hostName); return false; } if (!verifier.apply(host)) { - // host cannot use that event, its global event on player based app + Messenger.m(source, "r Global event can only be added to apps with global scope"); return false; } FunctionValue udf = host.getFunction(funName); if (udf == null || udf.getArguments().size() != reqArgs) { // call won't match arguments + Messenger.m(source, "r Callback doesn't expect required number of arguments: "+reqArgs); return false; } //all clear //remove duplicates + removeEventCall(hostName, udf.getString()); callList.add(new Callback(hostName, udf)); return true; @@ -567,38 +572,44 @@ public class CarpetEventServer scheduledCalls.add(new ScheduledCall(context, function, args, due)); } - public boolean addEvent(String event, String host, String funName) + public boolean addEvent(ServerCommandSource source, String event, String host, String funName) { if (!Event.byName.containsKey(event)) { return false; } Event ev = Event.byName.get(event); - return ev.handler.addEventCall(host, funName, h -> canAddEvent(ev, h)); + boolean added = ev.handler.addEventCall(source, host, funName, h -> canAddEvent(ev, h)); + if (added) Messenger.m(source, "gi Added " + funName + " to " + event); + return added; } public boolean addEventDirectly(String event, ScriptHost host, FunctionValue function) { Event ev = Event.byName.get(event); - // config can be analyzed after code is read, so this will always be player scope - //if (!canAddEvent(ev, host)) - // throw new InternalExpressionException("Global event "+event+" can only be added to apps with global scope"); + if (!canAddEvent(ev, host)) + throw new InternalExpressionException("Global event "+event+" can only be added to apps with global scope"); return ev.handler.addEventCallDirect(host, function); } private boolean canAddEvent(Event event, ScriptHost host) { - return !(event.globalOnly && host.perUser); + return !(event.globalOnly && (host.perUser || host.parent != null)); } - public boolean removeEvent(String event, String funName) + public boolean removeEvent(ServerCommandSource source, String event, String funName) { if (!Event.byName.containsKey(event)) + { + Messenger.m(source, "r Unknown event: " + event); return false; + } Pair call = decodeCallback(funName); Event.byName.get(event).handler.removeEventCall(call.getLeft(), call.getRight()); + // could verified if actually removed + Messenger.m(source, "gi Removed event: " + funName + " from "+event); return true; } diff --git a/src/main/java/carpet/script/CarpetScriptHost.java b/src/main/java/carpet/script/CarpetScriptHost.java index 354dba7d..7d27ba4e 100644 --- a/src/main/java/carpet/script/CarpetScriptHost.java +++ b/src/main/java/carpet/script/CarpetScriptHost.java @@ -7,6 +7,7 @@ import carpet.script.exception.ExpressionException; import carpet.script.exception.InternalExpressionException; import carpet.script.exception.InvalidCallbackException; import carpet.script.value.FunctionValue; +import carpet.script.value.MapValue; import carpet.script.value.NumericValue; import carpet.script.value.StringValue; import carpet.script.value.Value; @@ -26,6 +27,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Locale; +import java.util.Map; import static java.lang.Math.max; @@ -36,14 +38,23 @@ public class CarpetScriptHost extends ScriptHost private Tag globalState; private int saveTimeout; + public boolean persistenceRequired; private CarpetScriptHost(CarpetScriptServer server, Module code, boolean perUser, ScriptHost parent) { super(code, perUser, parent); this.saveTimeout = 0; this.scriptServer = server; - if (parent == null && code != null) + persistenceRequired = true; + if (parent == null && code != null) // app, not a global host + { + persistenceRequired = false; globalState = loadState(); + } + else if (parent != null) + { + persistenceRequired = ((CarpetScriptHost)parent).persistenceRequired; + } } public static CarpetScriptHost create(CarpetScriptServer scriptServer, Module module, boolean perPlayer, ServerCommandSource source) @@ -82,16 +93,39 @@ public class CarpetScriptHost extends ScriptHost } @Override - public void addUserDefinedFunction(Module module, String funName, FunctionValue function) + public void addUserDefinedFunction(Context ctx, Module module, String funName, FunctionValue function) { - super.addUserDefinedFunction(module, funName, function); + super.addUserDefinedFunction(ctx, module, funName, function); // mcarpet - if (funName.startsWith("__on_")) // here we can make a determination if we want to only accept events from main module. + if (funName.startsWith("__")) // potential fishy activity + { + if (funName.startsWith("__on_")) // here we can make a determination if we want to only accept events from main module. + { + // this is nasty, we have the host and function, yet we add it via names, but hey - works for now + String event = funName.replaceFirst("__on_", ""); + if (CarpetEventServer.Event.byName.containsKey(event)) + scriptServer.events.addEventDirectly(event, this, function); + } + else if (funName.equals("__config")) + { + addAppConfig(ctx, function); + } + } + } + + private void addAppConfig(Context ctx, FunctionValue function) + { + try + { + CarpetContext cctx = (CarpetContext)ctx; + Value ret = callUDF(BlockPos.ORIGIN, cctx.s, function, Collections.emptyList()); + if (!(ret instanceof MapValue)) return; + Map 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(); + } + catch (NullPointerException | InvalidCallbackException ignored) { - // this is nasty, we have the host and function, yet we add it via names, but hey - works for now - String event = funName.replaceFirst("__on_",""); - if (CarpetEventServer.Event.byName.containsKey(event)) - scriptServer.events.addEventDirectly(event, this, function); } } diff --git a/src/main/java/carpet/script/CarpetScriptServer.java b/src/main/java/carpet/script/CarpetScriptServer.java index 02761df8..aa5b0983 100644 --- a/src/main/java/carpet/script/CarpetScriptServer.java +++ b/src/main/java/carpet/script/CarpetScriptServer.java @@ -164,7 +164,7 @@ public class CarpetScriptServer modules.put(name, newHost); - if (!addConfig(source, name) && autoload) + if (autoload && !newHost.persistenceRequired) { removeScriptHost(source, name); return false; @@ -174,28 +174,6 @@ public class CarpetScriptServer return true; } - - private boolean addConfig(ServerCommandSource source, String hostName) - { - CarpetScriptHost host = modules.get(hostName); - if (host == null || host.getFunction("__config") == null) - { - return false; - } - try - { - Value ret = host.callUDF(BlockPos.ORIGIN, source, host.getFunction("__config"), Collections.emptyList()); - if (!(ret instanceof MapValue)) return false; - Map config = ((MapValue) ret).getMap(); - host.setPerPlayer(config.getOrDefault(new StringValue("scope"), new StringValue("player")).getString().equalsIgnoreCase("player")); - return config.getOrDefault(new StringValue("stay_loaded"), Value.FALSE).getBoolean(); - } - catch (NullPointerException | InvalidCallbackException ignored) - { - } - return false; - } - private void addCommand(ServerCommandSource source, String hostName) { ScriptHost host = modules.get(hostName); diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index acef00cd..13765769 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -680,7 +680,7 @@ public class Expression FunctionValue result = new FunctionValue(expr, token, name, code, arguments, contextValues); // do not store lambda definitions - if (!name.equals("_")) context.host.addUserDefinedFunction(module, name, result); + if (!name.equals("_")) context.host.addUserDefinedFunction(context, module, name, result); return result; } diff --git a/src/main/java/carpet/script/ScriptHost.java b/src/main/java/carpet/script/ScriptHost.java index 37f0de3f..e2dac757 100644 --- a/src/main/java/carpet/script/ScriptHost.java +++ b/src/main/java/carpet/script/ScriptHost.java @@ -268,7 +268,7 @@ public abstract class ScriptHost data.functionImports.remove(varName); } - public void addUserDefinedFunction(Module module, String name, FunctionValue fun) + public void addUserDefinedFunction(Context ctx, Module module, String name, FunctionValue fun) { moduleData.get(module).globalFunctions.put(name, fun); }