diff --git a/src/main/java/io/github/pacifistmc/forgix/plugin/MergeJarsTask.java b/src/main/java/io/github/pacifistmc/forgix/plugin/MergeJarsTask.java index e77b812..523ec0d 100644 --- a/src/main/java/io/github/pacifistmc/forgix/plugin/MergeJarsTask.java +++ b/src/main/java/io/github/pacifistmc/forgix/plugin/MergeJarsTask.java @@ -80,16 +80,7 @@ void mergeJars() throws IOException { if (forgeSettings.getJarLocation() != null) { forgeJar = new File(forgeProject.getProjectDir(), forgeSettings.getJarLocation()); } else { - int i = 0; - for (File file : new File(forgeProject.getBuildDir(), "libs").listFiles()) { - if (file.isDirectory()) continue; - if (io.github.pacifistmc.forgix.utils.FileUtils.isZipFile(file)) { - if (file.getName().length() < i || i == 0) { - i = file.getName().length(); - forgeJar = file; - } - } - } + forgeJar = io.github.pacifistmc.forgix.utils.FileUtils.findLatestFile(new File(forgeProject.getBuildDir(), "libs")); } } @@ -97,16 +88,7 @@ void mergeJars() throws IOException { if (neoforgeSettings.getJarLocation() != null) { neoforgeJar = new File(neoforgeProject.getProjectDir(), neoforgeSettings.getJarLocation()); } else { - int i = 0; - for (File file : new File(neoforgeProject.getBuildDir(), "libs").listFiles()) { - if (file.isDirectory()) continue; - if (io.github.pacifistmc.forgix.utils.FileUtils.isZipFile(file)) { - if (file.getName().length() < i || i == 0) { - i = file.getName().length(); - neoforgeJar = file; - } - } - } + neoforgeJar = io.github.pacifistmc.forgix.utils.FileUtils.findLatestFile(new File(neoforgeProject.getBuildDir(), "libs")); } } @@ -114,16 +96,7 @@ void mergeJars() throws IOException { if (fabricSettings.getJarLocation() != null) { fabricJar = new File(fabricProject.getProjectDir(), fabricSettings.getJarLocation()); } else { - int i = 0; - for (File file : new File(fabricProject.getBuildDir(), "libs").listFiles()) { - if (file.isDirectory()) continue; - if (io.github.pacifistmc.forgix.utils.FileUtils.isZipFile(file)) { - if (file.getName().length() < i || i == 0) { - i = file.getName().length(); - fabricJar = file; - } - } - } + fabricJar = io.github.pacifistmc.forgix.utils.FileUtils.findLatestFile(new File(fabricProject.getBuildDir(), "libs")); } } @@ -131,16 +104,7 @@ void mergeJars() throws IOException { if (quiltSettings.getJarLocation() != null) { quiltJar = new File(quiltProject.getProjectDir(), quiltSettings.getJarLocation()); } else { - int i = 0; - for (File file : new File(quiltProject.getBuildDir(), "libs").listFiles()) { - if (file.isDirectory()) continue; - if (io.github.pacifistmc.forgix.utils.FileUtils.isZipFile(file)) { - if (file.getName().length() < i || i == 0) { - i = file.getName().length(); - quiltJar = file; - } - } - } + quiltJar = io.github.pacifistmc.forgix.utils.FileUtils.findLatestFile(new File(quiltProject.getBuildDir(), "libs")); } } @@ -148,16 +112,7 @@ void mergeJars() throws IOException { if (entry.getValue().getJarLocation() != null) { customJars.put(entry.getValue(), new File(entry.getKey().getProjectDir(), entry.getValue().getJarLocation())); } else { - int i = 0; - for (File file : new File(entry.getKey().getBuildDir(), "libs").listFiles()) { - if (file.isDirectory()) continue; - if (io.github.pacifistmc.forgix.utils.FileUtils.isZipFile(file)) { - if (file.getName().length() < i || i == 0) { - i = file.getName().length(); - customJars.put(entry.getValue(), file); - } - } - } + customJars.put(entry.getValue(), io.github.pacifistmc.forgix.utils.FileUtils.findLatestFile(new File(entry.getKey().getBuildDir(), "libs"))); } } diff --git a/src/main/java/io/github/pacifistmc/forgix/utils/FileUtils.java b/src/main/java/io/github/pacifistmc/forgix/utils/FileUtils.java index f53969a..65f8470 100644 --- a/src/main/java/io/github/pacifistmc/forgix/utils/FileUtils.java +++ b/src/main/java/io/github/pacifistmc/forgix/utils/FileUtils.java @@ -1,6 +1,7 @@ package io.github.pacifistmc.forgix.utils; import org.apache.commons.io.FilenameUtils; +import org.jetbrains.annotations.Nullable; import java.io.*; import java.nio.charset.Charset; @@ -229,6 +230,45 @@ private static boolean isBinary(File file) { } } + /** + * Try to find the latest file with the shortest name in a directory + * @param directory The directory to find the file in + * @return The newest file or null if no files were found + */ + @Nullable + public static File findLatestFile(File directory) { + File latestFile = null; + long lastModifiedTime = Long.MIN_VALUE; + int i = 0; + List processedFiles = new ArrayList<>(); + + File[] files = directory.listFiles(); + if (files == null) + return null; + + for (File f : files) { + if (f.isDirectory() || !isZipFile(f)) + continue; + + if (f.getName().contains("-shadow") || f.getName().contains("-sources") || f.getName().contains("-dev")) + continue; + + if (f.lastModified() > lastModifiedTime) { + lastModifiedTime = f.lastModified(); + processedFiles.add(f); + } + } + + for (File f : processedFiles) { + if (f.getName().length() < i || i == 0) { + latestFile = f; + i = f.getName().length(); + } + } + + return latestFile; + } + /** * This method returns true if the character is a magic character that's used in binary files * @return If it's a magic character