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

58 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;
import net.minecraft.resource.ResourcePack;
import net.minecraft.resource.ResourceType;
2021-02-15 11:44:59 +01:00
import net.minecraft.util.Identifier;
import java.io.InputStream;
import java.util.Collection;
2021-09-15 16:45:54 +02:00
public class FileFallbackProvider {
2021-10-21 21:20:20 +02:00
public static boolean fileHasFallback(ResourcePack pack, String name) {
2021-09-15 17:13:14 +02:00
return FileRpoSearchProvider.modifyWithRpo(name, pack, rpo -> {
if (rpo.fallbacks != null) {
for (String s : rpo.fallbacks) {
2021-09-15 17:13:14 +02:00
ResourcePath tmp = new ResourcePath(s);
if (pack.contains(tmp.getType(), tmp.getId()))
return true;
}
}
return false;
2021-09-15 17:13:14 +02:00
}, false);
}
public static InputStream getReplacement(ResourcePack pack, String name) {
2021-09-15 17:13:14 +02:00
return FileRpoSearchProvider.modifyWithRpo(name, pack, rpo -> {
try {
if (rpo.fallbacks != null) {
for (String s : rpo.fallbacks) {
2021-07-13 16:15:03 +02:00
ResourcePath tmp = new ResourcePath(s);
if (pack.contains(tmp.getType(), tmp.getId()))
2021-09-15 17:13:14 +02:00
return pack.open(tmp.getType(), tmp.getId());
}
2021-02-15 11:44:59 +01:00
}
Respackopts.LOGGER.error("Could not determine replacement for " + name);
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 " + name, 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
}
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!
2021-02-15 11:44:59 +01:00
for (Identifier identifier : ret) {
String path = identifier.getPath();
2021-09-15 17:13:14 +02:00
if (FileRpoSearchProvider.isRpo(path)) {
String expectedTarget = path.substring(0, path.length() - Respackopts.FILE_EXTENSION.length());
2021-10-21 21:20:20 +02:00
if (ret.stream().noneMatch(s -> s.getPath().equals(expectedTarget)) && fileHasFallback(pack, type.getDirectory() + "/" + expectedTarget)) {
2021-02-15 11:44:59 +01:00
ret.add(new Identifier(namespace, expectedTarget));
}
}
}
}
}