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

56 lines
2.4 KiB
Java
Raw Normal View History

2021-09-15 16:45:54 +02:00
package io.gitlab.jfronny.respackopts.filters.util;
2021-02-15 11:44:59 +01:00
import io.gitlab.jfronny.libjf.ResourcePath;
import io.gitlab.jfronny.respackopts.Respackopts;
2022-12-08 19:00:41 +01:00
import net.minecraft.resource.*;
2021-02-15 11:44:59 +01:00
import net.minecraft.util.Identifier;
import java.io.InputStream;
2022-12-08 19:00:41 +01:00
import java.util.Set;
2021-02-15 11:44:59 +01:00
2021-09-15 16:45:54 +02:00
public class FileFallbackProvider {
public static boolean fileHasFallback(ResourcePack pack, String file) {
return FileRpoSearchProvider.modifyWithRpo(file, pack, rpo -> {
2021-09-15 17:13:14 +02:00
if (rpo.fallbacks != null) {
for (String s : rpo.fallbacks) {
2021-09-15 17:13:14 +02:00
ResourcePath tmp = new ResourcePath(s);
2022-12-08 19:00:41 +01:00
if (pack.open(tmp.getType(), tmp.getId()) != null) return true;
2021-09-15 17:13:14 +02:00
}
}
return false;
2021-09-15 17:13:14 +02:00
}, false);
}
2022-12-08 19:00:41 +01:00
public static InputSupplier<InputStream> getReplacement(ResourcePack pack, String file) {
return FileRpoSearchProvider.modifyWithRpo(file, pack, rpo -> {
2021-09-15 17:13:14 +02:00
try {
if (rpo.fallbacks != null) {
for (String s : rpo.fallbacks) {
2021-07-13 16:15:03 +02:00
ResourcePath tmp = new ResourcePath(s);
2022-12-08 19:00:41 +01:00
InputSupplier<InputStream> is = pack.open(tmp.getType(), tmp.getId());
if (is != null) return is;
}
2021-02-15 11:44:59 +01:00
}
Respackopts.LOGGER.error("Could not determine replacement for " + file);
2021-02-15 11:44:59 +01:00
}
2021-09-15 17:13:14 +02:00
catch (Exception e) {
Respackopts.LOGGER.error("Could not determine replacement for " + file, e);
2021-02-15 11:44:59 +01:00
}
2021-09-15 17:13:14 +02:00
return null;
}, null);
2021-02-15 11:44:59 +01:00
}
2022-12-08 19:00:41 +01:00
public static void addFallbackResources(String namespace, ResourceType type, Identifier identifier, ResourcePack pack, 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!
2022-12-08 19:00:41 +01:00
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(pack, type.getDirectory() + "/" + expectedTarget)) {
Identifier id = new Identifier(namespace, expectedTarget);
ids.add(id);
out.accept(id, pack.open(type, id));
2021-02-15 11:44:59 +01:00
}
}
}
}