11package 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+
313import org .bukkit .plugin .java .JavaPlugin ;
414
515import 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