package io.gitlab.jfronny.translater; import io.gitlab.jfronny.translater.interfaces.StringTransformer; import me.sargunvohra.mcmods.autoconfig1u.AutoConfig; import me.sargunvohra.mcmods.autoconfig1u.ConfigManager; import net.fabricmc.loader.api.FabricLoader; import java.io.*; import java.util.Properties; public abstract class CachingTransformer implements StringTransformer { private static final File cacheFile = new File(FabricLoader.getInstance().getConfigDirectory(), "Translater.cache"); Properties cache = null; @Override public String transform(String str) { if (str == null) return null; if (cache == null) { cache = new Properties(); if (ModInit.cfg.forceRegenerate) { ModInit.cfg.forceRegenerate = false; ((ConfigManager) AutoConfig.getConfigHolder(Cfg.class)).save(); } else { if (cacheFile.exists()) { try { FileInputStream inS = new FileInputStream(cacheFile); cache.load(inS); inS.close(); } catch (IOException e) { e.printStackTrace(); } } else { if (!ModInit.cfg.breakFully && ModInit.cfg.rounds == 5) { try { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream inS = classLoader.getResourceAsStream("namecache.ini"); if (inS != null) { cache.load(inS); inS.close(); } } catch (IOException e) { e.printStackTrace(); } } } } try { FileOutputStream outS = new FileOutputStream(cacheFile); cache.store(outS, "---Lang---"); outS.close(); } catch (IOException e) { e.printStackTrace(); } } if (!cache.containsKey(str)) { cache.put(str, getTransformed(str)); try { FileOutputStream outS = new FileOutputStream(cacheFile); cache.store(outS, "---Lang---"); outS.close(); } catch (IOException e) { e.printStackTrace(); } } return (String) cache.get(str); } public abstract String getTransformed(String str); }