Skip to content

Commit e2d8a36

Browse files
authored
Add Option to Print Errors in Reload Script Effect (#7830)
* init commit * switched to printing instead of catching errors
1 parent ecb7b39 commit e2d8a36

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

src/main/java/ch/njol/skript/effects/EffScriptFile.java

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@
22

33
import ch.njol.skript.ScriptLoader;
44
import ch.njol.skript.Skript;
5+
import ch.njol.skript.SkriptCommand;
6+
import ch.njol.skript.command.ScriptCommand;
57
import ch.njol.skript.doc.Description;
68
import ch.njol.skript.doc.Examples;
79
import ch.njol.skript.doc.Name;
810
import ch.njol.skript.doc.Since;
911
import ch.njol.skript.lang.Effect;
1012
import ch.njol.skript.lang.Expression;
13+
import ch.njol.skript.localization.Language;
14+
import ch.njol.skript.localization.PluralizingArgsMessage;
15+
import ch.njol.skript.log.LogEntry;
16+
import ch.njol.skript.log.LogHandler;
17+
import ch.njol.skript.log.RedirectingLogHandler;
18+
import ch.njol.skript.log.RetainingLogHandler;
19+
import ch.njol.skript.log.TimingLogHandler;
1120
import ch.njol.skript.registrations.Feature;
21+
import ch.njol.skript.util.Utils;
22+
import ch.njol.util.StringUtils;
23+
import org.bukkit.Bukkit;
24+
import org.bukkit.command.CommandSender;
1225
import org.jetbrains.annotations.Nullable;
1326
import org.jetbrains.annotations.UnknownNullability;
1427
import org.skriptlang.skript.lang.script.Script;
@@ -21,7 +34,10 @@
2134
import java.io.File;
2235
import java.io.FileFilter;
2336
import java.io.IOException;
37+
import java.util.HashSet;
2438
import java.util.Set;
39+
import java.util.logging.Level;
40+
import java.util.stream.Collectors;
2541

2642
@Name("Enable/Disable/Unload/Reload Script")
2743
@Description("""
@@ -41,9 +57,9 @@ public class EffScriptFile extends Effect {
4157

4258
static {
4359
Skript.registerEffect(EffScriptFile.class,
44-
"(1:(enable|load)|2:reload|3:disable|4:unload) script [file|named] %string%",
45-
"(1:(enable|load)|2:reload|3:disable|4:unload) skript file %string%",
46-
"(1:(enable|load)|2:reload|3:disable|4:unload) %scripts%"
60+
"(1:(enable|load)|2:reload|3:disable|4:unload) script [file|named] %string% [print:with errors]",
61+
"(1:(enable|load)|2:reload|3:disable|4:unload) skript file %string% [print:with errors]",
62+
"(1:(enable|load)|2:reload|3:disable|4:unload) %scripts% [print:with errors]"
4763
);
4864
/*
4965
The string-pattern must come first (since otherwise `script X` would match the expression)
@@ -57,12 +73,13 @@ public class EffScriptFile extends Effect {
5773

5874
private @UnknownNullability Expression<String> scriptNameExpression;
5975
private @UnknownNullability Expression<Script> scriptExpression;
60-
private boolean scripts, hasReflection;
76+
private boolean scripts, hasReflection, printErrors;
6177

6278
@Override
6379
@SuppressWarnings("unchecked")
6480
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
6581
this.mark = parseResult.mark;
82+
printErrors = parseResult.hasTag("print");
6683
switch (matchedPattern) {
6784
case 0, 1:
6885
this.scriptNameExpression = (Expression<String>) exprs[0];
@@ -77,20 +94,28 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
7794

7895
@Override
7996
protected void execute(Event event) {
97+
Set<CommandSender> recipients = new HashSet<>();
98+
if (printErrors) {
99+
recipients.addAll(Bukkit.getOnlinePlayers().stream()
100+
.filter(player -> player.hasPermission("skript.reloadnotify"))
101+
.collect(Collectors.toSet()));
102+
}
103+
RedirectingLogHandler logHandler = new RedirectingLogHandler(recipients, "").start();
104+
80105
if (scripts) {
81106
for (Script script : scriptExpression.getArray(event)) {
82107
@Nullable File file = script.getConfig().getFile();
83-
this.handle(file, script.getConfig().getFileName());
108+
this.handle(file, script.getConfig().getFileName(), logHandler);
84109
}
85110
} else {
86111
String name = scriptNameExpression.getSingle(event);
87-
if (name == null)
88-
return;
89-
this.handle(ScriptLoader.getScriptFromName(name), name);
112+
if (name != null)
113+
this.handle(ScriptLoader.getScriptFromName(name), name, logHandler);
90114
}
115+
logHandler.close();
91116
}
92117

93-
private void handle(@Nullable File scriptFile, @Nullable String name) {
118+
private void handle(@Nullable File scriptFile, @Nullable String name, OpenCloseable openCloseable) {
94119
if (scriptFile == null || !scriptFile.exists())
95120
return;
96121
if (name == null)
@@ -117,15 +142,15 @@ private void handle(@Nullable File scriptFile, @Nullable String name) {
117142
}
118143
}
119144

120-
ScriptLoader.loadScripts(scriptFile, OpenCloseable.EMPTY);
145+
ScriptLoader.loadScripts(scriptFile, openCloseable);
121146
break;
122147
case RELOAD:
123148
if (filter.accept(scriptFile))
124149
return;
125150

126151
this.unloadScripts(scriptFile);
127152

128-
ScriptLoader.loadScripts(scriptFile, OpenCloseable.EMPTY);
153+
ScriptLoader.loadScripts(scriptFile, openCloseable);
129154
break;
130155
case UNLOAD:
131156
if (hasReflection) { // if we don't use the new features this falls through into DISABLE

0 commit comments

Comments
 (0)