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
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 24 additions & 7 deletions src/main/java/ch/njol/skript/effects/EffScriptFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.ExprScriptFileErrors;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.log.LogEntry;
import ch.njol.skript.log.RetainingLogHandler;
import ch.njol.skript.registrations.Feature;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;
Expand Down Expand Up @@ -77,20 +80,34 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye

@Override
protected void execute(Event event) {
OpenCloseable openCloseable = OpenCloseable.EMPTY;
RetainingLogHandler logHandler = null;
if (mark == RELOAD || mark == ENABLE) {
logHandler = new RetainingLogHandler().start();
openCloseable = logHandler;
}
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(), openCloseable);
}
} 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, openCloseable);
}
if (logHandler != null) {
String[] errors = logHandler.getErrors().stream().map(LogEntry::getMessage).toArray(String[]::new);
if (mark == RELOAD) {
ExprScriptFileErrors.lastReloadedErrors = errors;
} else {
ExprScriptFileErrors.lastEnabledErrors = errors;
}
logHandler.printErrors();
}
}

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 +134,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
67 changes: 67 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprScriptFileErrors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Example;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Last Reloaded Errors")
@Description("The errors from the last singular or multiple scripts that were reloaded or enabled via an effect.")
@Example("""
reload script named "test.sk"
if last reloaded errors is set:
disable script file "test.sk"
""")
@Example("""
enable script named "-test.sk"
if last enabled errors is set:
disable script file "test.sk"
""")
@Since("INSERT VERSION")
public class ExprScriptFileErrors extends SimpleExpression<String> {

static {
Skript.registerExpression(ExprScriptFileErrors.class, String.class, ExpressionType.SIMPLE,
"[the] last (:reloaded|enabled) [script|skript] errors");
}

public static String[] lastReloadedErrors = null;
public static String[] lastEnabledErrors = null;

private boolean getReloaded;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
getReloaded = parseResult.hasTag("reloaded");
return true;
}

@Override
protected String @Nullable [] get(Event event) {
return getReloaded ? lastReloadedErrors : lastEnabledErrors;
}

@Override
public boolean isSingle() {
return false;
}

@Override
public Class<? extends String> getReturnType() {
return String.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "the last reloaded script errors";
}

}
2 changes: 2 additions & 0 deletions src/test/skript/tests/misc/-reloadErrors.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
on load:
set {_filler} to blah
13 changes: 13 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprScriptErrors.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
options:
path: "../../../../../../src/test/skript/tests/"

test "enabled errors":
set {_path} to {@path} + "misc/-reloadErrors.sk"
enable script named {_path}
assert last enabled errors contains "Can't understand this expression: 'blah'" with "Did not retrieve last enabled errors"

test "reloaded errors":
set {_path} to {@path} + "misc/reloadErrors.sk"
reload script named {_path}
assert last reloaded errors contains "Can't understand this expression: 'blah'" with "Did not retrieve last reloaded errors"
disable script named {_path}