-
-
Notifications
You must be signed in to change notification settings - Fork 435
Expand file tree
/
Copy pathEffScriptFile.java
More file actions
216 lines (195 loc) · 6.92 KB
/
EffScriptFile.java
File metadata and controls
216 lines (195 loc) · 6.92 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
package ch.njol.skript.effects;
import ch.njol.skript.ScriptLoader;
import ch.njol.skript.Skript;
import ch.njol.skript.SkriptCommand;
import ch.njol.skript.command.ScriptCommand;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.localization.Language;
import ch.njol.skript.localization.PluralizingArgsMessage;
import ch.njol.skript.log.LogEntry;
import ch.njol.skript.log.LogHandler;
import ch.njol.skript.log.RedirectingLogHandler;
import ch.njol.skript.log.RetainingLogHandler;
import ch.njol.skript.log.TimingLogHandler;
import ch.njol.skript.registrations.Feature;
import ch.njol.skript.util.Utils;
import ch.njol.util.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;
import org.skriptlang.skript.lang.script.Script;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.util.FileUtils;
import ch.njol.util.Kleenean;
import ch.njol.util.OpenCloseable;
import org.bukkit.event.Event;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.stream.Collectors;
@Name("Enable/Disable/Unload/Reload Script")
@Description("""
Enables, disables, unloads, or reloads a script.
Disabling a script unloads it and prepends - to its name so it will not be loaded the next time the server restarts.
If the script reflection experiment is enabled: unloading a script terminates it and removes it from memory, but does not alter the file.""")
@Examples({
"reload script \"test\"",
"enable script file \"testing\"",
"unload script file \"script.sk\"",
"set {_script} to the script \"MyScript.sk\"",
"reload {_script}"
})
@Since("2.4, 2.10 (unloading)")
public class EffScriptFile extends Effect {
static {
Skript.registerEffect(EffScriptFile.class,
"(1:(enable|load)|2:reload|3:disable|4:unload) script [file|named] %string% [print:with errors]",
"(1:(enable|load)|2:reload|3:disable|4:unload) skript file %string% [print:with errors]",
"(1:(enable|load)|2:reload|3:disable|4:unload) %scripts% [print:with errors]"
);
/*
The string-pattern must come first (since otherwise `script X` would match the expression)
and we cannot get a script object for a non-loaded script.
*/
}
private static final int ENABLE = 1, RELOAD = 2, DISABLE = 3, UNLOAD = 4;
private int mark;
private @UnknownNullability Expression<String> scriptNameExpression;
private @UnknownNullability Expression<Script> scriptExpression;
private boolean scripts, hasReflection, printErrors;
@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
this.mark = parseResult.mark;
printErrors = parseResult.hasTag("print");
switch (matchedPattern) {
case 0, 1:
this.scriptNameExpression = (Expression<String>) exprs[0];
break;
case 2:
this.scriptExpression = (Expression<Script>) exprs[0];
this.scripts = true;
}
this.hasReflection = this.getParser().hasExperiment(Feature.SCRIPT_REFLECTION);
return true;
}
@Override
protected void execute(Event event) {
Set<CommandSender> recipients = new HashSet<>();
if (printErrors) {
recipients.addAll(Bukkit.getOnlinePlayers().stream()
.filter(player -> player.hasPermission("skript.reloadnotify"))
.collect(Collectors.toSet()));
}
RedirectingLogHandler logHandler = new RedirectingLogHandler(recipients, "").start();
if (scripts) {
for (Script script : scriptExpression.getArray(event)) {
@Nullable File file = script.getConfig().getFile();
this.handle(file, script.getConfig().getFileName(), logHandler);
}
} else {
String name = scriptNameExpression.getSingle(event);
if (name != null)
this.handle(ScriptLoader.getScriptFromName(name), name, logHandler);
}
logHandler.close();
}
private void handle(@Nullable File scriptFile, @Nullable String name, OpenCloseable openCloseable) {
if (scriptFile == null || !scriptFile.exists())
return;
if (name == null)
name = scriptFile.getName();
FileFilter filter = ScriptLoader.getDisabledScriptsFilter();
switch (mark) {
case ENABLE:
if (ScriptLoader.getLoadedScripts().contains(ScriptLoader.getScript(scriptFile)))
return;
if (filter.accept(scriptFile)) {
try {
// TODO Central methods to be used between here and SkriptCommand should be created for
// enabling/disabling (renaming) files
scriptFile = FileUtils.move(
scriptFile,
new File(scriptFile.getParentFile(), scriptFile.getName()
.substring(ScriptLoader.DISABLED_SCRIPT_PREFIX_LENGTH)),
false
);
} catch (IOException ex) {
//noinspection ThrowableNotThrown
Skript.exception(ex, "Error while enabling script file: " + name);
return;
}
}
ScriptLoader.loadScripts(scriptFile, openCloseable);
break;
case RELOAD:
if (filter.accept(scriptFile))
return;
this.unloadScripts(scriptFile);
ScriptLoader.loadScripts(scriptFile, openCloseable);
break;
case UNLOAD:
if (hasReflection) { // if we don't use the new features this falls through into DISABLE
if (!ScriptLoader.getLoadedScriptsFilter().accept(scriptFile))
return;
this.unloadScripts(scriptFile);
break;
}
case DISABLE:
if (filter.accept(scriptFile))
return;
this.unloadScripts(scriptFile);
try {
FileUtils.move(
scriptFile,
new File(scriptFile.getParentFile(), ScriptLoader.DISABLED_SCRIPT_PREFIX + scriptFile.getName()),
false
);
} catch (IOException ex) {
//noinspection ThrowableNotThrown
Skript.exception(ex, "Error while disabling script file: " + name);
return;
}
break;
default:
assert false;
}
}
private void unloadScripts(File file) {
Set<Script> loaded = ScriptLoader.getLoadedScripts();
if (file.isDirectory()) {
Set<Script> scripts = ScriptLoader.getScripts(file);
if (scripts.isEmpty())
return;
scripts.retainAll(loaded); // skip any that are not loaded (avoid throwing error)
ScriptLoader.unloadScripts(scripts);
} else {
Script script = ScriptLoader.getScript(file);
if (!loaded.contains(script))
return; // don't need to unload if not loaded (avoid throwing error)
if (script != null)
ScriptLoader.unloadScript(script);
}
}
@Override
public String toString(@Nullable Event event, boolean debug) {
String start = switch (mark) {
case ENABLE -> "enable";
case DISABLE -> "disable";
case RELOAD -> "reload";
default -> "unload";
} + " ";
if (scripts)
return start + scriptExpression.toString(event, debug);
return start + "script file " + scriptNameExpression.toString(event, debug);
}
}