Ignore jar files in the mods folder that are not Forge mods. Closes #9968

This commit is contained in:
LexManos committed 2024-05-12 03:36:22 -07:00
1 parent 4b934809c4
commit dffa3dd4c9
2 files changed
+24 -5

No files matched your search

@@ -18,6 +18,7 @@ import net.minecraftforge.forgespi.locating.IModProvider;
import net.minecraftforge.forgespi.locating.ModFileLoadingException;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import java.io.IOException;
@@ -36,6 +37,11 @@ public abstract class AbstractModProvider implements IModProvider {
protected static final String MODS_TOML = "META-INF/mods.toml";
protected IModLocator.ModFileOrException createMod(Path path) {
return createMod(path, false);
}
@Nullable
protected IModLocator.ModFileOrException createMod(Path path, boolean ignoreUnknown) {
var mjm = new ModJarMetadata();
var sj = SecureJar.from(
jar -> jar.moduleDataProvider().findFile(MODS_TOML).isPresent() ? mjm : JarMetadata.from(jar, path),
@@ -57,6 +63,8 @@ public abstract class AbstractModProvider implements IModProvider {
} else if (type != null) {
LOGGER.debug(LogMarkers.SCAN, "Found {} mod of type {}: {}", JarFile.MANIFEST_NAME, type, path);
mod = new ModFile(sj, this, this::manifestParser, type);
} else if (ignoreUnknown) {
return null;
} else
return new IModLocator.ModFileOrException(null, new ModFileLoadingException("Invalid mod file found " + path));
@@ -16,6 +16,7 @@ import org.slf4j.Logger;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
@@ -42,11 +43,21 @@ public class ModsFolderLocator extends AbstractModProvider implements IModLocato
public List<IModLocator.ModFileOrException> scanMods() {
LOGGER.debug(LogMarkers.SCAN, "Scanning mods dir {} for mods", this.modFolder);
var excluded = ModDirTransformerDiscoverer.allExcluded();
try (var stream = Files.list(this.modFolder)) {
return stream.filter(path -> !excluded.contains(path) && path.getFileName().toString().toLowerCase(Locale.ROOT).endsWith(SUFFIX))
.sorted(Comparator.comparing(path -> path.getFileName().toString().toLowerCase(Locale.ROOT)))
.map(this::createMod)
.toList();
try {
var ret = new ArrayList<IModLocator.ModFileOrException>();
var files = Files.list(this.modFolder).toList();
for (var file : files) {
var name = file.getFileName().toString().toLowerCase(Locale.ROOT);
if (excluded.contains(file) || !name.endsWith(SUFFIX))
continue;
var mod = this.createMod(file, true);
if (mod == null) {
LOGGER.debug(LogMarkers.SCAN, "Found unknown jar file {} ignoring", file);
} else {
ret.add(mod);
}
}
return ret;
} catch (IOException e) {
return sneak(e);
}