4545 */
4646public final class ScriptBootstrap extends JavaPlugin {
4747
48+ private static ScriptBootstrap plugin ;
49+ private Map <String , Object > scripts ;
50+ private ScriptEngine jsEngine ;
4851 private final Map <String , HandledExecutor > executor = new HashMap <>();
49- private Map <String , Object > plugin ;
50- private ScriptLoader loader ;
52+ private ScriptLoader scriptLoader ;
5153 private Unsafe unsafe ;
52- private final ThreadLocal <ScriptEngine > jsEngine = ThreadLocal .withInitial (() -> new ScriptEngineManager ().getEngineByExtension ("js" ));
53- private static ScriptBootstrap instance ;
5454
5555 public static ScriptBootstrap get () {
56- return instance ;
56+ return plugin ;
5757 }
5858
5959 @ SneakyThrows
60- public static Object require (ScriptEngine ctx , File jsFile ) {
60+ public static Object require (File jsFile ) {
61+ ScriptEngine ctx = jsEngine ();
6162 Bindings bindings = ctx .createBindings ();
6263 ctx .eval ("exports = {}" , bindings );
6364 ctx .eval (Files .newReader (jsFile , StandardCharsets .UTF_8 ), bindings );
6465 return ctx .eval ("exports" , bindings );
6566 }
6667
67- public ScriptEngine jsEngine () {
68- return jsEngine . get () ;
68+ public static ScriptEngine jsEngine () {
69+ return plugin . jsEngine ;
6970 }
7071
7172 @ Override
72- public void onEnable () {
73- instance = this ;
74-
75- loader = new ScriptLoader ( this );
73+ public void onLoad () {
74+ plugin = this ;
75+ jsEngine = new ScriptEngineManager ( getClassLoader ()). getEngineByExtension ( "js" );
76+ }
7677
78+ @ Override
79+ public void onEnable () {
80+ scriptLoader = new ScriptLoader ();
7781 getServer ().getConsoleSender ().sendMessage (ArrayHelper .toArray (
7882 ChatColor .GREEN + "梦梦家高性能服务器出租店" ,
7983 ChatColor .GREEN + "shop105595113.taobao.com" ));
@@ -103,14 +107,14 @@ protected void reload() {
103107 @ Override
104108 @ SneakyThrows
105109 public void onDisable () {
106- for (Map .Entry <String , Object > i : new HashMap <>(plugin ).entrySet ()) {
110+ for (Map .Entry <String , Object > i : new HashMap <>(scripts ).entrySet ()) {
107111 ((Closeable ) i .getValue ()).close ();
108112 }
109- plugin = null ;
113+ scripts = null ;
110114 }
111115
112116 private void loadAll () {
113- plugin = new HashMap <>();
117+ scripts = new HashMap <>();
114118 for (File obj : getDataFolder ().listFiles ()) {
115119 if (obj .isFile () && obj .getName ().matches (".+\\ .js" )) {
116120 try {
@@ -126,10 +130,10 @@ private void loadAll() {
126130
127131 private void loadEx (File obj ) {
128132 ScriptingLoader scripting = new ScriptingLoader (obj );
129- if (plugin .containsKey (scripting .getName ())) {
130- getLogger ().warning (String .format ("!!! name conflict between %s and %s" , scripting .getId (), ((Named ) plugin .get (scripting .getName ())).getId ()));
133+ if (scripts .containsKey (scripting .getName ())) {
134+ getLogger ().warning (String .format ("!!! name conflict between %s and %s" , scripting .getId (), ((Named ) scripts .get (scripting .getName ())).getId ()));
131135 } else {
132- plugin .put (scripting .getName (), scripting );
136+ scripts .put (scripting .getName (), scripting );
133137 Bukkit .getPluginManager ().enablePlugin (scripting );
134138 }
135139 }
@@ -158,15 +162,15 @@ private boolean isLoaded(File file) {
158162 }
159163
160164 private void load (ScriptLoader .ScriptInfo info ) throws ScriptPluginException {
161- ScriptLoader .ScriptBinding binding = loader .load (info );
165+ ScriptLoader .ScriptBinding binding = scriptLoader .load (info );
162166 ScriptPlugin loaded = binding .getPlugin ();
163167 if (loaded .isHandled () && !loaded .isIdled ()) {
164168 String name = loaded .getDescription ("name" );
165- Named i = (Named ) plugin .get (name );
169+ Named i = (Named ) scripts .get (name );
166170 if (!nil (i )) {
167171 ScriptPluginException .thr (loaded , "Name conflict with " + i .getId ());
168172 }
169- plugin .put (name , binding );
173+ scripts .put (name , binding );
170174 }
171175 }
172176
@@ -175,7 +179,7 @@ public static boolean nil(Object i) {
175179 }
176180
177181 Named lookById (String id ) {
178- for (Object obj : plugin .values ()) {
182+ for (Object obj : scripts .values ()) {
179183 Named named = (Named ) obj ;
180184 if (named .getId ().equals (id )) {
181185 return named ;
@@ -185,7 +189,7 @@ Named lookById(String id) {
185189 }
186190
187191 public ImmutableList <String > list () {
188- return ImmutableList .copyOf (plugin .keySet ());
192+ return ImmutableList .copyOf (scripts .keySet ());
189193 }
190194
191195 @ SuppressWarnings ("unchecked" )
@@ -232,29 +236,29 @@ protected boolean remove(HandledExecutor handled) {
232236 boolean unload (ScriptPlugin i ) {
233237 String id = i .getDescription ("name" );
234238 if (nil (id )) return false ;
235- Object obj = plugin .get (id );
239+ Object obj = scripts .get (id );
236240 ScriptLoader .ScriptBinding binding = obj instanceof ScriptLoader .ScriptBinding ? (ScriptLoader .ScriptBinding ) obj : null ;
237- return !nil (binding ) && binding .getPlugin () == i && plugin .remove (id , binding );
241+ return !nil (binding ) && binding .getPlugin () == i && scripts .remove (id , binding );
238242 }
239243
240244 public void unload (ScriptingLoader scripting ) {
241- if (plugin .remove (scripting .getName (), scripting )) {
245+ if (scripts .remove (scripting .getName (), scripting )) {
242246 Bukkit .getPluginManager ().disablePlugin (scripting );
243247 }
244248 }
245249
246250 @ SneakyThrows
247251 boolean unload (String id ) {
248- if (plugin .containsKey (id )) {
249- ((Closeable ) plugin .get (id )).close ();
252+ if (scripts .containsKey (id )) {
253+ ((Closeable ) scripts .get (id )).close ();
250254 return true ;
251255 }
252256 val binding = getSBinding (id );
253257 return !nil (binding ) && binding .getPlugin ().unload ();
254258 }
255259
256260 public ScriptLoader .ScriptBinding getSBinding (String name ) {
257- Object binding = plugin .get (name );
261+ Object binding = scripts .get (name );
258262 if (nil (binding ) && name .startsWith ("file:" )) {
259263 binding = lookById (name );
260264 }
@@ -282,9 +286,9 @@ public Plugin getPlugin(String id) {
282286 return main .getServer ().getPluginManager ().getPlugin (id );
283287 }
284288
285- public ScriptEngine getScript (String id ) {
289+ public Object getScript (String id ) {
286290 ScriptLoader .ScriptBinding binding = main .getSBinding (id );
287- return !nil (binding ) ? binding .getEngine () : null ;
291+ return !nil (binding ) ? binding .getScriptObj () : null ;
288292 }
289293
290294 public BossBar createBossBar (String text ) {
0 commit comments