From 229bd77d401beb005edfca1b3ce387e150ddd21d Mon Sep 17 00:00:00 2001 From: sowgro Date: Mon, 5 Jun 2023 21:53:17 -0400 Subject: add level priority, fix sfx volume, fix settings file reading --- src/main/SettingsController.java | 50 ++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 27 deletions(-) (limited to 'src/main/SettingsController.java') diff --git a/src/main/SettingsController.java b/src/main/SettingsController.java index 36fda40..b7b2cb9 100644 --- a/src/main/SettingsController.java +++ b/src/main/SettingsController.java @@ -1,57 +1,53 @@ package main; import java.io.FileWriter; -import java.io.FileNotFoundException; +import java.io.File; import java.io.FileReader; import java.io.IOException; - import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; - import javafx.beans.property.SimpleDoubleProperty; -import javafx.beans.property.SimpleIntegerProperty; public class SettingsController { public SimpleDoubleProperty effectsVol = new SimpleDoubleProperty(1); public SimpleDoubleProperty musicVol = new SimpleDoubleProperty(1); - private JSONObject settings; + private File file = new File("settings.json"); + + public SettingsController() + { + read(); + } - public void read() throws ParseException + public void read() { JSONParser jsonParser = new JSONParser(); //parser to read the file - - try(FileReader reader = new FileReader("settings.json")) + try(FileReader reader = new FileReader(file)) { Object obj = jsonParser.parse(reader); - + JSONObject settings = new JSONObject(); settings = (JSONObject)(obj); //converts read object to a JSONObject - effectsVol.set((double) settings.get("effectsVol")); - musicVol.set((double) settings.get("musicVol")); + effectsVol.set(Double.parseDouble(settings.get("effectsVol")+"")); + musicVol.set(Double.parseDouble(settings.get("musicVol")+"")); } - catch (FileNotFoundException e) - { - e.printStackTrace(); - } - catch (IOException e) + catch (Exception e) { e.printStackTrace(); - } - + } } - public void write(int newEffVol, int newMusVol) + public void write() { - settings.put("musicVol", newMusVol); - settings.put("effectsVol", newEffVol); - try (FileWriter file = new FileWriter("settings.json")) + FileWriter fileWriter; + try { - //write the settings JSONObject instance to the file - file.write(settings.toJSONString()); - file.flush(); - + fileWriter = new FileWriter(file); + JSONObject obj = new JSONObject(); + obj.put("musicVol", musicVol.getValue()); + obj.put("effectsVol", effectsVol.getValue()); + obj.writeJSONString(fileWriter); + fileWriter.flush(); } catch (IOException e) { e.printStackTrace(); -- cgit v1.2.3