generated from Fallen-Breath/fabric-mod-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d311af8
commit af91ce4
Showing
2 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/com/vulpeus/vulpeus_carpet/utils/ScriptCollection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |