From 55425da586e28b2992ae29a6b395b2f0c56b4344 Mon Sep 17 00:00:00 2001 From: Cameron Reed Date: Mon, 12 Dec 2022 22:47:21 -0700 Subject: [PATCH] Store options in a properties file --- .../haxxor/client/options/HaxxorOptions.java | 92 ++++++++++++++----- .../haxxor/client/ui/CropSelectScreen.java | 2 +- .../haxxor/client/ui/HaxxorOptionsScreen.java | 4 + 3 files changed, 73 insertions(+), 25 deletions(-) diff --git a/src/main/java/cmods/haxxor/client/options/HaxxorOptions.java b/src/main/java/cmods/haxxor/client/options/HaxxorOptions.java index 58e7d3b..bf38438 100644 --- a/src/main/java/cmods/haxxor/client/options/HaxxorOptions.java +++ b/src/main/java/cmods/haxxor/client/options/HaxxorOptions.java @@ -1,12 +1,19 @@ package cmods.haxxor.client.options; import cmods.haxxor.client.AutoFarmer; +import net.fabricmc.loader.api.FabricLoader; -import java.util.prefs.Preferences; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Path; +import java.util.Properties; public final class HaxxorOptions { private static HaxxorOptions instance = null; + private final Path properties_file = FabricLoader.getInstance().getConfigDir().resolve("Haxxor.properties"); + public final AutoFarmerOptions autoFarmer; public final FlyHackOptions flyHack; @@ -28,17 +35,52 @@ public final class HaxxorOptions { } public void load() { - autoFarmer.load(); + Properties properties = new Properties(); + try (FileReader reader = new FileReader(properties_file.toFile())) { + properties.load(reader); + } catch (IOException e) { + properties = null; + } + + // Sub category properties + autoFarmer.load(properties); + + // Top level properties + cancel_fall_damage.value = getBooleanProperty(properties, "cancel_fall_damage", cancel_fall_damage.value); } public void save() { - autoFarmer.save(); + Properties properties = new Properties(); + + // Sub category properties + autoFarmer.save(properties); + + // Top level properties + properties.setProperty("cancel_fall_damage", cancel_fall_damage.value.toString()); + + try { + properties.store(new FileWriter(properties_file.toFile()), "Haxxor Properties"); + } catch (IOException e) { + System.out.println("[Haxxor]: Failed to save properties"); + } + } + + private static boolean getBooleanProperty(Properties properties, String key, Boolean default_value) { + if (properties == null) + return default_value; + return Boolean.parseBoolean(properties.getProperty(key, default_value.toString())); + } + + private static int getIntegerProperty(Properties properties, String key, Integer default_value) { + if (properties == null) + return default_value; + return Integer.parseInt(properties.getProperty(key, default_value.toString())); } public static class AutoFarmerOptions { - private final Preferences prefs = Preferences.userNodeForPackage(AutoFarmerOptions.class); + private final String PREFIX = "auto_farmer."; public BooleanOption enabled = new BooleanOption(false); public BooleanOption move_seeds = new BooleanOption(true); @@ -55,45 +97,47 @@ public final class HaxxorOptions { public AutoFarmerOptions() { } - private void load() { - move_seeds.value = prefs.getBoolean("move_seeds", move_seeds.value); - min_y.value = prefs.getInt("min_y", min_y.value); - max_y.value = prefs.getInt("max_y", max_y.value); - horizontal_reach.value = prefs.getInt("horizontal_reach", horizontal_reach.value); - max_actions_per_tick.value = prefs.getInt("max_actions_per_tick", max_actions_per_tick.value); + private void load(Properties properties) { + move_seeds.value = getBooleanProperty(properties, PREFIX + "move_seeds", move_seeds.value); + min_y.value = getIntegerProperty(properties, PREFIX + "min_y", min_y.value); + max_y.value = getIntegerProperty(properties, PREFIX + "max_y", max_y.value); + horizontal_reach.value = getIntegerProperty(properties, PREFIX + "horizontal_reach", + horizontal_reach.value); + max_actions_per_tick.value = getIntegerProperty(properties, PREFIX + "max_actions_per_tick", + max_actions_per_tick.value); for (int i = 0; i < AutoFarmer.crops.length; i++) { seeds_enabled[i] = new BooleanOption(true); crops_enabled[i] = new BooleanOption(true); if (AutoFarmer.crops[i].seed_type != null) { - seeds_enabled[i].value = prefs.getBoolean("seed_" + AutoFarmer.crops[i].seed_type.getName().getString(), - seeds_enabled[i].value); + seeds_enabled[i].value = getBooleanProperty(properties, + PREFIX + "seeds." + AutoFarmer.crops[i].seed_type.getName().getString(), seeds_enabled[i].value); } if (AutoFarmer.crops[i].crop_type != null) { - crops_enabled[i].value = prefs.getBoolean("crop_" + AutoFarmer.crops[i].crop_type.getName().getString(), - crops_enabled[i].value); + crops_enabled[i].value = getBooleanProperty(properties, + PREFIX + "crops." + AutoFarmer.crops[i].crop_type.getName().getString(), crops_enabled[i].value); } } } - private void save() { - prefs.putBoolean("move_seeds", move_seeds.value); - prefs.putInt("min_y", min_y.value); - prefs.putInt("max_y", max_y.value); - prefs.putInt("horizontal_reach", horizontal_reach.value); - prefs.putInt("max_actions_per_tick", max_actions_per_tick.value); + private void save(Properties properties) { + properties.setProperty(PREFIX + "move_seeds", move_seeds.value.toString()); + properties.setProperty(PREFIX + "min_y", min_y.value.toString()); + properties.setProperty(PREFIX + "max_y", max_y.value.toString()); + properties.setProperty(PREFIX + "horizontal_reach", horizontal_reach.value.toString()); + properties.setProperty(PREFIX + "max_actions_per_tick", max_actions_per_tick.value.toString()); for (int i = 0; i < AutoFarmer.crops.length; i++) { if (AutoFarmer.crops[i].seed_type != null) { - prefs.putBoolean("seed_" + AutoFarmer.crops[i].seed_type.getName().getString(), - seeds_enabled[i].value); + properties.setProperty(PREFIX + "seeds." + AutoFarmer.crops[i].seed_type.getName().getString(), + seeds_enabled[i].value.toString()); } if (AutoFarmer.crops[i].crop_type != null) { - prefs.putBoolean("crop_" + AutoFarmer.crops[i].crop_type.getName().getString(), - crops_enabled[i].value); + properties.setProperty(PREFIX + "crops." + AutoFarmer.crops[i].crop_type.getName().getString(), + crops_enabled[i].value.toString()); } } } diff --git a/src/main/java/cmods/haxxor/client/ui/CropSelectScreen.java b/src/main/java/cmods/haxxor/client/ui/CropSelectScreen.java index 82cb831..76b3ff9 100644 --- a/src/main/java/cmods/haxxor/client/ui/CropSelectScreen.java +++ b/src/main/java/cmods/haxxor/client/ui/CropSelectScreen.java @@ -51,7 +51,7 @@ public class CropSelectScreen extends Screen { } } - adder.add(ButtonWidget.builder(ScreenTexts.DONE, button -> { options.save(); client.setScreen(parent); }) + adder.add(ButtonWidget.builder(ScreenTexts.DONE, button -> client.setScreen(parent)) .width(doneButtonWidth).build(), 2, adder.copyPositioner().marginTop(doneButtonRowIncrement)); grid.recalculateDimensions(); diff --git a/src/main/java/cmods/haxxor/client/ui/HaxxorOptionsScreen.java b/src/main/java/cmods/haxxor/client/ui/HaxxorOptionsScreen.java index 2ac2792..9b8398b 100644 --- a/src/main/java/cmods/haxxor/client/ui/HaxxorOptionsScreen.java +++ b/src/main/java/cmods/haxxor/client/ui/HaxxorOptionsScreen.java @@ -55,4 +55,8 @@ public class HaxxorOptionsScreen extends Screen { this.height - textRenderer.fontHeight - 2, 0xffffff); super.render(matrices, mouseX, mouseY, delta); } + + public void removed() { + options.save(); + } }