ScriptsLab
WikiDownloadsSourcesSupport
ScriptsLab
DocumentationDownloadsGitHubDiscord

© 2026 ScriptsLab

Back to src/main/java/com/scriptslab/api/script
J

ScriptEngine.java

Java · 168 lines · 3.8 KB

src/main/java/com/scriptslab/api/script/ScriptEngine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.scriptslab.api.script;

import java.nio.file.Path;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

/**
 * Script engine for executing JavaScript code.
 * Thread-safe.
 */
public interface ScriptEngine {
    
    /**
     * Initializes the script engine.
     * 
     * @return future that completes when initialized
     */
    CompletableFuture<Void> initialize();
    
    /**
     * Initializes the script engine synchronously (blocking).
     * Must be called from the main server thread.
     */
    void initializeSync();
    
    /**
     * Shuts down the script engine.
     * 
     * @return future that completes when shut down
     */
    CompletableFuture<Void> shutdown();
    
    /**
     * Loads a script from a file.
     * 
     * @param scriptPath path to script file
     * @return future with loaded script
     */
    CompletableFuture<LoadedScript> loadScript(Path scriptPath);
    
    /**
     * Loads all scripts from a directory.
     * 
     * @param directory directory containing scripts
     * @return future with number of loaded scripts
     */
    CompletableFuture<Integer> loadScriptsFromDirectory(Path directory);
    
    /**
     * Executes JavaScript code.
     * 
     * @param code JavaScript code to execute
     * @return future with execution result
     */
    CompletableFuture<Object> execute(String code);
    
    /**
     * Executes a loaded script.
     * 
     * @param scriptId script identifier
     * @return future with execution result
     */
    CompletableFuture<Object> executeScript(String scriptId);
    
    /**
     * Reloads a script.
     * 
     * @param scriptId script identifier
     * @return future that completes when reloaded
     */
    CompletableFuture<Void> reloadScript(String scriptId);
    
    /**
     * Reloads all scripts.
     * 
     * @return future with number of reloaded scripts
     */
    CompletableFuture<Integer> reloadAllScripts();
    
    /**
     * Unloads a script.
     * 
     * @param scriptId script identifier
     * @return future that completes when unloaded
     */
    CompletableFuture<Void> unloadScript(String scriptId);
    
    /**
     * Gets a loaded script.
     * 
     * @param scriptId script identifier
     * @return optional containing script if found
     */
    Optional<LoadedScript> getScript(String scriptId);
    
    /**
     * Gets all loaded scripts.
     * 
     * @return collection of all scripts
     */
    Collection<LoadedScript> getAllScripts();
    
    /**
     * Checks if the engine is initialized.
     * 
     * @return true if initialized
     */
    boolean isInitialized();
    
    /**
     * Checks if the engine was previously loaded (before potential reload).
     * 
     * @return true if was loaded before
     */
    default boolean wasPreviouslyLoaded() {
        return false;
    }
    
    /**
     * Gets the script API object.
     * 
     * @return script API
     */
    Object getScriptAPI();
    
    /**
     * Represents a loaded script.
     */
    interface LoadedScript {
        
        /**
         * Gets the script ID.
         */
        String getId();
        
        /**
         * Gets the script file path.
         */
        Path getPath();
        
        /**
         * Gets the load timestamp.
         */
        long getLoadTime();
        
        /**
         * Gets the last execution timestamp.
         */
        long getLastExecutionTime();
        
        /**
         * Gets the number of times this script was executed.
         */
        int getExecutionCount();
        
        /**
         * Checks if the script has errors.
         */
        boolean hasErrors();
        
        /**
         * Gets the last error if any.
         */
        Optional<Throwable> getLastError();
    }
}