Easier for extensions to add their own built-in apps

This commit is contained in:
gnembon committed 2020-07-12 23:00:05 -03:00
1 parent 3140a4fe53
commit 5f34b5c96c
2 files changed
+29 -14

No files matched your search

@@ -43,14 +43,22 @@ public class CarpetScriptServer
private final Set<String> holyMoly; private final Set<String> holyMoly;
public final CarpetEventServer events; public final CarpetEventServer events;
private static final List<Module> bundledModuleData = new ArrayList<Module>(){{ private static final List<Module> bundledModuleData = new ArrayList<>();
add(new BundledModule("camera", false));
add(new BundledModule("overlay", false)); public static void registerBuiltInScript(BundledModule app)
add(new BundledModule("event_test", false)); {
add(new BundledModule("stats_test", false)); bundledModuleData.add(app);
add(new BundledModule("math", true)); }
add(new BundledModule("chunk_display", false));
}}; static
{
registerBuiltInScript(BundledModule.carpetNative("camera", false));
registerBuiltInScript(BundledModule.carpetNative("overlay", false));
registerBuiltInScript(BundledModule.carpetNative("event_test", false));
registerBuiltInScript(BundledModule.carpetNative("stats_test", false));
registerBuiltInScript(BundledModule.carpetNative("math", true));
registerBuiltInScript(BundledModule.carpetNative("chunk_display", false));
}
public CarpetScriptServer(MinecraftServer server) public CarpetScriptServer(MinecraftServer server)
{ {
@@ -11,22 +11,29 @@ public class BundledModule extends Module
private String name; private String name;
private String code; private String code;
private boolean library; private boolean library;
public BundledModule(String scriptName, boolean isLibrary) public BundledModule(String name, String code, boolean isLibrary)
{ {
library = isLibrary; library = isLibrary;
this.name = name;
this.code = code;
}
public static BundledModule carpetNative(String scriptName, boolean isLibrary)
{
BundledModule module = new BundledModule(null, null, isLibrary);
try try
{ {
name = scriptName.toLowerCase(Locale.ROOT); module.name = scriptName.toLowerCase(Locale.ROOT);
code = IOUtils.toString( module.code = IOUtils.toString(
getClass().getClassLoader().getResourceAsStream("assets/carpet/scripts/"+name+(isLibrary?".scl":".sc")), BundledModule.class.getClassLoader().getResourceAsStream("assets/carpet/scripts/"+scriptName+(isLibrary?".scl":".sc")),
StandardCharsets.UTF_8 StandardCharsets.UTF_8
); );
} }
catch ( NullPointerException | IOException e) catch ( NullPointerException | IOException e)
{ {
name = null; module.name = null;
code = null; module.code = null;
} }
return module;
} }
@Override @Override