package me.totalfreedom.totalfreedommod; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.HashMap; import java.util.Map; import me.totalfreedom.totalfreedommod.util.FLog; import static me.totalfreedom.totalfreedommod.util.FUtil.SAVED_FLAGS_FILENAME; public class SavedFlags extends FreedomService { @Override public void onStart() { } @Override public void onStop() { } @SuppressWarnings("unchecked") public Map getSavedFlags() { Map flags = null; File input = new File(TotalFreedomMod.getPlugin().getDataFolder(), SAVED_FLAGS_FILENAME); if (input.exists()) { try { try (FileInputStream fis = new FileInputStream(input); ObjectInputStream ois = new ObjectInputStream(fis)) { flags = (HashMap)ois.readObject(); } } catch (Exception ex) { FLog.severe(ex); } } return flags; } public boolean getSavedFlag(String flag) throws Exception { Boolean flagValue = null; Map flags = getSavedFlags(); if (flags != null) { if (flags.containsKey(flag)) { flagValue = flags.get(flag); } } if (flagValue != null) { return flagValue; } else { throw new Exception(); } } public void setSavedFlag(String flag, boolean value) { Map flags = getSavedFlags(); if (flags == null) { flags = new HashMap<>(); } flags.put(flag, value); try { final FileOutputStream fos = new FileOutputStream(new File(plugin.getDataFolder(), SAVED_FLAGS_FILENAME)); final ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(flags); oos.close(); fos.close(); } catch (Exception ex) { FLog.severe(ex); } } }