Store options in a properties file

This commit is contained in:
Cameron Reed 2022-12-12 22:47:21 -07:00
parent 9692e06a13
commit 55425da586
3 changed files with 73 additions and 25 deletions

View File

@ -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());
}
}
}

View File

@ -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();

View File

@ -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();
}
}