wip
This commit is contained in:
1 parent
1d81768c54
commit
d9727862da
2 files changed
+21
-11
No files matched your search
@@ -40,9 +40,9 @@ public class CarpetScriptHost extends ScriptHost
|
||||
private int saveTimeout;
|
||||
public boolean persistenceRequired;
|
||||
|
||||
public Map<Value, Value> appConfig = Collections.emptyMap();
|
||||
public Map<Value, Value> appConfig;
|
||||
|
||||
private CarpetScriptHost(CarpetScriptServer server, Module code, boolean perUser, ScriptHost parent)
|
||||
private CarpetScriptHost(CarpetScriptServer server, Module code, boolean perUser, ScriptHost parent, Map<Value, Value> config)
|
||||
{
|
||||
super(code, perUser, parent);
|
||||
this.saveTimeout = 0;
|
||||
@@ -57,11 +57,12 @@ public class CarpetScriptHost extends ScriptHost
|
||||
{
|
||||
persistenceRequired = ((CarpetScriptHost)parent).persistenceRequired;
|
||||
}
|
||||
appConfig = config;
|
||||
}
|
||||
|
||||
public static CarpetScriptHost create(CarpetScriptServer scriptServer, Module module, boolean perPlayer, ServerCommandSource source)
|
||||
{
|
||||
CarpetScriptHost host = new CarpetScriptHost(scriptServer, module, perPlayer, null );
|
||||
CarpetScriptHost host = new CarpetScriptHost(scriptServer, module, perPlayer, null, Collections.emptyMap() );
|
||||
// parse code and convert to expression
|
||||
if (module != null)
|
||||
{
|
||||
@@ -96,7 +97,7 @@ public class CarpetScriptHost extends ScriptHost
|
||||
@Override
|
||||
protected ScriptHost duplicate()
|
||||
{
|
||||
return new CarpetScriptHost(scriptServer, main, false, this);
|
||||
return new CarpetScriptHost(scriptServer, main, false, this, appConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -115,18 +116,19 @@ public class CarpetScriptHost extends ScriptHost
|
||||
}
|
||||
else if (funName.equals("__config"))
|
||||
{
|
||||
addAppConfig(ctx, function);
|
||||
// needs to be added as we read the code, cause other events may be affected.
|
||||
if (!readConfig())
|
||||
throw new InternalExpressionException("Invalid app config (via '__config()' function)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addAppConfig(Context ctx, FunctionValue function)
|
||||
private boolean readConfig()
|
||||
{
|
||||
try
|
||||
{
|
||||
CarpetContext cctx = (CarpetContext)ctx;
|
||||
Value ret = callUDF(BlockPos.ORIGIN, cctx.s, function, Collections.emptyList());
|
||||
if (!(ret instanceof MapValue)) return;
|
||||
Value ret = callUDF(BlockPos.ORIGIN, scriptServer.server.getCommandSource(), getFunction("__config"), Collections.emptyList());
|
||||
if (!(ret instanceof MapValue)) return false;
|
||||
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();
|
||||
@@ -134,7 +136,9 @@ public class CarpetScriptHost extends ScriptHost
|
||||
}
|
||||
catch (NullPointerException | InvalidCallbackException ignored)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -174,6 +174,12 @@ public class CarpetScriptServer
|
||||
Messenger.m(source, "r Failed to add "+name+" app");
|
||||
return false;
|
||||
}
|
||||
// config needs to be read as we load the app since some events use that info
|
||||
//if (!newHost.readConfig())
|
||||
//{
|
||||
// Messenger.m(source, "r Failed to add "+name+" app: invalid app config (via '__config()' function)");
|
||||
// return false;
|
||||
//}
|
||||
if (module == null)
|
||||
{
|
||||
Messenger.m(source, "r Unable to locate the app, but created empty "+name+" app instead");
|
||||
@@ -195,11 +201,11 @@ public class CarpetScriptServer
|
||||
return false;
|
||||
}
|
||||
//addEvents(source, name);
|
||||
addCommand(source, name, reload);
|
||||
addLegacyCommand(source, name, reload); // this needs to be moved to config reader, only supporting legacy command here
|
||||
return true;
|
||||
}
|
||||
|
||||
private void addCommand(ServerCommandSource source, String hostName, boolean isReload)
|
||||
private void addLegacyCommand(ServerCommandSource source, String hostName, boolean isReload)
|
||||
{
|
||||
CarpetScriptHost host = modules.get(hostName);
|
||||
String loaded = isReload?"reloaded":"loaded";
|
||||
|
||||
Reference in new issue
Block a user