package io.gitlab.jfronny.respackopts.filters.util; import io.gitlab.jfronny.libjf.ResourcePath; import io.gitlab.jfronny.respackopts.Respackopts; import net.minecraft.resource.*; import net.minecraft.util.Identifier; import java.io.InputStream; import java.util.Set; public class FileFallbackProvider { public static boolean fileHasFallback(ResourcePack pack, String file) { return FileRpoSearchProvider.modifyWithRpo(file, pack, rpo -> { if (rpo.fallbacks != null) { for (String s : rpo.fallbacks) { ResourcePath tmp = new ResourcePath(s); if (pack.open(tmp.getType(), tmp.getId()) != null) return true; } } return false; }, false); } public static InputSupplier getReplacement(ResourcePack pack, String file) { return FileRpoSearchProvider.modifyWithRpo(file, pack, rpo -> { try { if (rpo.fallbacks != null) { for (String s : rpo.fallbacks) { ResourcePath tmp = new ResourcePath(s); InputSupplier is = pack.open(tmp.getType(), tmp.getId()); if (is != null) return is; } } Respackopts.LOGGER.error("Could not determine replacement for " + file); } catch (Exception e) { Respackopts.LOGGER.error("Could not determine replacement for " + file, e); } return null; }, null); } public static void addFallbackResources(String namespace, ResourceType type, Identifier identifier, ResourcePack pack, Set 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! 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)); } } } }