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

58 lines
2.4 KiB
Java

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