package ru.bitheaven.donpayinteg.config; import net.fabricmc.loader.api.FabricLoader; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; import org.yaml.snakeyaml.inspector.TagInspector; import ru.bitheaven.donpayinteg.DonPayInteg; import java.io.*; import static ru.bitheaven.donpayinteg.DonPayInteg.LOGGER; public class ConfigHandler { private static final String PATH = FabricLoader.getInstance().getConfigDir().resolve(DonPayInteg.MOD_ID + ".yaml").toString(); public static void register() { if(!new File(PATH).exists()) std(); } public static Config load() { LoaderOptions loaderOptions = new LoaderOptions(); loaderOptions.setTagInspector(tag -> tag.getClassName().equals(Config.class.getName())); Yaml yaml = new Yaml(new Constructor(Config.class, loaderOptions)); Config config; try { config = yaml.load(new FileInputStream(PATH)); } catch (FileNotFoundException e) { throw new RuntimeException(e); } return config; } public static void save(Config config) { DumperOptions options = new DumperOptions(); options.setIndent(2); options.setPrettyFlow(true); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(options); try { yaml.dump(config, new FileWriter(PATH)); } catch (IOException e) { throw new RuntimeException(e); } } public static void std() { save(new Config()); LOGGER.info("Created new config file"); } }