added feedback to event calling and fixed event registry
disabled ability to handle global events from player scoped apps
This commit is contained in:
1 parent
a94ab55e66
commit
5c9774f1d2
6 files changed
+69
-43
No files matched your search
@@ -239,6 +239,7 @@ public class ScriptCommand
|
|||||||
then(argument("call", StringArgumentType.word()).
|
then(argument("call", StringArgumentType.word()).
|
||||||
suggests( (cc, bb) -> suggestMatching(suggestFunctionCalls(cc), bb)).
|
suggests( (cc, bb) -> suggestMatching(suggestFunctionCalls(cc), bb)).
|
||||||
executes( (cc) -> CarpetServer.scriptServer.events.addEvent(
|
executes( (cc) -> CarpetServer.scriptServer.events.addEvent(
|
||||||
|
cc.getSource(),
|
||||||
StringArgumentType.getString(cc, "event"),
|
StringArgumentType.getString(cc, "event"),
|
||||||
null,
|
null,
|
||||||
StringArgumentType.getString(cc, "call")
|
StringArgumentType.getString(cc, "call")
|
||||||
@@ -249,6 +250,7 @@ public class ScriptCommand
|
|||||||
then(argument("call", StringArgumentType.word()).
|
then(argument("call", StringArgumentType.word()).
|
||||||
suggests( (cc, bb) -> suggestMatching(suggestFunctionCalls(cc), bb)).
|
suggests( (cc, bb) -> suggestMatching(suggestFunctionCalls(cc), bb)).
|
||||||
executes( (cc) -> CarpetServer.scriptServer.events.addEvent(
|
executes( (cc) -> CarpetServer.scriptServer.events.addEvent(
|
||||||
|
cc.getSource(),
|
||||||
StringArgumentType.getString(cc, "event"),
|
StringArgumentType.getString(cc, "event"),
|
||||||
StringArgumentType.getString(cc, "app"),
|
StringArgumentType.getString(cc, "app"),
|
||||||
StringArgumentType.getString(cc, "call")
|
StringArgumentType.getString(cc, "call")
|
||||||
@@ -259,6 +261,7 @@ public class ScriptCommand
|
|||||||
then(argument("call", StringArgumentType.greedyString()).
|
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)).
|
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(
|
executes( (cc) -> CarpetServer.scriptServer.events.removeEvent(
|
||||||
|
cc.getSource(),
|
||||||
StringArgumentType.getString(cc, "event"),
|
StringArgumentType.getString(cc, "event"),
|
||||||
StringArgumentType.getString(cc, "call")
|
StringArgumentType.getString(cc, "call")
|
||||||
)?1:0))));
|
)?1:0))));
|
||||||
@@ -308,7 +311,7 @@ public class ScriptCommand
|
|||||||
Messenger.m(source, "w Handlers for "+event.name+": ");
|
Messenger.m(source, "w Handlers for "+event.name+": ");
|
||||||
shownEvent = true;
|
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;
|
return 1;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package carpet.script;
|
package carpet.script;
|
||||||
|
|
||||||
import carpet.CarpetServer;
|
import carpet.CarpetServer;
|
||||||
|
import carpet.script.exception.InternalExpressionException;
|
||||||
import carpet.script.value.BlockValue;
|
import carpet.script.value.BlockValue;
|
||||||
import carpet.script.value.EntityValue;
|
import carpet.script.value.EntityValue;
|
||||||
import carpet.script.value.FunctionValue;
|
import carpet.script.value.FunctionValue;
|
||||||
@@ -9,6 +10,7 @@ import carpet.script.value.NBTSerializableValue;
|
|||||||
import carpet.script.value.NumericValue;
|
import carpet.script.value.NumericValue;
|
||||||
import carpet.script.value.StringValue;
|
import carpet.script.value.StringValue;
|
||||||
import carpet.script.value.Value;
|
import carpet.script.value.Value;
|
||||||
|
import carpet.utils.Messenger;
|
||||||
import net.minecraft.block.BlockState;
|
import net.minecraft.block.BlockState;
|
||||||
import net.minecraft.server.command.ServerCommandSource;
|
import net.minecraft.server.command.ServerCommandSource;
|
||||||
import net.minecraft.entity.Entity;
|
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
|
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);
|
ScriptHost host = CarpetServer.scriptServer.getHostByName(hostName);
|
||||||
if (host == null)
|
if (host == null)
|
||||||
{
|
{
|
||||||
// impossible call to add
|
// impossible call to add
|
||||||
|
Messenger.m(source, "r Unknown app "+hostName);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!verifier.apply(host))
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
FunctionValue udf = host.getFunction(funName);
|
FunctionValue udf = host.getFunction(funName);
|
||||||
if (udf == null || udf.getArguments().size() != reqArgs)
|
if (udf == null || udf.getArguments().size() != reqArgs)
|
||||||
{
|
{
|
||||||
// call won't match arguments
|
// call won't match arguments
|
||||||
|
Messenger.m(source, "r Callback doesn't expect required number of arguments: "+reqArgs);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
//all clear
|
//all clear
|
||||||
//remove duplicates
|
//remove duplicates
|
||||||
|
|
||||||
removeEventCall(hostName, udf.getString());
|
removeEventCall(hostName, udf.getString());
|
||||||
callList.add(new Callback(hostName, udf));
|
callList.add(new Callback(hostName, udf));
|
||||||
return true;
|
return true;
|
||||||
@@ -567,38 +572,44 @@ public class CarpetEventServer
|
|||||||
scheduledCalls.add(new ScheduledCall(context, function, args, due));
|
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))
|
if (!Event.byName.containsKey(event))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Event ev = Event.byName.get(event);
|
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)
|
public boolean addEventDirectly(String event, ScriptHost host, FunctionValue function)
|
||||||
{
|
{
|
||||||
Event ev = Event.byName.get(event);
|
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))
|
||||||
//if (!canAddEvent(ev, host))
|
throw new InternalExpressionException("Global event "+event+" can only be added to apps with global scope");
|
||||||
// throw new InternalExpressionException("Global event "+event+" can only be added to apps with global scope");
|
|
||||||
return ev.handler.addEventCallDirect(host, function);
|
return ev.handler.addEventCallDirect(host, function);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean canAddEvent(Event event, ScriptHost host)
|
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))
|
if (!Event.byName.containsKey(event))
|
||||||
|
{
|
||||||
|
Messenger.m(source, "r Unknown event: " + event);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
Pair<String,String> call = decodeCallback(funName);
|
Pair<String,String> call = decodeCallback(funName);
|
||||||
Event.byName.get(event).handler.removeEventCall(call.getLeft(), call.getRight());
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import carpet.script.exception.ExpressionException;
|
|||||||
import carpet.script.exception.InternalExpressionException;
|
import carpet.script.exception.InternalExpressionException;
|
||||||
import carpet.script.exception.InvalidCallbackException;
|
import carpet.script.exception.InvalidCallbackException;
|
||||||
import carpet.script.value.FunctionValue;
|
import carpet.script.value.FunctionValue;
|
||||||
|
import carpet.script.value.MapValue;
|
||||||
import carpet.script.value.NumericValue;
|
import carpet.script.value.NumericValue;
|
||||||
import carpet.script.value.StringValue;
|
import carpet.script.value.StringValue;
|
||||||
import carpet.script.value.Value;
|
import carpet.script.value.Value;
|
||||||
@@ -26,6 +27,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import static java.lang.Math.max;
|
import static java.lang.Math.max;
|
||||||
|
|
||||||
@@ -36,14 +38,23 @@ public class CarpetScriptHost extends ScriptHost
|
|||||||
|
|
||||||
private Tag globalState;
|
private Tag globalState;
|
||||||
private int saveTimeout;
|
private int saveTimeout;
|
||||||
|
public boolean persistenceRequired;
|
||||||
|
|
||||||
private CarpetScriptHost(CarpetScriptServer server, Module code, boolean perUser, ScriptHost parent)
|
private CarpetScriptHost(CarpetScriptServer server, Module code, boolean perUser, ScriptHost parent)
|
||||||
{
|
{
|
||||||
super(code, perUser, parent);
|
super(code, perUser, parent);
|
||||||
this.saveTimeout = 0;
|
this.saveTimeout = 0;
|
||||||
this.scriptServer = server;
|
this.scriptServer = server;
|
||||||
if (parent == null && code != null)
|
persistenceRequired = true;
|
||||||
|
if (parent == null && code != null) // app, not a global host
|
||||||
|
{
|
||||||
|
persistenceRequired = false;
|
||||||
globalState = loadState();
|
globalState = loadState();
|
||||||
|
}
|
||||||
|
else if (parent != null)
|
||||||
|
{
|
||||||
|
persistenceRequired = ((CarpetScriptHost)parent).persistenceRequired;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CarpetScriptHost create(CarpetScriptServer scriptServer, Module module, boolean perPlayer, ServerCommandSource source)
|
public static CarpetScriptHost create(CarpetScriptServer scriptServer, Module module, boolean perPlayer, ServerCommandSource source)
|
||||||
@@ -82,16 +93,39 @@ public class CarpetScriptHost extends ScriptHost
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
// 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);
|
modules.put(name, newHost);
|
||||||
|
|
||||||
if (!addConfig(source, name) && autoload)
|
if (autoload && !newHost.persistenceRequired)
|
||||||
{
|
{
|
||||||
removeScriptHost(source, name);
|
removeScriptHost(source, name);
|
||||||
return false;
|
return false;
|
||||||
@@ -174,28 +174,6 @@ public class CarpetScriptServer
|
|||||||
return true;
|
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)
|
private void addCommand(ServerCommandSource source, String hostName)
|
||||||
{
|
{
|
||||||
ScriptHost host = modules.get(hostName);
|
ScriptHost host = modules.get(hostName);
|
||||||
|
|||||||
@@ -680,7 +680,7 @@ public class Expression
|
|||||||
|
|
||||||
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.addUserDefinedFunction(module, name, result);
|
if (!name.equals("_")) context.host.addUserDefinedFunction(context, module, name, result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -268,7 +268,7 @@ public abstract class ScriptHost
|
|||||||
data.functionImports.remove(varName);
|
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);
|
moduleData.get(module).globalFunctions.put(name, fun);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user