-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSkript.java
More file actions
217 lines (187 loc) · 7.46 KB
/
Skript.java
File metadata and controls
217 lines (187 loc) · 7.46 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
package com.github.skriptdev.skript.plugin;
import com.github.skriptdev.skript.api.skript.ScriptsLoader;
import com.github.skriptdev.skript.api.skript.addon.AddonLoader;
import com.github.skriptdev.skript.api.skript.command.ArgUtils;
import com.github.skriptdev.skript.api.skript.registration.SkriptRegistration;
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.command.EffectCommands;
import com.github.skriptdev.skript.plugin.elements.ElementRegistration;
import io.github.syst3ms.skriptparser.Parser;
import io.github.syst3ms.skriptparser.config.Config;
import io.github.syst3ms.skriptparser.config.Config.ConfigSection;
import io.github.syst3ms.skriptparser.lang.Structure;
import io.github.syst3ms.skriptparser.log.ErrorType;
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.registration.SkriptEventInfo;
import io.github.syst3ms.skriptparser.structures.functions.Functions;
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 Config skriptConfig;
private final Path scriptsPath;
private final SkriptLogger logger;
private SkriptRegistration registration;
private ElementRegistration elementRegistration;
private AddonLoader addonLoader;
private ScriptsLoader scriptsLoader;
Skript(HySk hySk) {
super("HySkript");
INSTANCE = this;
this.hySk = hySk;
this.scriptsPath = hySk.getDataDirectory().resolve("scripts");
this.logger = new SkriptLogger();
Path skriptConfigPath = hySk.getDataDirectory().resolve("config.sk");
this.skriptConfig = new Config(skriptConfigPath, "/config.sk", this.logger);
this.logger.setDebug(this.skriptConfig.getBoolean("debug"));
Utils.log("Setting up HySkript!");
setup();
this.logger.finalizeLogs();
for (LogEntry logEntry : this.logger.close()) {
Utils.log(null, logEntry);
}
Utils.log("HySkript setup complete!");
}
private void setup() {
ReflectionUtils.init();
ArgUtils.init();
this.registration = new SkriptRegistration(this);
this.elementRegistration = new ElementRegistration(this.registration);
this.elementRegistration.registerElements();
ConfigSection effectCommandSection = this.skriptConfig.getConfigSection("effect-commands");
if (effectCommandSection != null) {
if (effectCommandSection.getBoolean("enabled")) {
EffectCommands.register(this,
effectCommandSection.getString("token"),
effectCommandSection.getBoolean("allow-ops"),
effectCommandSection.getString("required-permission"));
}
}
// FINALIZE SETUP
this.registration.register();
printSyntaxCount();
Utils.log("HySkript setup complete!");
// LOAD ADDONS
this.logger.info("Loading addons...");
this.addonLoader = new AddonLoader(this.logger);
this.addonLoader.loadAddonsFromFolder();
this.logger.info("Finished loading addons!");
// LOAD VARIABLES
loadVariables();
// LOAD SCRIPTS
this.scriptsLoader = new ScriptsLoader(this);
this.scriptsLoader.loadScripts(null, this.scriptsPath, false);
// FINALIZE SCRIPT LOADING
Parser.getMainRegistration().getRegisterer().finishedLoading();
}
public void shutdown() {
Utils.log("Saving variables...");
Variables.shutdown();
Utils.log("Variable saving complete!");
// SHUTDOWN ADDONS
this.addonLoader.shutdownAddons();
}
private void printSyntaxCount() {
var mainRegistration = Parser.getMainRegistration();
int structureSize = 0;
int eventSize = 0;
for (SkriptEventInfo<?> event : this.registration.getEvents()) {
if (Structure.class.isAssignableFrom(event.getSyntaxClass())) {
structureSize++;
} else {
eventSize++;
}
}
for (SkriptEventInfo<?> event : mainRegistration.getEvents()) {
if (Structure.class.isAssignableFrom(event.getSyntaxClass())) {
structureSize++;
} else {
eventSize++;
}
}
int effectSize = this.registration.getEffects().size() + mainRegistration.getEffects().size();
int expsSize = this.registration.getExpressions().size() + mainRegistration.getExpressions().size();
int secSize = this.registration.getSections().size() + mainRegistration.getSections().size();
int typeSize = this.registration.getTypes().size() + mainRegistration.getTypes().size();
int funcSize = Functions.getAllFunctions().size();
int total = structureSize + eventSize + effectSize + expsSize + secSize + typeSize + funcSize;
Utils.log("Loaded HySkript %s elements:", total);
Utils.log("- Types: %s", typeSize);
Utils.log("- Structures: %s", structureSize);
Utils.log("- Events: %s ", eventSize);
Utils.log("- Effects: %s", effectSize);
Utils.log("- Expressions: %s", expsSize);
Utils.log("- Sections: %s", secSize);
Utils.log("- Functions: %s", funcSize);
}
private void loadVariables() {
Utils.log("Loading variables...");
Variables.registerStorage(JsonVariableStorage.class, "json-database");
ConfigSection databases = this.skriptConfig.getConfigSection("databases");
if (databases == null) {
this.logger.error("Databases section not found in config.sk", ErrorType.STRUCTURE_ERROR);
return;
}
Variables.load(this.logger, databases);
Utils.log("Finished loading variables!");
}
/**
* 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 Config 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;
}
public SkriptLogger getLogger() {
return this.logger;
}
/**
* Get the registration for Skript elements.
*
* @return The Skript registration.
*/
public @NotNull io.github.syst3ms.skriptparser.registration.SkriptRegistration getSkriptRegistration() {
return this.registration;
}
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;
}
}