package io.gitlab.jfronny.respackopts.filters.util; import io.gitlab.jfronny.libjf.ResourcePath; import io.gitlab.jfronny.respackopts.Respackopts; import io.gitlab.jfronny.respackopts.model.FileRpo; import io.gitlab.jfronny.respackopts.util.MetaCache; import net.minecraft.resource.ResourcePack; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.Map; public class FileRpoSearchProvider { public static boolean isRpo(String fileName) { return fileName.endsWith(Respackopts.FILE_EXTENSION); } public static T modifyWithRpo(String fileName, ResourcePack pack, ModifiedGenerator getModified, T defaultValue) { if (FileRpoSearchProvider.isRpo(fileName)) return defaultValue; Map frpReg = MetaCache.FILE_RPOS.get(MetaCache.getId(pack)); String rpPathName = fileName + Respackopts.FILE_EXTENSION; if (frpReg.containsKey(rpPathName)) return getModified.getModified(frpReg.get(rpPathName)); ResourcePath rpoPath; try { rpoPath = new ResourcePath(rpPathName); if (!pack.contains(rpoPath.getType(), rpoPath.getId())) return defaultValue; } catch (Throwable e) { Respackopts.LOGGER.error("Could not check file filter status", e); return defaultValue; } try (InputStream stream = pack.open(rpoPath.getType(), rpoPath.getId()); Reader w = new InputStreamReader(stream)) { FileRpo frp = Respackopts.GSON.fromJson(w, FileRpo.class); frp.path = rpPathName; frpReg.put(rpPathName, frp); return getModified.getModified(frp); } catch (Exception e) { Respackopts.LOGGER.error("Could not generate replacement for " + rpoPath.getName() + " in " + pack.getName(), e); return defaultValue; } } public interface ModifiedGenerator { T getModified(FileRpo rpo); } }