Refactor Module into a single record type and fix system globals (#1383)

This commit is contained in:
altrisi authored and GitHub committed 2022-05-17 20:11:49 +02:00
1 parent d681cb4c54
commit a60bc386cb
20 files changed
+279 -215

No files matched your search

@@ -5,7 +5,6 @@ import carpet.CarpetSettings;
import carpet.script.api.Auxiliary;
import carpet.script.argument.FileArgument;
import carpet.script.argument.FunctionArgument;
import carpet.script.bundled.Module;
import carpet.script.command.CommandArgument;
import carpet.script.command.CommandToken;
import carpet.script.exception.CarpetExpressionException;
@@ -71,7 +70,6 @@ import static net.minecraft.commands.Commands.literal;
public class CarpetScriptHost extends ScriptHost
{
private final CarpetScriptServer scriptServer;
public CommandSourceStack responsibleSource;
private Tag globalState;
@@ -87,9 +85,8 @@ public class CarpetScriptHost extends ScriptHost
private CarpetScriptHost(CarpetScriptServer server, Module code, boolean perUser, ScriptHost parent, Map<Value, Value> config, Map<String, CommandArgument> argTypes, Predicate<CommandSourceStack> commandValidator, boolean isRuleApp)
{
super(code, perUser, parent);
super(code, server, perUser, parent);
this.saveTimeout = 0;
this.scriptServer = server;
persistenceRequired = true;
if (parent == null && code != null) // app, not a global host
{
@@ -115,13 +112,8 @@ public class CarpetScriptHost extends ScriptHost
{
try
{
String code = module.getCode();
if (code == null)
{
throw new LoadException("Code not found");
}
host.setChatErrorSnooper(source);
CarpetExpression ex = new CarpetExpression(host.main, code, source, new BlockPos(0, 0, 0));
CarpetExpression ex = new CarpetExpression(host.main, module.code(), source, new BlockPos(0, 0, 0));
ex.getExpr().asATextSource();
host.storeSource = storeSource;
ex.scriptRunCommand(host, new BlockPos(source.getPosition()));
@@ -175,7 +167,7 @@ public class CarpetScriptHost extends ScriptHost
FunctionArgument functionSpec
) throws CommandSyntaxException
{
String hostName = main.getName();
String hostName = main.name();
List<String> commandArgs = path.stream().filter(t -> t.isArgument).map(t -> t.surface).collect(Collectors.toList());
if (commandArgs.size() != (functionSpec.function.getNumParams()-functionSpec.args.size()) )
throw CommandArgument.error("Number of parameters in function "+functionSpec.function.fullName()+" doesn't match parameters for a command");
@@ -197,7 +189,7 @@ public class CarpetScriptHost extends ScriptHost
List<Pair<List<CommandToken>,FunctionArgument>> entries, Predicate<CommandSourceStack> useValidator
) throws CommandSyntaxException
{
String hostName = main.getName();
String hostName = main.name();
Predicate<CommandSourceStack> configValidator = getCommandConfigPermissions();
LiteralArgumentBuilder<CommandSourceStack> command = literal(hostName).
requires((player) -> CarpetServer.scriptServer.modules.containsKey(hostName) && useValidator.test(player) && configValidator.test(player));
@@ -257,7 +249,7 @@ public class CarpetScriptHost extends ScriptHost
@Override
protected ScriptHost duplicate()
{
return new CarpetScriptHost(scriptServer, main, false, this, appConfig, appArgTypes, commandValidator, isRuleApp);
return new CarpetScriptHost(scriptServer(), main, false, this, appConfig, appArgTypes, commandValidator, isRuleApp);
}
@Override
@@ -283,7 +275,7 @@ public class CarpetScriptHost extends ScriptHost
// 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.addBuiltInEvent(event, this, function, null);
scriptServer().events.addBuiltInEvent(event, this, function, null);
}
else if (funName.equals("__config"))
{
@@ -402,7 +394,7 @@ public class CarpetScriptHost extends ScriptHost
LiteralArgumentBuilder<CommandSourceStack> command = readCommands(commandValidator);
if (command != null)
{
scriptServer.server.getCommands().getDispatcher().register(command);
scriptServer().server.getCommands().getDispatcher().register(command);
return true;
}
else {
@@ -457,7 +449,7 @@ public class CarpetScriptHost extends ScriptHost
if (main == null) return false;
if (getFunction("__command") == null) return false;
if (scriptServer.isInvalidCommandRoot(getName()))
if (scriptServer().isInvalidCommandRoot(getName()))
{
notifier.accept(Messenger.c("gi Tried to mask vanilla command."));
return null;
@@ -475,10 +467,10 @@ public class CarpetScriptHost extends ScriptHost
}
String hostName = getName();
LiteralArgumentBuilder<CommandSourceStack> command = literal(hostName).
requires((player) -> scriptServer.modules.containsKey(hostName) && commandValidator.test(player) && configValidator.test(player)).
requires((player) -> scriptServer().modules.containsKey(hostName) && commandValidator.test(player) && configValidator.test(player)).
executes( (c) ->
{
CarpetScriptHost targetHost = scriptServer.modules.get(hostName).retrieveOwnForExecution(c.getSource());
CarpetScriptHost targetHost = scriptServer().modules.get(hostName).retrieveOwnForExecution(c.getSource());
Value response = targetHost.handleCommandLegacy(c.getSource(),"__command", null, "");
if (!response.isNull()) Messenger.m(c.getSource(), "gi "+response.getString());
return (int)response.readInteger();
@@ -508,23 +500,23 @@ public class CarpetScriptHost extends ScriptHost
{
command = command.
then(literal(function).
requires((player) -> scriptServer.modules.containsKey(hostName) && scriptServer.modules.get(hostName).getFunction(function) != null).
requires((player) -> scriptServer().modules.containsKey(hostName) && scriptServer().modules.get(hostName).getFunction(function) != null).
executes((c) -> {
CarpetScriptHost targetHost = scriptServer.modules.get(hostName).retrieveOwnForExecution(c.getSource());
CarpetScriptHost targetHost = scriptServer().modules.get(hostName).retrieveOwnForExecution(c.getSource());
Value response = targetHost.handleCommandLegacy(c.getSource(),function, null, "");
if (!response.isNull()) Messenger.m(c.getSource(), "gi " + response.getString());
return (int) response.readInteger();
}).
then(argument("args...", StringArgumentType.greedyString()).
executes( (c) -> {
CarpetScriptHost targetHost = scriptServer.modules.get(hostName).retrieveOwnForExecution(c.getSource());
CarpetScriptHost targetHost = scriptServer().modules.get(hostName).retrieveOwnForExecution(c.getSource());
Value response = targetHost.handleCommandLegacy(c.getSource(),function, null, StringArgumentType.getString(c, "args..."));
if (!response.isNull()) Messenger.m(c.getSource(), "gi "+response.getString());
return (int)response.readInteger();
})));
}
}
scriptServer.server.getCommands().getDispatcher().register(command);
scriptServer().server.getCommands().getDispatcher().register(command);
return true;
}
@@ -570,8 +562,8 @@ public class CarpetScriptHost extends ScriptHost
@Override
protected Module getModuleOrLibraryByName(String name)
{
Module module = scriptServer.getModule(name, true);
if (module == null || module.getCode() == null)
Module module = scriptServer().getModule(name, true);
if (module == null)
throw new InternalExpressionException("Unable to locate package: "+name);
return module;
}
@@ -580,7 +572,7 @@ public class CarpetScriptHost extends ScriptHost
protected void runModuleCode(Context c, Module module)
{
CarpetContext cc = (CarpetContext)c;
CarpetExpression ex = new CarpetExpression(module, module.getCode(), cc.s, cc.origin);
CarpetExpression ex = new CarpetExpression(module, module.code(), cc.s, cc.origin);
ex.getExpr().asATextSource();
ex.scriptRunCommand(this, cc.origin);
}
@@ -594,7 +586,7 @@ public class CarpetScriptHost extends ScriptHost
{
// 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_","");
scriptServer.events.removeBuiltInEvent(event, this, funName);
scriptServer().events.removeBuiltInEvent(event, this, funName);
}
}
@@ -663,7 +655,7 @@ public class CarpetScriptHost extends ScriptHost
{
try
{
return scriptServer.events.handleEvents.getWhileDisabled(() -> call(source, function, args));
return scriptServer().events.handleEvents.getWhileDisabled(() -> call(source, function, args));
}
catch (CarpetExpressionException exc)
{
@@ -768,7 +760,7 @@ public class CarpetScriptHost extends ScriptHost
// TODO: this is just for now - invoke would be able to invoke other hosts scripts
assertAppIntegrity(function.getModule());
Context context = new CarpetContext(this, source, BlockPos.ZERO);
return scriptServer.events.handleEvents.getWhileDisabled(() -> function.getExpression().evalValue(
return scriptServer().events.handleEvents.getWhileDisabled(() -> function.getExpression().evalValue(
() -> function.lazyEval(context, Context.VOID, function.getExpression(), function.getToken(), argv),
context,
Context.VOID
@@ -842,9 +834,9 @@ public class CarpetScriptHost extends ScriptHost
public Value callNow(FunctionValue fun, List<Value> arguments)
{
ServerPlayer player = (user==null)?null:scriptServer.server.getPlayerList().getPlayerByName(user);
CommandSourceStack source = (player != null)?player.createCommandSourceStack():scriptServer.server.createCommandSourceStack();
return scriptServer.events.handleEvents.getWhileDisabled(()->{
ServerPlayer player = (user==null)?null:scriptServer().server.getPlayerList().getPlayerByName(user);
CommandSourceStack source = (player != null)?player.createCommandSourceStack():scriptServer().server.createCommandSourceStack();
return scriptServer().events.handleEvents.getWhileDisabled(()->{
try
{
return callUDF(BlockPos.ZERO, source, fun, arguments);
@@ -872,7 +864,7 @@ public class CarpetScriptHost extends ScriptHost
{
String markerName = Auxiliary.MARKER_STRING + "_" + ((getName() == null) ? "" : getName());
for (ServerLevel world : scriptServer.server.getAllLevels())
for (ServerLevel world : scriptServer().server.getAllLevels())
{
for (Entity e : world.getEntities(EntityType.ARMOR_STAND, (as) -> as.getTags().contains(markerName)))
{
@@ -1101,9 +1093,19 @@ public class CarpetScriptHost extends ScriptHost
handleErrorWithStack(message, new CarpetExpressionException(exc.getMessage(), exc.stack));
}
/**
* @deprecated Use {@link #scriptServer()} instead
*/
@Deprecated(forRemoval = true)
public CarpetScriptServer getScriptServer()
{
return scriptServer;
return scriptServer();
}
@Override
public CarpetScriptServer scriptServer()
{
return (CarpetScriptServer)super.scriptServer();
}
@Override