]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/commitdiff
Rework config ready for gui
authorIrtimaled <irtimaled@gmail.com>
Sun, 3 Mar 2019 06:54:42 +0000 (22:54 -0800)
committerIrtimaled <irtimaled@gmail.com>
Sun, 10 Mar 2019 01:43:25 +0000 (17:43 -0800)
13 files changed:
src/main/java/com/irtimaled/bbor/client/ClientProxy.java
src/main/java/com/irtimaled/bbor/client/ClientRenderer.java
src/main/java/com/irtimaled/bbor/client/renderers/MobSpawnerRenderer.java
src/main/java/com/irtimaled/bbor/client/renderers/Renderer.java
src/main/java/com/irtimaled/bbor/client/renderers/SlimeChunkRenderer.java
src/main/java/com/irtimaled/bbor/client/renderers/VillageRenderer.java
src/main/java/com/irtimaled/bbor/client/renderers/WorldSpawnRenderer.java
src/main/java/com/irtimaled/bbor/common/BoundingBoxType.java
src/main/java/com/irtimaled/bbor/common/CommonProxy.java
src/main/java/com/irtimaled/bbor/config/ConfigManager.java
src/main/java/com/irtimaled/bbor/config/Configuration.java
src/main/java/com/irtimaled/bbor/config/Setting.java
src/main/java/com/irtimaled/bbor/config/SettingBase.java [new file with mode: 0644]

