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;
|
|
|
|
import net.minecraft.resource.ResourcePack;
|
2021-08-24 13:02:54 +02:00
|
|
|
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) {
|
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);
|
|
|
|
if (pack.contains(tmp.getType(), tmp.getId()))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-02-15 12:17:52 +01:00
|
|
|
return false;
|
2021-09-15 17:13:14 +02:00
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
2021-10-03 16:45:29 +02:00
|
|
|
public static InputStream getReplacement(ResourcePack pack, String name) {
|
2021-09-15 17:13:14 +02:00
|
|
|
return FileRpoSearchProvider.modifyWithRpo(name, pack, rpo -> {
|
|
|
|
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);
|
|
|
|
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 12:17:52 +01:00
|
|
|
}
|
2021-02-15 11:44:59 +01:00
|
|
|
}
|
2021-10-03 16:45:29 +02: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) {
|
2021-10-03 16:45:29 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-10-03 16:45:29 +02:00
|
|
|
public static void addFallbackResources(ResourcePack pack, Collection<Identifier> ret, String namespace, ResourceType type) {
|
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!
|
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)) {
|
2021-10-03 16:45:29 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|