Skip to content

Commit 419224d

Browse files
committed
Load JS plugins! <spigot/repcraft/js-plugins
1 parent 1bf34b7 commit 419224d

3 files changed

Lines changed: 88 additions & 5 deletions

File tree

ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This project is meant to address some issues I have with scriptcraft:
99
- Not enough standardized documentation
1010
- Not enough documented caveats
1111
- Relies on deprecated java APIs
12-
12+
- It evals every JS file within its plugin structure
1313

1414
This project is a SpigotMC API `JavaPlugin`<br/>
1515
It gets its capabilities from GraalVM, a modern polyglot virtual machine<br/>

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
<version>1.15.2-R0.1-SNAPSHOT</version>
3939
<scope>provided</scope>
4040
</dependency>
41+
42+
<dependency>
43+
<groupId>com.google.code.gson</groupId>
44+
<artifactId>gson</artifactId>
45+
<version>2.8.5</version>
46+
</dependency>
4147
</dependencies>
4248
<build>
4349
<pluginManagement>

src/main/java/com/roguecircuitry/repcraft/RepCraft.java

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package com.roguecircuitry.repcraft;
22

3+
import java.io.File;
4+
import java.io.FileFilter;
5+
import java.io.FileReader;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
8+
9+
import com.google.gson.JsonObject;
10+
import com.google.gson.JsonParser;
11+
import com.google.gson.stream.JsonReader;
12+
313
import org.bukkit.plugin.java.JavaPlugin;
414

515
import org.graalvm.polyglot.Context;
@@ -12,13 +22,80 @@ public final class RepCraft extends JavaPlugin {
1222
Context ctx;
1323
JSCommand jsc;
1424
Value jsBinding;
25+
File pluginFile, pluginsDir, jsRepCraftDir, jsPluginsDir;
26+
27+
private void critial(String msg) {
28+
System.err.println("[RepCraft][CRITICAL] " + msg);
29+
getServer().getPluginManager().disablePlugin(this);
30+
}
1531

1632
@Override
17-
public void onEnable () {
33+
public void onEnable() {
1834
System.out.println("[RepCraft] Initializing GraalVM js context!");
1935
this.ctx = Context.newBuilder("js").allowAllAccess(true).build();
2036
this.jsBinding = this.ctx.getBindings("js");
2137

38+
File jsPluginsDir = Paths.get("repcraft/js-plugins").toAbsolutePath().toFile();
39+
40+
if (!jsPluginsDir.exists()) {
41+
if (jsPluginsDir.mkdirs()) {
42+
System.out.println("[RepCraft] Created " + jsPluginsDir.getAbsolutePath());
43+
} else {
44+
this.critial("Failed to create all of the necessary dirs for <spigot>/repcraft/js-plugins");
45+
return;
46+
}
47+
}
48+
49+
File[] subdirs = jsPluginsDir.listFiles(new FileFilter() {
50+
public boolean accept(File f) {
51+
return f.isDirectory();
52+
}
53+
});
54+
55+
File pluginSubDir = null;
56+
File packageJson = null;
57+
JsonObject pkgJson = null;
58+
JsonParser parser = new JsonParser();
59+
String pkgJsonMain = null;
60+
File jsPluginFile = null;
61+
62+
for (int i = 0; i < subdirs.length; i++) {
63+
pluginSubDir = subdirs[i];
64+
65+
packageJson = new File(pluginSubDir.getAbsoluteFile() + "/package.json");
66+
try {
67+
pkgJson = parser.parse(
68+
new JsonReader(
69+
new FileReader(
70+
packageJson
71+
)
72+
)
73+
).getAsJsonObject();
74+
75+
} catch (Exception ex) {
76+
// Suppress, file doesn't exist , no biggy :)
77+
continue;
78+
}
79+
// Skip package.json that don't have "main" in them
80+
if (!pkgJson.has("main")) continue;
81+
82+
pkgJsonMain = pkgJson.get("main").getAsString();
83+
84+
jsPluginFile = new File(pluginSubDir.getAbsoluteFile() + "/" + pkgJsonMain);
85+
if (!jsPluginFile.exists()) {
86+
System.err.println("Couldn't import 'main' : " + jsPluginFile.toPath() + ", ignoring!");
87+
continue;
88+
}
89+
90+
try {
91+
this.ctx.eval("js", new String(Files.readAllBytes(jsPluginFile.toPath())));
92+
} catch (Exception e) {
93+
//Technically we already handled this, just skip this script
94+
e.printStackTrace();
95+
continue;
96+
}
97+
}
98+
2299
try {
23100
this.ctx.eval("js", "print('[js] Hello World');");
24101
} catch (Exception ex) {
@@ -31,16 +108,16 @@ public void onEnable () {
31108
this.getCommand("js").setExecutor(this.jsc);
32109
}
33110

34-
public Object eval (String js) {
111+
public Object eval(String js) {
35112
return this.ctx.eval("js", js);
36113
}
37114

38-
public void put (String key, Object value) {
115+
public void put(String key, Object value) {
39116
this.jsBinding.putMember(key, value);
40117
}
41118

42119
@Override
43-
public void onDisable () {
120+
public void onDisable() {
44121

45122
}
46123
}

0 commit comments

Comments
 (0)