From c3d85827b2c331e3d5a36ff26a8912912644d343 Mon Sep 17 00:00:00 2001 From: Waiting Idly <25394029+WaitingIdly@users.noreply.github.com> Date: Fri, 8 Nov 2024 12:34:29 -0800 Subject: [PATCH] Fix file sorting when previously matched (#261) * use StringUtils match instead of split length * putAndMoveToLast and comment --- .../com/cleanroommc/groovyscript/sandbox/SandboxData.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/cleanroommc/groovyscript/sandbox/SandboxData.java b/src/main/java/com/cleanroommc/groovyscript/sandbox/SandboxData.java index 2710a9670..443b47f0d 100644 --- a/src/main/java/com/cleanroommc/groovyscript/sandbox/SandboxData.java +++ b/src/main/java/com/cleanroommc/groovyscript/sandbox/SandboxData.java @@ -2,6 +2,7 @@ import com.cleanroommc.groovyscript.api.GroovyLog; import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap; +import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -147,7 +148,9 @@ static Collection getSortedFilesOf(File root, Collection paths) { if (!rootFile.exists()) { continue; } - int pathSize = path.split(separator).length; + // if we are looking at a specific file, we don't want that to be overridden. + // otherwise, we want to use the specificity based on the number of file separators. + int pathSize = StringUtils.countMatches(path, separator); try (Stream stream = Files.walk(rootFile.toPath())) { stream.filter(path1 -> isGroovyFile(path1.toString())) .map(Path::toFile) @@ -155,8 +158,9 @@ static Collection getSortedFilesOf(File root, Collection paths) { .sorted(Comparator.comparing(File::getPath)) .forEach(file -> { if (files.containsKey(file)) { + // if the file already exists, push the priority down if we are more specific than the already existing entry if (pathSize > files.getInt(file)) { - files.put(file, pathSize); + files.putAndMoveToLast(file, pathSize); } } else { files.put(file, pathSize);