This commit is contained in:
gnembon committed 2020-11-02 16:27:03 -04:00
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; private int saveTimeout;
public boolean persistenceRequired; 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); super(code, perUser, parent);
this.saveTimeout = 0; this.saveTimeout = 0;
@@ -57,11 +57,12 @@ public class CarpetScriptHost extends ScriptHost
{ {
persistenceRequired = ((CarpetScriptHost)parent).persistenceRequired; persistenceRequired = ((CarpetScriptHost)parent).persistenceRequired;
} }
appConfig = config;
} }
public static CarpetScriptHost create(CarpetScriptServer scriptServer, Module module, boolean perPlayer, ServerCommandSource source) 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 // parse code and convert to expression
if (module != null) if (module != null)
{ {
@@ -96,7 +97,7 @@ public class CarpetScriptHost extends ScriptHost
@Override @Override
protected ScriptHost duplicate() protected ScriptHost duplicate()
{ {
return new CarpetScriptHost(scriptServer, main, false, this); return new CarpetScriptHost(scriptServer, main, false, this, appConfig);
} }
@Override @Override
@@ -115,18 +116,19 @@ public class CarpetScriptHost extends ScriptHost
} }
else if (funName.equals("__config")) 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 try
{ {
CarpetContext cctx = (CarpetContext)ctx; Value ret = callUDF(BlockPos.ORIGIN, scriptServer.server.getCommandSource(), getFunction("__config"), Collections.emptyList());
Value ret = callUDF(BlockPos.ORIGIN, cctx.s, function, Collections.emptyList()); if (!(ret instanceof MapValue)) return false;
if (!(ret instanceof MapValue)) return;
Map<Value, Value> config = ((MapValue) ret).getMap(); Map<Value, Value> config = ((MapValue) ret).getMap();
setPerPlayer(config.getOrDefault(new StringValue("scope"), new StringValue("player")).getString().equalsIgnoreCase("player")); setPerPlayer(config.getOrDefault(new StringValue("scope"), new StringValue("player")).getString().equalsIgnoreCase("player"));
persistenceRequired = config.getOrDefault(new StringValue("stay_loaded"), Value.FALSE).getBoolean(); persistenceRequired = config.getOrDefault(new StringValue("stay_loaded"), Value.FALSE).getBoolean();
@@ -134,7 +136,9 @@ public class CarpetScriptHost extends ScriptHost
} }
catch (NullPointerException | InvalidCallbackException ignored) catch (NullPointerException | InvalidCallbackException ignored)
{ {
return false;
} }
return true;
} }
@Override @Override
@@ -174,6 +174,12 @@ public class CarpetScriptServer
Messenger.m(source, "r Failed to add "+name+" app"); Messenger.m(source, "r Failed to add "+name+" app");
return false; 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) if (module == null)
{ {
Messenger.m(source, "r Unable to locate the app, but created empty "+name+" app instead"); Messenger.m(source, "r Unable to locate the app, but created empty "+name+" app instead");
@@ -195,11 +201,11 @@ public class CarpetScriptServer
return false; return false;
} }
//addEvents(source, name); //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; 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); CarpetScriptHost host = modules.get(hostName);
String loaded = isReload?"reloaded":"loaded"; String loaded = isReload?"reloaded":"loaded";