2021-09-15 16:45:54 +02:00
|
|
|
package io.gitlab.jfronny.respackopts.filters.util;
|
2021-02-15 11:44:59 +01:00
|
|
|
|
2021-10-03 16:45:29 +02: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 {
|
2022-01-12 18:23:49 +01:00
|
|
|
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) {
|
2021-09-16 08:35:26 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-02-15 12:17:52 +01: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) {
|
2022-01-12 18:23:49 +01:00
|
|
|
return FileRpoSearchProvider.modifyWithRpo(file, pack, rpo -> {
|
2021-09-15 17:13:14 +02:00
|
|
|
try {
|
2021-02-15 12:17:52 +01:00
|
|
|
if (rpo.fallbacks != null) {
|
2021-09-16 08:35:26 +02:00
|
|
|
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 12:17:52 +01:00
|
|
|
}
|
2021-02-15 11:44:59 +01:00
|
|
|
}
|
2022-01-12 18:23:49 +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) {
|
2022-01-12 18:23:49 +01:00
|
|
|
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) {
|
2021-08-24 13:02:54 +02:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|