Skip to content

Commit

Permalink
add support for runScript
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Aug 12, 2020
1 parent 11ce009 commit a400894
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dependencies {
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

modImplementation name: "jsmacros-1.16.1-1.2.2-beta-b071ddf-dev"
modImplementation name: "jsmacros-1.16.2-1.2.4-beta-921af6a-dev"

//partial fabric api
modImplementation "net.fabricmc.fabric-api:fabric-api-base:${project.api_base_version}"
Expand Down
27 changes: 24 additions & 3 deletions src/main/java/xyz/wagyourtail/jsmacrosjython/JsMacrosJython.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xyz.wagyourtail.jsmacrosjython;

import java.io.File;
import java.nio.file.Path;
import java.util.Map;

import org.python.util.PythonInterpreter;
Expand All @@ -13,7 +14,6 @@

public class JsMacrosJython implements ClientModInitializer {
public static boolean hasJEP = false;
private static Functions consumerFix = new consumerFunctions("consumer");

@Override
public void onInitializeClient() {
Expand All @@ -26,6 +26,7 @@ public void onInitializeClient() {

// register language
RunScript.addLanguage(new RunScript.Language() {
private Functions consumerFix = new consumerFunctions("consumer");

@Override
public void exec(RawMacro macro, File file, String event, Map<String, Object> args) throws Exception {
Expand All @@ -49,12 +50,32 @@ public void exec(RawMacro macro, File file, String event, Map<String, Object> ar
}
}


@Override
public void exec(String script, Map<String, Object> globals, Path path) throws Exception {
try (PythonInterpreter interp = new PythonInterpreter()) {

for (Functions f : RunScript.standardLib) {
if (!f.excludeLanguages.contains("jython.py")) {
interp.set(f.libName, f);
}
}
interp.set(consumerFix.libName, consumerFix);

if (globals != null) for (Map.Entry<String, Object> e : globals.entrySet()) {
interp.set(e.getKey(), e.getValue());
}

interp.exec(script);
} catch (Exception e) {
throw e;
}

}

@Override
public String extension() {
return hasJEP ? "jython.py" : ".py";
}

});

RunScript.sortLanguages();
Expand Down

0 comments on commit a400894

Please sign in to comment.