events are added on the fly as source loads
This commit is contained in:
1 parent
bf11cb7967
commit
bd61340e46
4 files changed
+43
-5
No files matched your search
@@ -133,6 +133,19 @@ public class CarpetEventServer
|
|||||||
callList.add(new Callback(hostName, udf));
|
callList.add(new Callback(hostName, udf));
|
||||||
return true;
|
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)
|
public void removeEventCall(String hostName, String funName)
|
||||||
{
|
{
|
||||||
callList.removeIf((c)-> c.udf.getString().equals(funName) && ( hostName == null || c.host.equalsIgnoreCase(hostName) ) );
|
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);
|
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)
|
public boolean removeEvent(String event, String funName)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ public class CarpetScriptServer
|
|||||||
|
|
||||||
private ScriptHost createMinecraftScriptHost(String name, ModuleInterface module, boolean perPlayer, ServerCommandSource source)
|
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("_x", (c, t) -> Value.ZERO);
|
||||||
host.globalVariables.put("_y", (c, t) -> Value.ZERO);
|
host.globalVariables.put("_y", (c, t) -> Value.ZERO);
|
||||||
host.globalVariables.put("_z", (c, t) -> Value.ZERO);
|
host.globalVariables.put("_z", (c, t) -> Value.ZERO);
|
||||||
@@ -192,7 +192,7 @@ public class CarpetScriptServer
|
|||||||
removeScriptHost(source, name);
|
removeScriptHost(source, name);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
addEvents(source, name);
|
//addEvents(source, name);
|
||||||
addCommand(source, name);
|
addCommand(source, name);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -220,6 +220,7 @@ public class CarpetScriptServer
|
|||||||
}
|
}
|
||||||
private void addEvents(ServerCommandSource source, String hostName)
|
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);
|
ScriptHost host = modules.get(hostName);
|
||||||
if (host == null)
|
if (host == null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -680,7 +680,7 @@ public class Expression implements Cloneable
|
|||||||
|
|
||||||
FunctionValue result = new FunctionValue(expr, token, name, code, arguments, contextValues);
|
FunctionValue result = new FunctionValue(expr, token, name, code, arguments, contextValues);
|
||||||
// do not store lambda definitions
|
// do not store lambda definitions
|
||||||
if (!name.equals("_")) context.host.globalFunctions.put(name, result);
|
if (!name.equals("_")) context.host.addUserDefinedFunction(name, result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,9 @@ public class ScriptHost
|
|||||||
private final ModuleInterface myCode;
|
private final ModuleInterface myCode;
|
||||||
private boolean perUser;
|
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.saveTimeout = 0;
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
@@ -65,6 +67,23 @@ public class ScriptHost
|
|||||||
|
|
||||||
if (parent == null && name != null)
|
if (parent == null && name != null)
|
||||||
globalState = loadState();
|
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)
|
private ScriptHost retrieveForExecution(String /*Nullable*/ user)
|
||||||
@@ -74,7 +93,7 @@ public class ScriptHost
|
|||||||
ScriptHost userHost = userHosts.get(user);
|
ScriptHost userHost = userHosts.get(user);
|
||||||
if (userHost != null)
|
if (userHost != null)
|
||||||
return userHost;
|
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.globalVariables.putAll(this.globalVariables);
|
||||||
userHost.globalFunctions.putAll(this.globalFunctions);
|
userHost.globalFunctions.putAll(this.globalFunctions);
|
||||||
userHosts.put(user, userHost);
|
userHosts.put(user, userHost);
|
||||||
|
|||||||
Reference in new issue
Block a user