Skip to content

Commit

Permalink
Script effects & tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Moderocky committed May 17, 2024
1 parent dc9b950 commit 609bac0
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 24 deletions.
78 changes: 54 additions & 24 deletions src/main/java/ch/njol/skript/effects/EffScriptFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,21 @@
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
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 org.eclipse.jdt.annotation.Nullable;

import java.io.File;
import java.io.IOException;
import java.util.Set;

@Name("Enable/Disable/Reload Script File")
@Description("Enables, disables, or reloads a script file.")
@Name("Enable/Disable/Reload Script")
@Description("Enables, disables, or reloads a script.")
@Examples({
"reload script \"test\"",
"enable script file \"testing\"",
Expand All @@ -51,34 +52,61 @@ public class EffScriptFile extends Effect {

static {
Skript.registerEffect(EffScriptFile.class,
"(1:(enable|load)|2:reload|3:(disable|unload)) s(c|k)ript [file] %string%"
"(1:(enable|load)|2:reload|3:(disable|unload)) script [file|named] %string%",
"(1:(enable|load)|2:reload|3:(disable|unload)) skript file %string%",
"(2:reload|3:(disable|unload)) %scripts%"
);
/*
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;

private int mark;

@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<String> fileName;
private @UnknownNullability Expression<String> stringExpression;
private @UnknownNullability Expression<Script> scriptExpression;
private boolean scripts;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
mark = parseResult.mark;
fileName = (Expression<String>) exprs[0];
this.mark = parseResult.mark;
switch (matchedPattern) {
case 0:
case 1:
this.stringExpression = (Expression<String>) exprs[0];
break;
case 2:
this.scriptExpression = (Expression<Script>) exprs[0];
this.scripts = true;
}
return true;
}

@Override
protected void execute(Event e) {
String name = fileName.getSingle(e);
if (name == null)
return;
File scriptFile = SkriptCommand.getScriptFromName(name);
if (scriptFile == null)
return;
protected void execute(Event event) {
if (scripts) {
Script[] array = scriptExpression.getArray(event);
for (Script script : array) {
@Nullable File file = script.getConfig().getFile();
this.handle(file, script.getConfig().getFileName());
}
} else {
String name = stringExpression.getSingle(event);
if (name == null)
return;
this.handle(SkriptCommand.getScriptFromName(name), name);
}
}

private void handle(@Nullable File scriptFile, @Nullable String name) {
if (scriptFile == null || !scriptFile.exists())
return;
if (name == null)
name = scriptFile.getName();
switch (mark) {
case ENABLE: {
if (ScriptLoader.getLoadedScriptsFilter().accept(scriptFile))
Expand All @@ -103,9 +131,9 @@ protected void execute(Event e) {
case RELOAD: {
if (ScriptLoader.getDisabledScriptsFilter().accept(scriptFile))
return;

this.unloadScripts(scriptFile);

ScriptLoader.loadScripts(scriptFile, OpenCloseable.EMPTY);
break;
}
Expand All @@ -132,7 +160,7 @@ protected void execute(Event e) {
assert false;
}
}

private void unloadScripts(File file) {
if (file.isDirectory()) {
Set<Script> scripts = ScriptLoader.getScripts(file);
Expand All @@ -147,9 +175,11 @@ private void unloadScripts(File file) {
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return (mark == ENABLE ? "enable" : mark == RELOAD ? "disable" : mark == DISABLE ? "unload" : "")
+ " script file " + fileName.toString(e, debug);
public String toString(@Nullable Event event, boolean debug) {
String start = mark == ENABLE ? "enable " : mark == RELOAD ? "disable " : mark == DISABLE ? "unload " : " ";
if (scripts)
return start + scriptExpression.toString(event, debug);
return start + "script file " + stringExpression.toString(event, debug);
}

}
3 changes: 3 additions & 0 deletions src/test/skript/tests/misc/-disabled.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

on script load:
set {EffScriptFile} to true
26 changes: 26 additions & 0 deletions src/test/skript/tests/syntaxes/effects/EffScriptFile.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
options:
path: "../../../../../../src/test/skript/tests/"

# Princess test script is in another castle, Mario!
# paths are relativised to the /scripts/ directory
# but we are loading these scripts from the test folder :(

test "enable script":
set {_path} to {@path} + "misc/-disabled.sk"
set {EffScriptFile} to false
load script named {_path}
assert {EffScriptFile} is true with "load event didn't run"
delete {EffScriptFile}

test "reload script":
set {_path} to {@path} + "misc/disabled.sk"
set {EffScriptFile} to false
reload (script named {_path})
assert {EffScriptFile} is true with "load event didn't run"
delete {EffScriptFile}

test "disable script":
set {_path} to {@path} + "misc/disabled.sk"
disable script named {_path}
delete {EffScriptFile}

0 comments on commit 609bac0

Please sign in to comment.