Merge branch 'master' into 1.15

This commit is contained in:
gnembon committed 2019-11-18 22:07:42 -04:00
commit 729399870e
4 files changed
+43 -5

No files matched your search

@@ -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)
{
@@ -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)
{
+1 -1
View File
@@ -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;
}
+21 -2
View File
@@ -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);