package com.irtimaled.bbor.config;
import java.io.File;
+import java.util.HashSet;
+import java.util.Set;
public class ConfigManager {
+ private static Set<Setting<?>> settings = new HashSet<>();
public static File configDir;
- public static Setting fill;
- public static Setting drawVillages;
- public static Setting drawDesertTemples;
- public static Setting drawJungleTemples;
- public static Setting drawWitchHuts;
- public static Setting drawStrongholds;
- public static Setting drawMineShafts;
- public static Setting drawNetherFortresses;
- public static Setting drawOceanMonuments;
- public static Setting alwaysVisible;
- public static Setting renderVillageAsSphere;
- public static Setting drawIronGolemSpawnArea;
- public static Setting drawVillageDoors;
- public static Setting drawSlimeChunks;
- public static Setting slimeChunkMaxY;
- public static Setting keepCacheBetweenSessions;
- public static Setting drawWorldSpawn;
- public static Setting worldSpawnMaxY;
- public static Setting drawLazySpawnChunks;
- public static Setting drawEndCities;
- public static Setting drawMansions;
- public static Setting drawShipwrecks;
- public static Setting drawOceanRuins;
- public static Setting drawBuriedTreasure;
- public static Setting drawIgloos;
- public static Setting drawMobSpawners;
- public static Setting renderMobSpawnerSpawnArea;
- public static Setting renderMobSpawnerActivationLines;
- public static Setting drawPillagerOutposts;
+ public static Setting<Boolean> fill;
+ public static Setting<Boolean> drawVillages;
+ public static Setting<Boolean> drawDesertTemples;
+ public static Setting<Boolean> drawJungleTemples;
+ public static Setting<Boolean> drawWitchHuts;
+ public static Setting<Boolean> drawStrongholds;
+ public static Setting<Boolean> drawMineShafts;
+ public static Setting<Boolean> drawNetherFortresses;
+ public static Setting<Boolean> drawOceanMonuments;
+ public static Setting<Boolean> alwaysVisible;
+ public static Setting<Boolean> renderVillageAsSphere;
+ public static Setting<Boolean> drawIronGolemSpawnArea;
+ public static Setting<Boolean> drawVillageDoors;
+ public static Setting<Boolean> drawSlimeChunks;
+ public static Setting<Integer> slimeChunkMaxY;
+ public static Setting<Boolean> keepCacheBetweenSessions;
+ public static Setting<Boolean> drawWorldSpawn;
+ public static Setting<Integer> worldSpawnMaxY;
+ public static Setting<Boolean> drawLazySpawnChunks;
+ public static Setting<Boolean> drawEndCities;
+ public static Setting<Boolean> drawMansions;
+ public static Setting<Boolean> drawShipwrecks;
+ public static Setting<Boolean> drawOceanRuins;
+ public static Setting<Boolean> drawBuriedTreasure;
+ public static Setting<Boolean> drawIgloos;
+ public static Setting<Boolean> drawMobSpawners;
+ public static Setting<Boolean> renderMobSpawnerSpawnArea;
+ public static Setting<Boolean> renderMobSpawnerActivationLines;
+ public static Setting<Boolean> drawPillagerOutposts;
public static void loadConfig(File mcConfigDir) {
configDir = new File(mcConfigDir, "config");
configDir.mkdirs();
- Configuration config = new Configuration(new File(configDir, "BBOutlineReloaded.cfg"));
- config.load();
+ Configuration config = loadConfig();
+
+ fill = setup(config, "general", "fill", true, "If set to true the bounding boxes are filled.");
+ alwaysVisible = setup(config, "general", "alwaysVisible", false, "If set to true boxes will be visible even through other blocks.");
+ keepCacheBetweenSessions = setup(config, "general", "keepCacheBetweenSessions", false, "If set to true bounding box caches will be kept between sessions.");
+
+ drawVillages = setup(config, "features", "drawVillages", true, "If set to true village bounding boxes are drawn.");
+ renderVillageAsSphere = setup(config, "features", "renderVillageAsSphere", true, "If set to true villages will be drawn as a sphere. (default:true)");
+ drawIronGolemSpawnArea = setup(config, "features", "drawIronGolemSpawnArea", true, "If set to true the iron golem spawn area of the village will be drawn. (default:true)");
+ drawVillageDoors = setup(config, "features", "drawVillageDoors", false, "If set to true lines between the village centre and doors will be drawn. (default:false)");
+
+ drawDesertTemples = setup(config, "features", "drawDesertTemples", true, "If set to true desert temple bounding boxes are drawn.");
+ drawJungleTemples = setup(config, "features", "drawJungleTemples", true, "If set to true jungle temple bounding boxes are drawn.");
+ drawWitchHuts = setup(config, "features", "drawWitchHuts", true, "If set to true witch hut bounding boxes are drawn.");
+ drawStrongholds = setup(config, "features", "drawStrongholds", false, "If set to true stronghold bounding boxes are drawn.");
+ drawMineShafts = setup(config, "features", "drawMineShafts", false, "If set to true mineshaft bounding boxes are drawn.");
+ drawNetherFortresses = setup(config, "features", "drawNetherFortresses", true, "If set to true nether fortress bounding boxes are drawn.");
+ drawOceanMonuments = setup(config, "features", "drawOceanMonuments", true, "If set to true ocean monument bounding boxes are drawn.");
+ drawEndCities = setup(config, "features", "drawEndCities", true, "If set to true end city bounding boxes will be drawn.");
+ drawMansions = setup(config, "features", "drawMansions", true, "If set to true woodland mansions will be drawn.");
+ drawIgloos = setup(config, "features", "drawIgloos", true, "If set to true igloos will be drawn.");
+ drawShipwrecks = setup(config, "features", "drawShipwrecks", true, "If set to true shipwrecks will be drawn.");
+ drawOceanRuins = setup(config, "features", "drawOceanRuins", true, "If set to true ocean ruins will be drawn.");
+ drawBuriedTreasure = setup(config, "features", "drawBuriedTreasures", true, "If set to true buried treasure will be drawn.");
+ drawPillagerOutposts = setup(config, "features", "drawPillagerOutposts", true, "If set to true pillager outposts will be drawn.");
+
+ drawSlimeChunks = setup(config, "features", "drawSlimeChunks", true, "If set to true slime chunks bounding boxes are drawn.");
+ slimeChunkMaxY = setup(config, "features", "slimeChunkMaxY", -1, "The maximum top of the slime chunk bounding box. If set to -1 it will use the value when activated, if set to 0 it will always track the player's feet.");
+
+ drawWorldSpawn = setup(config, "features", "drawWorldSpawn", true, "If set to true world spawn and spawn chunks bounding boxes are drawn.");
+ worldSpawnMaxY = setup(config, "features", "worldSpawnMaxY", -1, "The maximum top of the world spawn bounding boxes. If set to -1 it will use the value when activated, if set to 0 it will always track the players feet.");
+ drawLazySpawnChunks = setup(config, "features", "drawLazySpawnChunks", false, "If set to true the lazy spawn chunks bounding boxes will be drawn.");
+
+ drawMobSpawners = setup(config, "features", "drawMobSpawners", true, "If set to true mob spawners will be drawn.");
+ renderMobSpawnerSpawnArea = setup(config, "features", "renderMobSpawnerSpawnArea", true, "If set to true a box to show the maximum possible spawn area (10x10x4) for a spawner will be drawn");
+ renderMobSpawnerActivationLines = setup(config, "features", "renderMobSpawnerActivationLines", true, "If set to true a red/orange/green line will be drawn to show if the spawner is active");
- fill = SetupBooleanProperty(config, "general", "fill", true, "If set to true the bounding boxes are filled. (default: true)");
- alwaysVisible = SetupBooleanProperty(config, "general", "alwaysVisible", false, "If set to true boxes will be visible even through other blocks. (default: false)");
- keepCacheBetweenSessions = SetupBooleanProperty(config, "general", "keepCacheBetweenSessions", false, "If set to true bounding box caches will be kept between sessions. (default: false)");
- drawVillages = SetupBooleanProperty(config, "features", "drawVillages", true, "If set to true village bounding boxes are drawn. (default: true)");
- renderVillageAsSphere = SetupBooleanProperty(config, "features", "renderVillageAsSphere", true, "If set to true villages will be drawn as a sphere. (default:true)");
- drawIronGolemSpawnArea = SetupBooleanProperty(config, "features", "drawIronGolemSpawnArea", true, "If set to true the iron golem spawn area of the village will be drawn. (default:true)");
- drawVillageDoors = SetupBooleanProperty(config, "features", "drawVillageDoors", false, "If set to true lines between the village centre and doors will be drawn. (default:false)");
- drawDesertTemples = SetupBooleanProperty(config, "features", "drawDesertTemples", true, "If set to true desert temple bounding boxes are drawn. (default: true)");
- drawJungleTemples = SetupBooleanProperty(config, "features", "drawJungleTemples", true, "If set to true jungle temple bounding boxes are drawn. (default: true)");
- drawWitchHuts = SetupBooleanProperty(config, "features", "drawWitchHuts", true, "If set to true witch hut bounding boxes are drawn. (default: true)");
- drawStrongholds = SetupBooleanProperty(config, "features", "drawStrongholds", false, "If set to true stronghold bounding boxes are drawn. (default: false)");
- drawMineShafts = SetupBooleanProperty(config, "features", "drawMineShafts", false, "If set to true mineshaft bounding boxes are drawn. (default: false)");
- drawNetherFortresses = SetupBooleanProperty(config, "features", "drawNetherFortresses", true, "If set to true nether fortress bounding boxes are drawn. (default: true)");
- drawOceanMonuments = SetupBooleanProperty(config, "features", "drawOceanMonuments", true, "If set to true ocean monument bounding boxes are drawn. (default: true)");
- drawSlimeChunks = SetupBooleanProperty(config, "features", "drawSlimeChunks", true, "If set to true slime chunks bounding boxes are drawn. (default: true)");
- slimeChunkMaxY = SetupIntegerProperty(config, "features", "slimeChunkMaxY", -1, "The maximum top of the slime chunk bounding box. If set to -1 it will use the value when activated, if set to 0 it will always track the player's feet. (default: -1)");
- drawWorldSpawn = SetupBooleanProperty(config, "features", "drawWorldSpawn", true, "If set to true world spawn and spawn chunks bounding boxes are drawn. (default: true)");
- worldSpawnMaxY = SetupIntegerProperty(config, "features", "worldSpawnMaxY", -1, "The maximum top of the world spawn bounding boxes. If set to -1 it will use the value when activated, if set to 0 it will always track the players feet. (default: -1)");
- drawLazySpawnChunks = SetupBooleanProperty(config, "features", "drawLazySpawnChunks", false, "If set to true the lazy spawn chunks bounding boxes will be drawn. (default: false)");
- drawEndCities = SetupBooleanProperty(config, "features", "drawEndCities", true, "If set to true end city bounding boxes will be drawn. (default: true)");
- drawMansions = SetupBooleanProperty(config, "features", "drawMansions", true, "If set to true woodland mansions will be drawn. (default: true)");
- drawIgloos = SetupBooleanProperty(config, "features", "drawIgloos", true, "If set to true igloos will be drawn. (default: true)");
- drawShipwrecks = SetupBooleanProperty(config, "features", "drawShipwrecks", true, "If set to true shipwrecks will be drawn. (default: true)");
- drawOceanRuins = SetupBooleanProperty(config, "features", "drawOceanRuins", true, "If set to true ocean ruins will be drawn. (default: true)");
- drawBuriedTreasure = SetupBooleanProperty(config, "features", "drawBuriedTreasures", true, "If set to true buried treasure will be drawn. (default: true)");
- drawMobSpawners = SetupBooleanProperty(config, "features", "drawMobSpawners", true, "If set to true mob spawners will be drawn. (default: true}");
- renderMobSpawnerSpawnArea = SetupBooleanProperty(config, "features", "renderMobSpawnerSpawnArea", true, "If set to true a box to show the maximum possible spawn area (10x10x4) for a spawner will be drawn (default: true)");
- renderMobSpawnerActivationLines = SetupBooleanProperty(config, "features", "renderMobSpawnerActivationLines", true, "If set to true a red/orange/green line will be drawn to show if the spawner is active (default: true)");
- drawPillagerOutposts = SetupBooleanProperty(config, "features", "drawPillagerOutposts", true, "If set to true pillager outposts will be drawn. (default: true)");
config.save();
}
- private static Setting SetupBooleanProperty(Configuration config, String category, String settingName, Boolean defaultValue, String comment) {
- Setting property = config.get(category, settingName, defaultValue);
- property.comment = comment;
- property.set(property.getBoolean(defaultValue));
- return property;
+ private static Configuration loadConfig() {
+ Configuration config = new Configuration(new File(configDir, "BBOutlineReloaded.cfg"));
+ config.load();
+ return config;
+ }
+
+ public static void saveConfig()
+ {
+ Configuration config = new Configuration(new File(configDir, "BBOutlineReloaded.cfg"));
+ for(Setting<?> setting : settings) {
+ config.put(setting);
+ }
+ config.save();
}
- private static Setting SetupIntegerProperty(Configuration config, String category, String settingName, int defaultValue, String comment) {
- Setting property = config.get(category, settingName, defaultValue);
- property.comment = comment;
- property.set(property.getInt(defaultValue));
- return property;
+ private static <T> Setting<T> setup(Configuration config, String category, String settingName, T defaultValue, String comment) {
+ Setting<T> setting = config.get(category, settingName, defaultValue);
+ setting.category = category;
+ setting.name = settingName;
+ setting.comment = comment + " (default: " + defaultValue.toString() + ")";
+ settings.add(setting);
+ return setting;
}
}
import java.io.*;
import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
}
void save() {
- Writer writer = null;
- try {
- writer = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream(file), "utf-8"));
+ try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) {
writer.write("# Configuration file\n");
for (String category : settingsGroup.keySet()) {
writer.write("\n");
writer.write(String.format("%s {\n", category));
- Map<String, Setting> settings = settingsGroup.get(category);
- Boolean first = true;
+ Map<String, Setting<?>> settings = settingsGroup.get(category);
+ boolean first = true;
for (String settingName : settings.keySet()) {
if (!first)
writer.write("\n");
writer.write("}\n");
}
} catch (IOException ignored) {
- } finally {
- try {
- if (writer != null) {
- writer.close();
- }
- } catch (Exception ignored) {
- }
}
}
- private Map<String, Map<String, Setting>> settingsGroup = new HashMap<>();
+ private Map<String, Map<String, Setting<?>>> settingsGroup = new HashMap<>();
void load() {
try {
char type = items[0].charAt(0);
String name = items[1];
String stringValue = items[2];
- Object value = getTypedValue(type, stringValue);
- Setting setting = new Setting(value);
+ Setting setting = getTypedSetting(type, stringValue);
setting.comment = lastCommentLine;
settingsGroup.get(category).put(name, setting);
}
}
}
- private Object getTypedValue(char type, String stringValue) {
+ private Setting<?> getTypedSetting(char type, String value) {
switch (type) {
case 'I':
- return Integer.parseInt(stringValue);
+ return new Setting<>(type, Integer.parseInt(value));
case 'B':
- return stringValue.equals("1") || stringValue.toLowerCase().equals("true");
+ return new Setting<>(type, value.equals("1") || value.toLowerCase().equals("true"));
}
- return stringValue;
+ return new Setting<>(type, value);
}
- public Setting get(String category, String settingName, Object defaultValue) {
+ <T> Setting<T> get(String category, String settingName, T defaultValue) {
+ char type = getType(defaultValue);
if (!settingsGroup.containsKey(category)) {
settingsGroup.put(category, new HashMap<>());
}
- Map<String, Setting> settings = settingsGroup.get(category);
- if (!settings.containsKey(settingName)) {
- settings.put(settingName, new Setting(defaultValue));
+ Map<String, Setting<?>> settings = settingsGroup.get(category);
+ Setting<?> setting = settings.get(settingName);
+ if(setting != null && setting.getType() != type) {
+ setting = null;
+ }
+ if (setting == null) {
+ settings.put(settingName, setting = new Setting<>(type, defaultValue));
+ }
+ return (Setting<T>) setting;
+ }
+
+ private <T> char getType(T defaultValue) {
+ String[] typeNames = defaultValue.getClass().getName().split("[.]");
+ return typeNames[typeNames.length-1].charAt(0);
+ }
+
+ void put(Setting<?> setting) {
+ String category = setting.category;
+ if (!settingsGroup.containsKey(category)) {
+ settingsGroup.put(category, new HashMap<>());
}
- return settings.get(settingName);
+ Map<String, Setting<?>> settings = settingsGroup.get(category);
+ settings.put(setting.name, setting);
}
}