Respackopts/src/main/java/io/gitlab/jfronny/respackopts/filters/util/FileFallbackProvider.java

55 lines
2.4 KiB
Java

package io.gitlab.jfronny.respackopts.filters.util;
import io.gitlab.jfronny.respackopts.Respackopts;
import io.gitlab.jfronny.respackopts.model.cache.CacheKey;
import io.gitlab.jfronny.respackopts.muscript.RespackoptsFS;
import net.minecraft.resource.*;
import net.minecraft.util.Identifier;
import java.io.InputStream;
import java.util.Set;
public class FileFallbackProvider {
public static boolean fileHasFallback(CacheKey key, RespackoptsFS fs, String file) {
return FileRpoSearchProvider.modifyWithRpo(file, key, fs, rpo -> {
if (rpo.fallbacks != null) {
for (String s : rpo.fallbacks) {
if (fs.open(s) != null) return true;
}
}
return false;
}, false);
}
public static InputSupplier<InputStream> getReplacement(CacheKey key, RespackoptsFS fs, String file) {
return FileRpoSearchProvider.modifyWithRpo(file, key, fs, rpo -> {
try {
if (rpo.fallbacks != null) {
for (String s : rpo.fallbacks) {
InputSupplier<InputStream> is = fs.open(s);
if (is != null) return is;
}
}
Respackopts.LOGGER.error("Could not determine replacement for " + file);
}
catch (Exception e) {
Respackopts.LOGGER.error("Could not determine replacement for " + file, e);
}
return null;
}, null);
}
public static void addFallbackResources(String namespace, ResourceType type, Identifier identifier, CacheKey key, RespackoptsFS fs, Set<Identifier> ids, ResourcePack.ResultConsumer out) {
// Warning: the Identifiers here DON'T CONTAIN THE TYPE! Therefore, it needs to be added when calling a method that generates a ResourcePath!
String path = identifier.getPath();
if (FileRpoSearchProvider.isRpo(path)) {
String expectedTarget = path.substring(0, path.length() - Respackopts.FILE_EXTENSION.length());
if (ids.stream().noneMatch(s -> s.getPath().equals(expectedTarget)) && fileHasFallback(key, fs, type.getDirectory() + "/" + expectedTarget)) {
Identifier id = new Identifier(namespace, expectedTarget);
ids.add(id);
out.accept(id, fs.open(type, id));
}
}
}
}