fixes to scarpet 1.5

This commit is contained in:
gnembon committed 2019-10-15 09:37:50 -03:00
1 parent 701a717135
commit 6524725245
11 files changed
+129 -30

No files matched your search

@@ -1,6 +1,8 @@
package carpet.script;
import carpet.script.bundled.BundledModule;
import carpet.script.value.MapValue;
import carpet.script.value.StringValue;
import carpet.settings.CarpetSettings;
import carpet.CarpetServer;
import carpet.script.bundled.FileModule;
@@ -19,6 +21,7 @@ import net.minecraft.util.math.BlockPos;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@@ -59,9 +62,10 @@ public class CarpetScriptServer
{
if (CarpetSettings.scriptsAutoload)
{
Messenger.m(CarpetServer.minecraft_server.getCommandSource(), "Autoloading world scarpet apps");
for (String moduleName: listAvailableModules(false))
{
addScriptHost(CarpetServer.minecraft_server.getCommandSource(), moduleName, true);
addScriptHost(CarpetServer.minecraft_server.getCommandSource(), moduleName, true, true);
}
}
@@ -156,7 +160,7 @@ public class CarpetScriptServer
return host;
}
public boolean addScriptHost(ServerCommandSource source, String name, boolean perPlayer)
public boolean addScriptHost(ServerCommandSource source, String name, boolean perPlayer, boolean autoload)
{
//TODO add per player modules to support player actions better on a server
name = name.toLowerCase(Locale.ROOT);
@@ -164,32 +168,56 @@ public class CarpetScriptServer
ScriptHost newHost = createMinecraftScriptHost(name, module, perPlayer, source);
if (newHost == null)
{
Messenger.m(source, "r Failed to add a module");
Messenger.m(source, "r Failed to add "+name+" app");
return false;
}
if (module == null)
{
Messenger.m(source, "r Unable to locate the package, but created empty host "+name+" instead");
Messenger.m(source, "r Unable to locate the app, but created empty "+name+" app instead");
modules.put(name, newHost);
return true;
}
String code = module.getCode();
if (module.getCode() == null)
{
Messenger.m(source, "r Unable to load the package - not found");
Messenger.m(source, "r Unable to load "+name+" app - not found");
return false;
}
modules.put(name, newHost);
if (!addConfig(source, name) && autoload)
{
removeScriptHost(source, name);
return false;
}
addEvents(source, name);
addCommand(source, name);
return true;
}
public void addEvents(ServerCommandSource source, String hostName)
private boolean addConfig(ServerCommandSource source, String hostName)
{
ScriptHost host = modules.get(hostName);
if (host == null || !host.globalFunctions.containsKey("__config"))
{
return false;
}
try
{
Value ret = host.callUDF(BlockPos.ORIGIN, source, host.globalFunctions.get("__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 addEvents(ServerCommandSource source, String hostName)
{
ScriptHost host = modules.get(hostName);
if (host == null)
@@ -208,7 +236,7 @@ public class CarpetScriptServer
}
public void addCommand(ServerCommandSource source, String hostName)
private void addCommand(ServerCommandSource source, String hostName)
{
ScriptHost host = modules.get(hostName);
if (host == null)