Package ch.njol.skript
Class ScriptLoader
java.lang.Object
ch.njol.skript.ScriptLoader
The main class for loading, unloading and reloading scripts.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Used for listening to events involving a ScriptLoader.static class
A class for keeping track of the general content of a script: The amount of files The amount of structuresstatic interface
Called when aScript
is created and preloaded in theScriptLoader
.static interface
Called when aScript
is loaded in theScriptLoader
.static interface
static interface
Called when aScript
is unloaded in theScriptLoader
. -
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Script
createDummyScript
(String name, @Nullable File file) Creates a Script object for a file (or resource) that may (or may not) exist.static FileFilter
static FileFilter
static @Nullable Script
Searches through the loaded scripts to find the script loaded from the provided file.static @Nullable File
getScriptFromName
(String script) Gets a script's file from its name, if one exists.getScripts
(File directory) Searches through the loaded scripts to find all scripts loaded from the files contained within the provided directory.static boolean
isAsync()
Checks if scripts are loaded in separate thread.static boolean
Checks if scripts are loaded in multiple threads instead of one thread.static ArrayList
<TriggerItem> loadItems
(SectionNode node) Loads a section by converting it toTriggerItem
s.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 String
replaceOptions
(String string) Replaces options in a string.static void
setAsyncLoaderSize
(int size) Sets the amount of async loaders, by updatingasyncLoaderSize
andloaderThreads
.static ScriptLoader.ScriptInfo
unloadScript
(Script script) Unloads the provided script.static ScriptLoader.ScriptInfo
unloadScripts
(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.
-
setAsyncLoaderSize
Sets the amount of async loaders, by updatingasyncLoaderSize
andloaderThreads
.
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
- AnOpenCloseable
that 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
- AnOpenCloseable
that 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
- AnOpenCloseable
that will be called before and after each individual Script load (seemakeFuture(Supplier, OpenCloseable)
).- Returns:
- Info on the loaded Scripts.
-
replaceOptions
Replaces options in a string. Options are obtained from aScript
'sStructOptions.OptionsData
. Example:script.getData(OptionsData.class)
-
loadItems
Loads a section by converting it toTriggerItem
s. -
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
- Returns:
- An unmodifiable set containing a snapshot of the currently loaded scripts. Any changes to loaded scripts will not be reflected in the returned set.
-
getDisabledScripts
- Returns:
- An unmodifiable set containing a snapshot of the currently disabled scripts. Any changes to disabled scripts will not be reflected in the returned set.
-
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.
-
getScriptFromName
Gets a script's file from its name, if one exists.- Parameters:
script
- The script name/path- Returns:
- The script file, if one is found
-