diff --git a/src/main/java/ch/njol/skript/ScriptLoader.java b/src/main/java/ch/njol/skript/ScriptLoader.java index f9e194f75ea..aa64130955a 100644 --- a/src/main/java/ch/njol/skript/ScriptLoader.java +++ b/src/main/java/ch/njol/skript/ScriptLoader.java @@ -836,8 +836,6 @@ public static List loadStructures(File directory) { String name = Skript.getInstance().getDataFolder().toPath().toAbsolutePath() .resolve(Skript.SCRIPTSFOLDER).relativize(f.toPath().toAbsolutePath()).toString(); assert name != null; - Functions.clearFunctions(name); // Functions are still callable from other scripts - // We're just making it impossible to look them up return loadStructure(new FileInputStream(f), name); } catch (final IOException e) { Skript.error("Could not load " + f.getName() + ": " + ExceptionUtils.toString(e)); @@ -948,10 +946,6 @@ private static ScriptInfo unloadScripts_(final File folder) { */ public static ScriptInfo unloadScript(final File script) { final ScriptInfo r = unloadScript_(script); - String name = Skript.getInstance().getDataFolder().toPath().toAbsolutePath() - .resolve(Skript.SCRIPTSFOLDER).relativize(script.toPath().toAbsolutePath()).toString(); - assert name != null; - Functions.clearFunctions(name); Functions.validateFunctions(); return r; } @@ -964,11 +958,48 @@ private static ScriptInfo unloadScript_(final File script) { } loadedFiles.remove(script); // We just unloaded it, so... + + // Clear functions, DO NOT validate them yet + // If unloading, our caller will do this immediately after we return + // However, if reloading, new version of this script is first loaded + String name = Skript.getInstance().getDataFolder().toPath().toAbsolutePath() + .resolve(Skript.SCRIPTSFOLDER).relativize(script.toPath().toAbsolutePath()).toString(); + assert name != null; + Functions.clearFunctions(name); + return info; // Return how much we unloaded } return new ScriptInfo(); // Return that we unloaded literally nothing } + + /** + * Reloads a single script. + * @param script Script file. + * @return Statistics of the newly loaded script. + */ + public static ScriptInfo reloadScript(File script) { + if (!isAsync()) { + unloadScript_(script); + } + Config configs = loadStructure(script); + Functions.validateFunctions(); + return loadScripts(configs); + } + + /** + * Reloads all scripts in the given folder and its subfolders. + * @param folder A folder. + * @return Statistics of newly loaded scripts. + */ + public static ScriptInfo reloadScripts(File folder) { + if (!isAsync()) { + unloadScripts_(folder); + } + List configs = loadStructures(folder); + Functions.validateFunctions(); + return loadScripts(configs); + } /** * Replaces options in a string. diff --git a/src/main/java/ch/njol/skript/SkriptCommand.java b/src/main/java/ch/njol/skript/SkriptCommand.java index 54b83832d34..45b058bcad9 100644 --- a/src/main/java/ch/njol/skript/SkriptCommand.java +++ b/src/main/java/ch/njol/skript/SkriptCommand.java @@ -187,17 +187,11 @@ public boolean onCommand(final @Nullable CommandSender sender, final @Nullable C return true; } reloading(sender, "script", f.getName()); - if (!ScriptLoader.loadAsync) - ScriptLoader.unloadScript(f); - Config config = ScriptLoader.loadStructure(f); - ScriptLoader.loadScripts(config); + ScriptLoader.reloadScript(f); reloaded(sender, r, "script", f.getName()); } else { reloading(sender, "scripts in folder", f.getName()); - if (!ScriptLoader.loadAsync) - ScriptLoader.unloadScripts(f); - List configs = ScriptLoader.loadStructures(f); - final int enabled = ScriptLoader.loadScripts(configs).files; + final int enabled = ScriptLoader.reloadScripts(f).files; if (enabled == 0) info(sender, "reload.empty folder", f.getName()); else diff --git a/src/main/java/ch/njol/skript/effects/EffScriptFile.java b/src/main/java/ch/njol/skript/effects/EffScriptFile.java index 6fe4b7aa3e5..ed87bf0b3ee 100644 --- a/src/main/java/ch/njol/skript/effects/EffScriptFile.java +++ b/src/main/java/ch/njol/skript/effects/EffScriptFile.java @@ -97,10 +97,7 @@ protected void execute(Event e) { return; } - if (!ScriptLoader.isAsync()) - ScriptLoader.unloadScript(f); - Config config = ScriptLoader.loadStructure(f); - ScriptLoader.loadScripts(config); + ScriptLoader.reloadScripts(f); break; } case DISABLE: {