From bd61340e466115e8fc29b78f70144e63eb54f3d0 Mon Sep 17 00:00:00 2001 From: gnembon Date: Mon, 18 Nov 2019 16:06:35 -0400 Subject: [PATCH] events are added on the fly as source loads --- .../java/carpet/script/CarpetEventServer.java | 18 +++++++++++++++ .../carpet/script/CarpetScriptServer.java | 5 ++-- src/main/java/carpet/script/Expression.java | 2 +- src/main/java/carpet/script/ScriptHost.java | 23 +++++++++++++++++-- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/main/java/carpet/script/CarpetEventServer.java b/src/main/java/carpet/script/CarpetEventServer.java index 8515f259..9b9c66ff 100644 --- a/src/main/java/carpet/script/CarpetEventServer.java +++ b/src/main/java/carpet/script/CarpetEventServer.java @@ -133,6 +133,19 @@ public class CarpetEventServer callList.add(new Callback(hostName, udf)); return true; } + public boolean addEventCallDirect(ScriptHost host, FunctionValue function) + { + if (function == null || function.getArguments().size() != reqArgs) + { + return false; + } + //all clear + //remove duplicates + removeEventCall(host.getName(), function.getString()); + callList.add(new Callback(host.getName(), function)); + return true; + } + public void removeEventCall(String hostName, String funName) { callList.removeIf((c)-> c.udf.getString().equals(funName) && ( hostName == null || c.host.equalsIgnoreCase(hostName) ) ); @@ -461,6 +474,11 @@ public class CarpetEventServer return Event.byName.get(event).handler.addEventCall(host, funName); } + public boolean addEventDirectly(String event, ScriptHost host, FunctionValue function) + { + return Event.byName.get(event).handler.addEventCallDirect(host, function); + } + public boolean removeEvent(String event, String funName) { diff --git a/src/main/java/carpet/script/CarpetScriptServer.java b/src/main/java/carpet/script/CarpetScriptServer.java index c6cef679..58e623c7 100644 --- a/src/main/java/carpet/script/CarpetScriptServer.java +++ b/src/main/java/carpet/script/CarpetScriptServer.java @@ -131,7 +131,7 @@ public class CarpetScriptServer private ScriptHost createMinecraftScriptHost(String name, ModuleInterface module, boolean perPlayer, ServerCommandSource source) { - ScriptHost host = new ScriptHost(name, module, perPlayer, null ); + ScriptHost host = new ScriptHost(this, name, module, perPlayer, null ); host.globalVariables.put("_x", (c, t) -> Value.ZERO); host.globalVariables.put("_y", (c, t) -> Value.ZERO); host.globalVariables.put("_z", (c, t) -> Value.ZERO); @@ -192,7 +192,7 @@ public class CarpetScriptServer removeScriptHost(source, name); return false; } - addEvents(source, name); + //addEvents(source, name); addCommand(source, name); return true; } @@ -220,6 +220,7 @@ public class CarpetScriptServer } private void addEvents(ServerCommandSource source, String hostName) { + if (1+2 == 3) throw new RuntimeException("This should run when code is evaluated"); ScriptHost host = modules.get(hostName); if (host == null) { diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index 910d84ca..993823aa 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -680,7 +680,7 @@ public class Expression implements Cloneable FunctionValue result = new FunctionValue(expr, token, name, code, arguments, contextValues); // do not store lambda definitions - if (!name.equals("_")) context.host.globalFunctions.put(name, result); + if (!name.equals("_")) context.host.addUserDefinedFunction(name, result); return result; } diff --git a/src/main/java/carpet/script/ScriptHost.java b/src/main/java/carpet/script/ScriptHost.java index 6875cf35..19965625 100644 --- a/src/main/java/carpet/script/ScriptHost.java +++ b/src/main/java/carpet/script/ScriptHost.java @@ -45,7 +45,9 @@ public class ScriptHost private final ModuleInterface myCode; private boolean perUser; - ScriptHost(String name, ModuleInterface code, boolean perUser, ScriptHost parent) + private CarpetScriptServer scriptServer; + + ScriptHost(CarpetScriptServer server, String name, ModuleInterface code, boolean perUser, ScriptHost parent) { this.saveTimeout = 0; this.parent = parent; @@ -65,6 +67,23 @@ public class ScriptHost if (parent == null && name != null) globalState = loadState(); + + this.scriptServer = server; + } + + public void addUserDefinedFunction(String funName, FunctionValue function) + { + // generic + globalFunctions.put(funName, function); + // mcarpet + if (funName.startsWith("__on_")) + { + // 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); + } + } private ScriptHost retrieveForExecution(String /*Nullable*/ user) @@ -74,7 +93,7 @@ public class ScriptHost ScriptHost userHost = userHosts.get(user); if (userHost != null) return userHost; - userHost = new ScriptHost(this.name, myCode, false, this); + userHost = new ScriptHost(this.scriptServer, this.name, myCode, false, this); userHost.globalVariables.putAll(this.globalVariables); userHost.globalFunctions.putAll(this.globalFunctions); userHosts.put(user, userHost);