Skip to content

Add Option to Print Errors in Reload Script Effect #7830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev/feature
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions src/main/java/ch/njol/skript/effects/EffScriptFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@

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;
Expand All @@ -21,7 +34,10 @@
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("""
Expand All @@ -41,9 +57,9 @@ public class EffScriptFile extends Effect {

static {
Skript.registerEffect(EffScriptFile.class,
"(1:(enable|load)|2:reload|3:disable|4:unload) script [file|named] %string%",
"(1:(enable|load)|2:reload|3:disable|4:unload) skript file %string%",
"(1:(enable|load)|2:reload|3:disable|4:unload) %scripts%"
"(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)
Expand All @@ -57,12 +73,13 @@ public class EffScriptFile extends Effect {

private @UnknownNullability Expression<String> scriptNameExpression;
private @UnknownNullability Expression<Script> scriptExpression;
private boolean scripts, hasReflection;
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];
Expand All @@ -77,20 +94,28 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye

@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());
this.handle(file, script.getConfig().getFileName(), logHandler);
}
} else {
String name = scriptNameExpression.getSingle(event);
if (name == null)
return;
this.handle(ScriptLoader.getScriptFromName(name), name);
if (name != null)
this.handle(ScriptLoader.getScriptFromName(name), name, logHandler);
}
logHandler.close();
}

private void handle(@Nullable File scriptFile, @Nullable String name) {
private void handle(@Nullable File scriptFile, @Nullable String name, OpenCloseable openCloseable) {
if (scriptFile == null || !scriptFile.exists())
return;
if (name == null)
Expand All @@ -117,15 +142,15 @@ private void handle(@Nullable File scriptFile, @Nullable String name) {
}
}

ScriptLoader.loadScripts(scriptFile, OpenCloseable.EMPTY);
ScriptLoader.loadScripts(scriptFile, openCloseable);
break;
case RELOAD:
if (filter.accept(scriptFile))
return;

this.unloadScripts(scriptFile);

ScriptLoader.loadScripts(scriptFile, OpenCloseable.EMPTY);
ScriptLoader.loadScripts(scriptFile, openCloseable);
break;
case UNLOAD:
if (hasReflection) { // if we don't use the new features this falls through into DISABLE
Expand Down