added feedback to event calling and fixed event registry

disabled ability to handle global events from player scoped apps
This commit is contained in:
gnembon committed 2020-02-25 01:29:21 -04:00
1 parent a94ab55e66
commit 5c9774f1d2
6 files changed
+69 -43

No files matched your search

@@ -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<ScriptHost, Boolean> verifier)
public boolean addEventCall(ServerCommandSource source, String hostName, String funName, Function<ScriptHost, Boolean> 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<String,String> 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;
}
@@ -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<Value, Value> 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);
}
}
@@ -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<Value, Value> 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);
+1 -1
View File
@@ -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;
}
+1 -1
View File
@@ -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);
}