Skip to content

Commit

Permalink
Script Collection Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
topi-banana committed Sep 10, 2024
1 parent d311af8 commit af91ce4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@

import carpet.CarpetExtension;
import carpet.CarpetServer;
import carpet.script.CarpetScriptServer;
import carpet.script.Module;
import carpet.utils.Translations;
import com.mojang.brigadier.CommandDispatcher;
import com.vulpeus.vulpeus_carpet.commands.hatCommand;
import com.vulpeus.vulpeus_carpet.commands.sitCommand;
import com.vulpeus.vulpeus_carpet.commands.viewCommand;
import com.vulpeus.vulpeus_carpet.utils.ScriptCollection;
import com.vulpeus.vulpeus_carpet.utils.rule.defaultOpLevel.PlayerUtil;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Map;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
Expand All @@ -45,6 +53,8 @@ public class VulpeusCarpetExtension implements CarpetExtension, ModInitializer {
public static final String MOD_NAME;
public static final Version MOD_VERSION;
public static final GameVersion GAME_VERSION;
public static final String ASSETS_PATH;
public static final ClassLoader CLASS_LOADER;

static {
ModMetadata metadata =
Expand All @@ -55,6 +65,10 @@ public class VulpeusCarpetExtension implements CarpetExtension, ModInitializer {
MOD_NAME = metadata.getName();
MOD_VERSION = metadata.getVersion();
GAME_VERSION = MinecraftVersion.CURRENT;

ASSETS_PATH = String.format("assets/%s", MOD_ID);

CLASS_LOADER = VulpeusCarpetExtension.class.getClassLoader();
}

public static void loadExtension() {
Expand All @@ -74,6 +88,7 @@ public void onInitialize() {
@Override
public void onGameStarted() {
CarpetServer.settingsManager.parseSettingsClass(VulpeusCarpetSettings.class);
ScriptCollection.load();
}

@Override
Expand All @@ -93,6 +108,7 @@ public void onPlayerLoggedIn(ServerPlayerEntity player) {
@Override
public Map<String, String> canHasTranslations(String lang) {
return Translations.getTranslationFromResourcePath(
String.format("assets/%s/lang/%s.json", MOD_ID, lang));
String.format("%s/lang/%s.json", ASSETS_PATH, lang)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.vulpeus.vulpeus_carpet.utils;

import static com.vulpeus.vulpeus_carpet.VulpeusCarpetExtension.*;

import carpet.script.CarpetScriptServer;
import carpet.script.Module;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;

public class ScriptCollection {

public static void load() {
URL scripts = CLASS_LOADER.getResource(String.format("%s/scripts/", ASSETS_PATH));
File scriptDir = new File(scripts.getPath());
File[] scriptFiles = scriptDir.listFiles();
for (File scriptFile : scriptFiles) {
Module module = fromScriptFilename(scriptFile.getName());
CarpetScriptServer.registerBuiltInApp(module);
}
}

private static Module fromScriptFilename(String filename) {
int index = filename.lastIndexOf(".");

String ext = filename.substring(index);
String scriptName = filename.substring(0, index);

if (ext.equals(".sc") || ext.equals(".scl")) {
return nativeScript(
filename,
scriptName,
ext.equals(".scl")
);
}
return null;
}
public static Module nativeScript(String filename, String scriptName, boolean isLibrary) {
try {
String code = IOUtils.toString(
CLASS_LOADER.getResourceAsStream(String.format("%s/scripts/%s", ASSETS_PATH, filename)),
StandardCharsets.UTF_8
);
return new Module(scriptName, code, isLibrary);
} catch (IOException e) {
throw new IllegalArgumentException("Failed to load bundled module", e);
}
}
}

0 comments on commit af91ce4

Please sign in to comment.