-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSkript.java
More file actions
242 lines (204 loc) · 7.67 KB
/
Skript.java
File metadata and controls
242 lines (204 loc) · 7.67 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package com.github.skriptdev.skript.plugin;
import com.github.skriptdev.skript.api.skript.ErrorHandler;
import com.github.skriptdev.skript.api.skript.ScriptsLoader;
import com.github.skriptdev.skript.api.skript.addon.AddonLoader;
import com.github.skriptdev.skript.api.skript.addon.HySkriptAddon;
import com.github.skriptdev.skript.api.skript.command.ArgUtils;
import com.github.skriptdev.skript.api.skript.config.SkriptConfig;
import com.github.skriptdev.skript.api.skript.registration.SkriptRegistration;
import com.github.skriptdev.skript.api.skript.testing.TestProperties;
import com.github.skriptdev.skript.api.skript.testing.TestRunner;
import com.github.skriptdev.skript.api.skript.variables.JsonVariableStorage;
import com.github.skriptdev.skript.api.utils.ReflectionUtils;
import com.github.skriptdev.skript.api.utils.Utils;
import com.github.skriptdev.skript.plugin.elements.ElementRegistration;
import com.github.skriptdev.skript.plugin.elements.events.EventHandler;
import com.github.skriptdev.skript.plugin.elements.events.server.EvtBoot;
import com.hypixel.hytale.server.core.event.events.BootEvent;
import com.hypixel.hytale.server.core.plugin.JavaPlugin;
import io.github.syst3ms.skriptparser.config.Config.ConfigSection;
import io.github.syst3ms.skriptparser.lang.TriggerMap;
import io.github.syst3ms.skriptparser.log.LogEntry;
import io.github.syst3ms.skriptparser.log.SkriptLogger;
import io.github.syst3ms.skriptparser.registration.SkriptAddon;
import io.github.syst3ms.skriptparser.variables.Variables;
import org.jetbrains.annotations.NotNull;
import java.nio.file.Path;
/**
* Main class for the Skript aspects of HySkript.
*/
@SuppressWarnings("unused")
public class Skript extends SkriptAddon {
public static Skript INSTANCE;
private final HySk hySk;
private final AddonLoader addonLoader;
private final Path scriptsPath;
private SkriptConfig skriptConfig;
private SkriptRegistration registration;
private ElementRegistration elementRegistration;
private ScriptsLoader scriptsLoader;
private boolean isAcceptingRegistrations = true;
Skript(HySk hySk) {
super("HySkript");
INSTANCE = this;
this.hySk = hySk;
this.addonLoader = new AddonLoader();
this.scriptsPath = hySk.getDataDirectory().resolve("scripts");
}
void start() {
// Stop accepting registrations after Skript starts to load
this.isAcceptingRegistrations = false;
long start = System.currentTimeMillis();
Utils.log(" ");
Utils.log("Setting up HySkript!");
Utils.log(" ");
// LOAD CONFIG
this.skriptConfig = new SkriptConfig(this);
// SETUP SKRIPT
setupSkript();
// ALL DONE
Utils.log(" ");
long fin = System.currentTimeMillis() - start;
Utils.log("HySkript loading completed in %sms!", fin);
Utils.log(" ");
}
private void setupSkript() {
long start = System.currentTimeMillis();
// INITIALIZE UTILITIES
Utils.debug("Initializing utilities...");
ReflectionUtils.init();
ArgUtils.init();
// SETUP REGISTRATION
Utils.debug("Setting up registration...");
this.registration = new SkriptRegistration(this);
this.elementRegistration = new ElementRegistration(this.registration);
// REGISTER ELEMENTS
Utils.debug("Registering elements...");
this.elementRegistration.registerElements();
// FINISH SETUP
long fin = System.currentTimeMillis() - start;
Utils.log("HySkript setup completed in %sms!", fin);
// LOAD ADDONS
this.addonLoader.loadAddons();
// SETUP ERROR HANDLER
Utils.debug("Setting up error handler...");
ErrorHandler.init();
// LOAD VARIABLES
loadVariables();
// LOAD SCRIPTS
this.scriptsLoader = new ScriptsLoader(this);
this.scriptsLoader.loadScripts(null, this.scriptsPath, false);
// FINALIZE SCRIPT LOADING
this.hySk.getEventRegistry().register(BootEvent.class, event -> {
Utils.debug("Hytale finished booting, starting post-load triggers...");
// Fire boot event
TriggerMap.callTriggersByContext(new EvtBoot.BootContext(event));
// Start any post-load triggers after Hytale finishes booting.
getAddons().forEach(SkriptAddon::finishedLoading);
// RUN TESTS
if (TestProperties.ENABLED) {
TestRunner testRunner = new TestRunner();
testRunner.start();
}
});
}
public void shutdown() {
// SHUTDOWN LISTENERS
EventHandler.shutdown();
// SHUTDOWN SCRIPTS
this.scriptsLoader.shutdown();
// SHUTDOWN VARIABLES
Utils.log("Saving variables...");
Variables.shutdown();
Utils.log("Variable saving complete!");
// SHUTDOWN ADDONS
this.addonLoader.shutdownAddons();
}
private void loadVariables() {
long start = System.currentTimeMillis();
Utils.log("Loading variables...");
Variables.registerStorage(JsonVariableStorage.class, "json-database");
ConfigSection databases = this.skriptConfig.getDatabases();
if (databases == null) {
return;
}
SkriptLogger skriptLogger = new SkriptLogger(true);
Variables.load(skriptLogger, databases);
skriptLogger.finalizeLogs();
for (LogEntry logEntry : skriptLogger.close()) {
Utils.log(null, logEntry);
}
long fin = System.currentTimeMillis() - start;
Utils.log("Finished loading variables in %sms!", fin);
}
/**
* Get the instance of the HySkript plugin.
*
* @return The instance of HySkript.
*/
public @NotNull HySk getPlugin() {
return this.hySk;
}
/**
* Get the Skript configuration.
*
* @return The Skript configuration.
*/
public @NotNull SkriptConfig getSkriptConfig() {
return this.skriptConfig;
}
/**
* Get the path where scripts are stored.
*
* @return The path to the scripts directory.
*/
public @NotNull Path getScriptsPath() {
return this.scriptsPath;
}
/**
* Get the registration for Skript elements.
*
* @return The Skript registration.
*/
public @NotNull SkriptRegistration getSkriptRegistration() {
return this.registration;
}
/**
* Check if HySkript is accepting registrations.
*
* @return True if HySkript is accepting registrations, false otherwise.
*/
public boolean isAcceptingRegistrations() {
return this.isAcceptingRegistrations;
}
/**
* Register an addon to HySkript.
* <br>ONLY use this when registering a plugin/mod as an addon.
* <br>Regular addons are self registering.
*
* @param plugin Main plugin instance
* @param addonClass Class of the addon
* @param <T> Type of the addon
* @return New instance of the addon
*/
public <T extends HySkriptAddon> T registerAddon(@NotNull JavaPlugin plugin, @NotNull Class<T> addonClass) {
if (!this.isAcceptingRegistrations) {
throw new IllegalStateException("Cannot register addons after HySkript has started!");
}
return this.addonLoader.registerAddon(plugin, addonClass);
}
public ElementRegistration getElementRegistration() {
return this.elementRegistration;
}
public ScriptsLoader getScriptsLoader() {
return this.scriptsLoader;
}
/**
* Get an instance of Skript.
*
* @return Instance of Skript.
*/
public static Skript getInstance() {
return INSTANCE;
}
}