index 84e37747d7ede0bae8ca57f7f881920aca370d4c..f5429a33809444934e2f402f842f646a2ad045f2 100644 (file)
@@ -72,7 +72,7 @@ public class ClientProxy extends CommonProxy {
 
     private void disconnectedFromServer() {
         active = false;
-        if (ConfigManager.keepCacheBetweenSessions.getBoolean()) return;
+        if (ConfigManager.keepCacheBetweenSessions.get()) return;
         VillageColorCache.clear();
         clearCaches();
     }
index dc56e753fc1b4da7b425f9cd344b2d7d8415619e..af0d8792e32bcf671e3a8155982f130a16efc408 100644 (file)
@@ -51,7 +51,7 @@ public class ClientRenderer {
         GL11.glDisable(GL11.GL_TEXTURE_2D);
         GL11.glDisable(GL11.GL_CULL_FACE);
 
-        if (ConfigManager.alwaysVisible.getBoolean()) {
+        if (ConfigManager.alwaysVisible.get()) {
             GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
         }
         for (Map.Entry<BoundingBox, Set<BoundingBox>> entry : cache.getBoundingBoxes().entrySet()) {
index 200a901532abf87ddbc0ce3a73ec2ddd00056ff2..7c9935ba264692cb4212ca9f184ac8861451d4be 100644 (file)
@@ -17,13 +17,13 @@ public class MobSpawnerRenderer extends Renderer<BoundingBoxMobSpawner> {
         OffsetPoint centerPoint = new OffsetPoint(center)
                 .add(0.5, 0.5, 0.5);
         double radius = boundingBox.getRadius();
-        if (ConfigManager.renderMobSpawnerSpawnArea.getBoolean()) {
+        if (ConfigManager.renderMobSpawnerSpawnArea.get()) {
             renderBoundingBox(boundingBox);
         } else {
             renderCuboid(new AxisAlignedBB(center, center).expand(1, 1, 1), color, fill());
         }
 
-        if (!ConfigManager.renderMobSpawnerActivationLines.getBoolean()) return;
+        if (!ConfigManager.renderMobSpawnerActivationLines.get()) return;
 
         OffsetPoint playerPos = new OffsetPoint(PlayerData.getX(), PlayerData.getY(), PlayerData.getZ());
         double distance = centerPoint.getDistance(playerPos);
index 3f23c9e9c796cf707fe5fc07f488d5f6e0c318c5..3e4552db950dc19c0d43b93efe99975991f9a73d 100644 (file)
@@ -22,7 +22,7 @@ public abstract class Renderer<T extends BoundingBox> {
     }
 
     boolean fill() {
-        return ConfigManager.fill.getBoolean();
+        return ConfigManager.fill.get();
     }
 
     void renderRectangle(AxisAlignedBB aaBB, double minY, double maxY, Color color, Boolean fill) {
index 173007ac1291d8f6b5bde26edb93060238e75a37..e86e0f5eff4dbee9f7c59aaac2a18b3e5793bf5b 100644 (file)
@@ -14,7 +14,7 @@ public class SlimeChunkRenderer extends Renderer<BoundingBoxSlimeChunk> {
         Color color = boundingBox.getColor();
         renderCuboid(aaBB, color, fill());
 
-        double maxY = PlayerData.getMaxY(ConfigManager.slimeChunkMaxY.getInt());
+        double maxY = PlayerData.getMaxY(ConfigManager.slimeChunkMaxY.get());
         if (maxY > 39) {
             renderRectangle(aaBB, 39, maxY, color, fill());
         }
index e4bd07210114b051d1adc84a4b3d3dfa74ea4028..1a8e603ad3cfba040fa50316f81f5b0080afd94c 100644 (file)
@@ -16,16 +16,16 @@ import java.util.Set;
 public class VillageRenderer extends Renderer<BoundingBoxVillage> {
     @Override
     public void render(BoundingBoxVillage boundingBox) {
-        if (ConfigManager.renderVillageAsSphere.getBoolean()) {
+        if (ConfigManager.renderVillageAsSphere.get()) {
             renderBoundingBoxVillageAsSphere(boundingBox);
         } else {
             renderBoundingBox(boundingBox);
         }
-        if (ConfigManager.drawIronGolemSpawnArea.getBoolean() &&
+        if (ConfigManager.drawIronGolemSpawnArea.get() &&
                 boundingBox.getSpawnsIronGolems()) {
             renderIronGolemSpawnArea(boundingBox);
         }
-        if (ConfigManager.drawVillageDoors.getBoolean()) {
+        if (ConfigManager.drawVillageDoors.get()) {
             renderVillageDoors(boundingBox);
         }
     }
index 96e07acce2a1aaa37fc31677bed627cacf1bdb20..0de2127af54dac91e7caaaf6c6f48cd9003c2e12 100644 (file)
@@ -12,7 +12,7 @@ public class WorldSpawnRenderer extends Renderer<BoundingBoxWorldSpawn> {
     public void render(BoundingBoxWorldSpawn boundingBox) {
         AxisAlignedBB aaBB = boundingBox.toAxisAlignedBB(false);
         Color color = boundingBox.getColor();
-        double y = PlayerData.getMaxY(ConfigManager.worldSpawnMaxY.getInt()) + 0.001F;
+        double y = PlayerData.getMaxY(ConfigManager.worldSpawnMaxY.get()) + 0.001F;
         renderRectangle(aaBB, y, y, color, false);
     }
 }
\ No newline at end of file
index a5360e5a372e6b20981b9acd1f9cce0ea287b482..5b8ba4324ca43911271719dc82b9af613ca6ba4f 100644 (file)
@@ -32,7 +32,7 @@ public class BoundingBoxType {
     public final static BoundingBoxType SlimeChunks = register(Colors.DARK_GREEN, "Slime_Chunks", ConfigManager.drawSlimeChunks);
     public final static BoundingBoxType Village = register(null, "Village", ConfigManager.drawVillages);
 
-    private static BoundingBoxType register(Color color, String name, Setting shouldRenderSetting) {
+    private static BoundingBoxType register(Color color, String name, Setting<Boolean> shouldRenderSetting) {
         return structureTypeMap.computeIfAbsent(name.hashCode(), k -> new BoundingBoxType(color, name, shouldRenderSetting));
     }
 
@@ -42,9 +42,9 @@ public class BoundingBoxType {
 
     private final Color color;
     private final String name;
-    private final Setting shouldRenderSetting;
+    private final Setting<Boolean> shouldRenderSetting;
 
-    private BoundingBoxType(Color color, String name, Setting shouldRenderSetting) {
+    private BoundingBoxType(Color color, String name, Setting<Boolean> shouldRenderSetting) {
         this.color = color;
         this.name = name;
         this.shouldRenderSetting = shouldRenderSetting;
@@ -59,6 +59,6 @@ public class BoundingBoxType {
     }
 
     public Boolean shouldRender() {
-        return shouldRenderSetting.getBoolean();
+        return shouldRenderSetting.get();
     }
 }
index 995a5ffd5033bac19cd779f10a5f030f2dd5721f..e2de4405e21a6b2fcc8f6f41525a0bae64283f68 100644 (file)
@@ -48,7 +48,7 @@ public class CommonProxy {
         EventBus.subscribe(PlayerLoggedOut.class, e -> playerLoggedOut(e.getPlayer()));
         EventBus.subscribe(PlayerSubscribed.class, e -> sendBoundingBoxes(e.getPlayer()));
         EventBus.subscribe(Tick.class, e -> tick());
-        if (ConfigManager.drawVillages.getBoolean()) {
+        if (ConfigManager.drawVillages.get()) {
             EventBus.subscribe(VillageUpdated.class, e -> villageUpdated(e.getDimensionType(), e.getVillage()));
         }
     }
index 891e986c2c61cea84057c41b7f1d0e4263348dc7..f150151cc054fdf1d5c172aad9ba787b00c72b8e 100644 (file)
 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;
     }
 }
index 20485262fde6a6a1b3dbd9f323df222b82d95e44..09f0dba44b0f4e552b64fc3716e58f9f00f14655 100644 (file)
@@ -4,6 +4,7 @@ import com.google.common.io.Files;
 
 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;
@@ -16,16 +17,13 @@ public class Configuration {
     }
 
     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");
@@ -37,17 +35,10 @@ public class Configuration {
                 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 {
@@ -77,8 +68,7 @@ public class Configuration {
                     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);
                 }
@@ -87,24 +77,43 @@ public class Configuration {
         }
     }
 
-    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);
     }
 }
index ec7bbefe92998e5efca2a083e96add20d0508568..0af115ac3f3b01dfb221b9ac02dad26b6e673c9d 100644 (file)
@@ -1,47 +1,22 @@
 package com.irtimaled.bbor.config;
 
-public class Setting {
-    private Object value;
-    String comment;
+public class Setting<T> extends SettingBase {
+    private T value;
 
-    Setting(Object value) {
+    public Setting(char type, T value) {
+        super(type);
         this.value = value;
     }
 
-    public Boolean getBoolean(Boolean defaultValue) {
-        if (value instanceof Boolean)
-            return (Boolean) value;
-
-        return defaultValue;
-    }
-
-    int getInt(int defaultValue) {
-        if (value instanceof Integer)
-            return (Integer) value;
-
-        return defaultValue;
+    public T get() {
+        return value;
     }
 
-    public void set(Object value) {
+    public void set(T value) {
         this.value = value;
     }
 
-    public boolean getBoolean() {
-        return getBoolean(false);
-    }
-
-    public int getInt() {
-        return getInt(0);
-    }
-
-    String getType() {
-        if (value instanceof Integer)
-            return "I";
-        if (value instanceof Boolean)
-            return "B";
-        return "S";
-    }
-
+    @Override
     Object getValue() {
         return value;
     }
diff --git a/src/main/java/com/irtimaled/bbor/config/SettingBase.java b/src/main/java/com/irtimaled/bbor/config/SettingBase.java
new file mode 100644 (file)
index 0000000..418fb9f
--- /dev/null
@@ -0,0 +1,19 @@
+package com.irtimaled.bbor.config;
+
+public abstract class SettingBase {
+    String comment;
+    String category;
+    String name;
+
+    private final char type;
+
+    public SettingBase(char type) {
+        this.type = type;
+    }
+
+    char getType() {
+        return type;
+    }
+
+    abstract Object getValue();
+}