diff --git a/src/main/java/ch/njol/skript/ScriptLoader.java b/src/main/java/ch/njol/skript/ScriptLoader.java index b269e5142fb..7759a02563d 100644 --- a/src/main/java/ch/njol/skript/ScriptLoader.java +++ b/src/main/java/ch/njol/skript/ScriptLoader.java @@ -246,8 +246,9 @@ public String toString() { // Load scripts in separate (one) thread static final BlockingQueue loadQueue = new ArrayBlockingQueue<>(20, true); - static final Thread loaderThread; - static boolean loadAsync; // See below + @Nullable + static Thread loaderThread; + private static boolean loadAsync; // See below /** * Checks if scripts are loaded in separate thread. If true, @@ -309,23 +310,32 @@ private static void updateDisabledScripts(Path path) { } } - // Initialize and start load thread - static { - loaderThread = new AsyncLoaderThread(); - loaderThread.start(); + static void setLoadAsync(boolean loadAsync) { + ScriptLoader.loadAsync = loadAsync; + + // Shutdown old thread in case of config reload + if (loaderThread != null) { + loaderThread.interrupt(); + } + + if (ScriptLoader.loadAsync) { + // Initialize and start load thread + loaderThread = new Thread(new AsyncLoaderTask(), "Skript async script loader thread"); + loaderThread.start(); + } } - private static class AsyncLoaderThread extends Thread { + private static class AsyncLoaderTask implements Runnable { - public AsyncLoaderThread() { } + public AsyncLoaderTask() { } @Override public void run() { - while (true) { + while (!Thread.currentThread().isInterrupted()) { try { loadQueue.take().run(); } catch (InterruptedException e) { - Skript.exception(e); // Bubble it up with instructions on how to report it + Thread.currentThread().interrupt(); } } } diff --git a/src/main/java/ch/njol/skript/Skript.java b/src/main/java/ch/njol/skript/Skript.java index 6e579add513..e02192835af 100644 --- a/src/main/java/ch/njol/skript/Skript.java +++ b/src/main/java/ch/njol/skript/Skript.java @@ -978,7 +978,7 @@ static void disableScripts() { * Prints errors from reloading the config & scripts */ static void reload() { - if (!ScriptLoader.loadAsync) + if (!ScriptLoader.isAsync()) disableScripts(); reloadMainConfig(); reloadAliases(); @@ -989,7 +989,7 @@ static void reload() { * Prints errors */ static void reloadScripts() { - if (!ScriptLoader.loadAsync) + if (!ScriptLoader.isAsync()) disableScripts(); ScriptLoader.loadScripts(); } diff --git a/src/main/java/ch/njol/skript/SkriptConfig.java b/src/main/java/ch/njol/skript/SkriptConfig.java index b93ab51e490..864b2235fe4 100644 --- a/src/main/java/ch/njol/skript/SkriptConfig.java +++ b/src/main/java/ch/njol/skript/SkriptConfig.java @@ -305,7 +305,7 @@ public void set(Boolean t) { @Override public void set(Boolean t) { - ScriptLoader.loadAsync = t; + ScriptLoader.setLoadAsync(t); } }) diff --git a/src/main/resources/config.sk b/src/main/resources/config.sk index 92a6e897126..cdbbd3f7b50 100644 --- a/src/main/resources/config.sk +++ b/src/main/resources/config.sk @@ -186,6 +186,12 @@ keep command last usage dates: false # but when the player uses the command again after the cooldown period is over, the last usage will be deleted as it's no longer needed, # If you need to use the expression 'last usage date', then you'll want to enable this. +asynchronous script loading: false +# Enables asynchronous script loading, which will load scripts in the background instead of blocking the server to load scripts. +# This will not necessarily make loading faster since it will still load the scripts on one thread and not in parallel. +# Do note that though, this option may cause issues with addons and possibly some scripts! Do NOT enable this option unless you have really long +# script load times AND you take the risk of lost data and full responsibility! + # ==== Variables ==== databases: