Skip to content
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

Fix paths for enabled scripts in ExprScripts. #6374

Merged
merged 6 commits into from
Feb 1, 2024
Merged
Changes from 4 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
32 changes: 21 additions & 11 deletions src/main/java/ch/njol/skript/expressions/ExprScripts.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
import ch.njol.util.Kleenean;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.bukkit.event.Event;

Expand All @@ -54,12 +57,12 @@ public class ExprScripts extends SimpleExpression<String> {

static {
Skript.registerExpression(ExprScripts.class, String.class, ExpressionType.SIMPLE,
"[all [of the]] scripts [(1:without ([subdirectory] paths|parents))]",
"[all [of the]] (enabled|loaded) scripts [(1:without ([subdirectory] paths|parents))]",
"[all [of the]] (disabled|unloaded) scripts [(1:without ([subdirectory] paths|parents))]");
"[all [of the]] scripts [1:without ([subdirectory] paths|parents)]",
"[all [of the]] (enabled|loaded) scripts [1:without ([subdirectory] paths|parents)]",
"[all [of the]] (disabled|unloaded) scripts [1:without ([subdirectory] paths|parents)]");
APickledWalrus marked this conversation as resolved.
Show resolved Hide resolved
}

private static final String SCRIPTS_PATH = new File(Skript.getInstance().getDataFolder(), Skript.SCRIPTSFOLDER).getPath() + File.separator;
private static final Path SCRIPTS_PATH = Skript.getInstance().getScriptsFolder().getAbsoluteFile().toPath();

private boolean includeEnabled;
private boolean includeDisabled;
Expand All @@ -75,20 +78,27 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye

@Override
protected String[] get(Event event) {
List<File> scripts = new ArrayList<>();
List<Path> scripts = new ArrayList<>();
if (includeEnabled) {
for (Script script : ScriptLoader.getLoadedScripts())
scripts.add(script.getConfig().getFile());
scripts.add(script.getConfig().getPath());
}
if (includeDisabled)
scripts.addAll(ScriptLoader.getDisabledScripts());
return formatFiles(scripts);
scripts.addAll(ScriptLoader.getDisabledScripts()
.stream()
.map(File::toPath)
.collect(Collectors.toList()));
return formatPaths(scripts);
}

@SuppressWarnings("null")
private String[] formatFiles(List<File> files) {
return files.stream()
.map(f -> noPaths ? f.getName() : f.getPath().replaceFirst(Pattern.quote(SCRIPTS_PATH), ""))
private String[] formatPaths(List<Path> paths) {
return paths.stream()
.map(path -> {
if (noPaths)
return path.getFileName();
return SCRIPTS_PATH.relativize(path.toAbsolutePath()).toString();
})
.toArray(String[]::new);
}

Expand Down
Loading