Inceptum/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/model/inceptum/rt/Mod.java

69 lines
2.4 KiB
Java

package io.gitlab.jfronny.inceptum.launcher.model.inceptum.rt;
import io.gitlab.jfronny.commons.io.JFiles;
import io.gitlab.jfronny.inceptum.common.Utils;
import io.gitlab.jfronny.inceptum.launcher.model.fabric.FabricModJson;
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.ModDescription;
import io.gitlab.jfronny.inceptum.launcher.util.ModPath;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
public record Mod(Path path, Optional<ModDescription> mod, Optional<FabricModJson> fmj, Optional<Path> imod) implements Comparable<Mod> {
public String getName() {
if (fmj.isEmpty()) return path.getFileName().toString();
String base;
if (fmj.get().name != null) base = fmj.get().name;
else if (fmj.get().id != null) base = fmj.get().id;
else {
base = path.getFileName().toString();
if (Files.isDirectory(path))
base += '/';
}
if (fmj.get().version != null) base += ' ' + fmj.get().version;
return base;
}
public String[] getDescription() {
try {
if (fmj.isPresent() && fmj.get().description != null) {
return fmj.get().description.split("\n");
} else if (Files.isDirectory(path)) {
return JFiles.listNames(path);
}
} catch (IOException e) {
Utils.LOGGER.error("Could not get description", e);
}
return new String[]{"No description is available", "This mod may have been added manually"};
}
/**
* Generates a description with only the path and no additional information
*
* @param path The path of the mod entry
*/
public Mod(Path path) {
this(path,
Files.isDirectory(path) ? Optional.empty() : Optional.of(ModDescription.of(path)),
Optional.empty(),
ModPath.hasImod(path) ? Optional.of(ModPath.appendImod(path)) : Optional.empty());
}
/**
* Generates a new mod description based on an existing one with an adjusted path
*
* @param path The new path to use
* @param base The mod description to copy
*/
public Mod(Path path, Mod base) {
this(path, base.mod, base.fmj, base.imod);
}
@Override
public int compareTo(Mod o) {
return getName().compareTo(o.getName());
}
}