Class ScriptLoader
java.lang.Object
ch.njol.skript.ScriptLoader
The main class for loading, unloading and reloading scripts.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceUsed for listening to events involving a ScriptLoader.static classA class for keeping track of the general content of a script: The amount of files The amount of structuresstatic interfaceCalled when aScriptis created and preloaded in theScriptLoader.static interfaceCalled when aScriptis loaded in theScriptLoader.static interfacestatic interfaceCalled when aScriptis unloaded in theScriptLoader. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ScriptcreateDummyScript(String name, @Nullable File file) Creates a Script object for a file (or resource) that may (or may not) exist.static FileFilterstatic @UnknownNullability ExecutorReturns the executor used for submitting tasks based on the user config.sk settings.static @Nullable ScriptgetLoadedScriptFromName(@Nullable String name) Searches the loaded scripts for the one with the provided name.static FileFilterstatic @Nullable ScriptSearches through the loaded scripts to find the script loaded from the provided file.static @Nullable FilegetScriptFileFromName(String script) Gets a script's file from its name, if one exists.static @Nullable FilegetScriptFileFromName(String script, File directory) Gets a script's file from its name and directory, if one exists.static @Nullable FilegetScriptFromName(String script) Deprecated, for removal: This API element is subject to removal in a future version.static @Nullable FilegetScriptFromName(String script, File directory) Deprecated, for removal: This API element is subject to removal in a future version.UsegetScriptFileFromName(String, File)instead.getScripts(File directory) Searches through the loaded scripts to find all scripts loaded from the files contained within the provided directory.static booleanisAsync()Checks if scripts are loaded in separate thread.static booleanChecks if scripts are loaded in multiple threads instead of one thread.static ArrayList<TriggerItem> loadItems(SectionNode node) Loads a section by converting it toTriggerItems.loadScripts(File file, OpenCloseable openCloseable) Loads the Script present at the file usingloadScripts(List, OpenCloseable), sending info/error messages when done.loadScripts(Set<File> files, OpenCloseable openCloseable) Loads the Scripts present at the files usingloadScripts(List, OpenCloseable), sending info/error messages when done.reloadScript(Script script, OpenCloseable openCloseable) Reloads a single Script.reloadScripts(Set<Script> scripts, OpenCloseable openCloseable) Reloads all provided Scripts.static StringreplaceOptions(String string) Replaces options in a string.static voidsetAsyncLoaderSize(int size) Sets the amount of async loaders, by updatingasyncLoaderSizeandloaderThreads.static ScriptLoader.ScriptInfounloadScript(Script script) Unloads the provided script.static ScriptLoader.ScriptInfounloadScripts(Set<Script> scripts) Unloads all scripts present in the provided collection.
-
Field Details
-
DISABLED_SCRIPT_PREFIX
- See Also:
-
DISABLED_SCRIPT_PREFIX_LENGTH
public static final int DISABLED_SCRIPT_PREFIX_LENGTH
-
-
Constructor Details
-
ScriptLoader
public ScriptLoader()
-
-
Method Details
-
getScript
Searches through the loaded scripts to find the script loaded from the provided file.- Parameters:
file- The file containing the script to find. Must not be a directory.- Returns:
- The script loaded from the provided file, or null if no script was found.
-
getScripts
Searches through the loaded scripts to find all scripts loaded from the files contained within the provided directory.- Parameters:
directory- The directory containing scripts to find.- Returns:
- The scripts loaded from the files of the provided directory. Empty if no scripts were found.
-
isAsync
public static boolean isAsync()Checks if scripts are loaded in separate thread. If true, following behavior should be expected:- Scripts are still unloaded and enabled in server thread
- When reloading a script, old version is unloaded after it has been parsed, immediately before it has been loaded
- When reloading all scripts, scripts that were removed are disabled after everything has been reloaded
- Script infos returned by most methods are inaccurate
- Returns:
- If main thread is not blocked when loading.
-
isParallel
public static boolean isParallel()Checks if scripts are loaded in multiple threads instead of one thread. If true,isAsync()will also be true.- Returns:
- if parallel loading is enabled.
-
getExecutor
Returns the executor used for submitting tasks based on the user config.sk settings. The thread count will be based on the value ofasyncLoaderSize.You may also use class
Taskand the appropriate constructor to run tasks on the script loader executor.- Returns:
- the executor used for submitting tasks. Can be null if called before Skript loads config.sk
-
setAsyncLoaderSize
Sets the amount of async loaders, by updatingasyncLoaderSizeandloaderThreads.
Ifsize <= 0, async and parallel loading are disabled.
Ifsize == 1, async loading is enabled but parallel loading is disabled.
Ifsize >= 2, async and parallel loading are enabled.- Parameters:
size- the amount of async loaders to use.- Throws:
IllegalStateException
-
loadScripts
public static CompletableFuture<ScriptLoader.ScriptInfo> loadScripts(File file, OpenCloseable openCloseable) Loads the Script present at the file usingloadScripts(List, OpenCloseable), sending info/error messages when done.- Parameters:
file- The file to load. If this is a directory, all scripts within the directory and any subdirectories will be loaded.openCloseable- AnOpenCloseablethat will be called before and after each individual script load (seemakeFuture(Supplier, OpenCloseable)).
-
loadScripts
public static CompletableFuture<ScriptLoader.ScriptInfo> loadScripts(Set<File> files, OpenCloseable openCloseable) Loads the Scripts present at the files usingloadScripts(List, OpenCloseable), sending info/error messages when done.- Parameters:
files- The files to load. If any file is a directory, all scripts within the directory and any subdirectories will be loaded.openCloseable- AnOpenCloseablethat will be called before and after each individual script load (seemakeFuture(Supplier, OpenCloseable)).
-
unloadScripts
Unloads all scripts present in the provided collection.- Parameters:
scripts- The scripts to unload.- Returns:
- Combined statistics for the unloaded scripts.
This data is calculated by using
ScriptLoader.ScriptInfo.add(ScriptInfo).
-
unloadScript
Unloads the provided script.- Parameters:
script- The script to unload.- Returns:
- Statistics for the unloaded script.
-
reloadScript
public static CompletableFuture<ScriptLoader.ScriptInfo> reloadScript(Script script, OpenCloseable openCloseable) Reloads a single Script.- Parameters:
script- The Script to reload.- Returns:
- Info on the loaded Script.
-
reloadScripts
public static CompletableFuture<ScriptLoader.ScriptInfo> reloadScripts(Set<Script> scripts, OpenCloseable openCloseable) Reloads all provided Scripts.- Parameters:
scripts- The Scripts to reload.openCloseable- AnOpenCloseablethat will be called before and after each individual Script load (seemakeFuture(Supplier, OpenCloseable)).- Returns:
- Info on the loaded Scripts.
-
replaceOptions
-
loadItems
Loads a section by converting it toTriggerItems. -
createDummyScript
Creates a Script object for a file (or resource) that may (or may not) exist. This is used for providing handles for disabled scripts.
This does not load (or parse or open or do anything to) the given file.- Returns:
- An unlinked, empty script object with an empty backing config
-
getLoadedScripts
-
getDisabledScripts
-
getLoadedScriptsFilter
- Returns:
- A FileFilter defining the naming conditions of a loaded script.
-
getDisabledScriptsFilter
- Returns:
- A FileFilter defining the naming conditions of a disabled script.
-
eventRegistry
- Returns:
- An EventRegistry for the ScriptLoader's events.
-
getScriptFileFromName
-
getScriptFromName
@Deprecated(since="2.17.0", forRemoval=true) @Nullable public static @Nullable File getScriptFromName(String script) Deprecated, for removal: This API element is subject to removal in a future version.UsegetScriptFileFromName(String)instead.Gets a script's file from its name, if one exists. This is simply a file lookup, and does not care if the script is loaded or not.- Parameters:
script- The script name/path- Returns:
- The script file, if one is found
-
getScriptFileFromName
Gets a script's file from its name and directory, if one exists. This is simply a file lookup, and does not care if the script is loaded or not.- Parameters:
script- The script name/pathdirectory- The scripts (or testing scripts) directory- Returns:
- The script file, if one is found
-
getScriptFromName
@Deprecated(since="2.17.0", forRemoval=true) @Nullable public static @Nullable File getScriptFromName(String script, File directory) Deprecated, for removal: This API element is subject to removal in a future version.UsegetScriptFileFromName(String, File)instead.Gets a script's file from its name and directory, if one exists. This is simply a file lookup, and does not care if the script is loaded or not.- Parameters:
script- The script name/pathdirectory- The scripts (or testing scripts) directory- Returns:
- The script file, if one is found
-
getLoadedScriptFromName
Searches the loaded scripts for the one with the provided name.Unlike
getScriptFileFromName(String)this never touches the filesystem, but it can only find scripts that are currently loaded. Prefer this method when aScriptis what you need, as the file-based lookup requires several filesystem operations.- Parameters:
name- The script's name, relative to the scripts folder, with or without the '.sk' extension. Both '/' and '\' are accepted as separators, regardless of OS.- Returns:
- The loaded script with the provided name, or null if no such script is loaded.
-
getScriptFileFromName(String)instead.