44import java .io .FileFilter ;
55import java .io .FileReader ;
66import java .nio .file .Paths ;
7+ import java .util .ArrayList ;
8+ import java .util .List ;
79
810import com .google .gson .JsonObject ;
911import com .google .gson .JsonParser ;
1012import com .google .gson .stream .JsonReader ;
13+ import com .roguecircuitry .repcraft .resources .DefaultResources ;
1114
15+ import org .bukkit .event .EventPriority ;
16+ import org .bukkit .plugin .RegisteredListener ;
1217import org .bukkit .plugin .java .JavaPlugin ;
1318
1419import org .graalvm .polyglot .Context ;
1520import org .graalvm .polyglot .Value ;
1621import org .graalvm .polyglot .Source ;
1722
18- /**RepCraft main Spigot plugin class
19- * Most of the core logic is in here
23+ /**
24+ * RepCraft main Spigot plugin class Most of the core logic is in here
2025 */
2126public final class RepCraft extends JavaPlugin {
2227 Context ctx ;
@@ -33,12 +38,38 @@ private void critial(String msg) {
3338
3439 @ Override
3540 public void onEnable () {
41+ // Initialize the JavaScript engine/bindings
3642 System .out .println ("[RepCraft] Initializing GraalVM js context!" );
3743 this .ctx = Context .newBuilder ("js" ).allowAllAccess (true ).build ();
3844 this .jsBinding = this .ctx .getBindings ("js" );
3945
46+ getServer ().getPluginManager ().registerEvents (new JSEvents (this ), this );
47+
48+ // Create a JSON parser so we can load package.json's for all of the plugins
4049 this .jsonParser = new JsonParser ();
4150
51+ File repcraftDir = Paths .get ("repcraft" ).toAbsolutePath ().toFile ();
52+ List <File > sources = new ArrayList <File >();
53+ if (!repcraftDir .exists ()) {
54+ if (!repcraftDir .mkdir ()) {
55+ this .critial ("Could not create <spigot>/repcomm ! This is required. Disabling." );
56+ return ;
57+ }
58+ String resFromPath = "/com/roguecircuitry/repcraft/resources/" ;
59+
60+ // Unpack necessary js
61+ File unpacked = new File (repcraftDir , "repcraft.mjs" );
62+ DefaultResources .unpackResTo (resFromPath + "repcraft.mjs" , unpacked , true );
63+ sources .add (unpacked );
64+ } else {
65+ sources .add (new File (repcraftDir , "repcraft.mjs" ));
66+ }
67+ for (File s : sources ) {
68+ if (this .loadSource (s )) {
69+ System .err .println ("Loaded " + s .getAbsolutePath ());
70+ }
71+ }
72+
4273 File jsPluginsDir = Paths .get ("repcraft/js-plugins" ).toAbsolutePath ().toFile ();
4374
4475 if (jsPluginsDir .exists ()) {
@@ -55,7 +86,7 @@ public void onEnable() {
5586 this .getCommand ("js" ).setExecutor (this .jsc );
5687 }
5788
58- public void loadPluginsFrom (File jsPluginsDir ) {
89+ public void loadPluginsFrom (File jsPluginsDir ) {
5990 File [] subdirs = jsPluginsDir .listFiles (new FileFilter () {
6091 public boolean accept (File f ) {
6192 return f .isDirectory ();
@@ -83,32 +114,33 @@ public boolean loadPlugin(File packageJson, File pluginSubDir) {
83114 File jsPluginFile ;
84115
85116 try {
86- pkgJson = this .jsonParser .parse (
87- new JsonReader (
88- new FileReader (
89- packageJson
90- )
91- )
92- ).getAsJsonObject ();
117+ pkgJson = this .jsonParser .parse (new JsonReader (new FileReader (packageJson ))).getAsJsonObject ();
93118 } catch (Exception ex ) {
94119 // Suppress, file doesn't exist , no biggy :)
95120 return false ;
96121 }
97122 // Skip package.json that don't have "main" in them
98- if (!pkgJson .has ("main" )) return false ;
123+ if (!pkgJson .has ("main" ))
124+ return false ;
99125
100126 pkgJsonMain = pkgJson .get ("main" ).getAsString ();
101127
102128 jsPluginFile = new File (pluginSubDir .getAbsoluteFile () + "/" + pkgJsonMain );
103- if (!jsPluginFile .exists ()) {
129+
130+ if (!this .loadSource (jsPluginFile )) {
104131 System .err .println ("Couldn't import 'main' : " + jsPluginFile .toPath () + ", ignoring!" );
105132 return false ;
106133 }
134+ return true ;
135+ }
136+
137+ public boolean loadSource (File source ) {
138+ if (!source .exists ())
139+ return false ;
107140 try {
108- Source src = Source .newBuilder ("js" , jsPluginFile ).build ();
141+ Source src = Source .newBuilder ("js" , source ).build ();
109142 this .ctx .eval (src );
110143 } catch (Exception e ) {
111- // Just skip this script
112144 e .printStackTrace ();
113145 return false ;
114146 }
0 commit comments