From 9873b7ad56ab8f32e6073dea060c4b67aad8b77e Mon Sep 17 00:00:00 2001 From: Adubbz Date: Sun, 11 Jun 2017 16:39:35 +1000 Subject: [PATCH] Re-added errors for properties using default value (as log spam this time), fixed some errors not showing during json parsing --- .../biomesoplenty/common/biome/BOPBiome.java | 14 ++--- .../common/biome/nether/BOPHellBiome.java | 4 ++ .../biome/overworld/BOPOverworldBiome.java | 3 + .../biome/vanilla/ExtendedBiomeWrapper.java | 4 ++ .../biomesoplenty/common/init/ModBiomes.java | 5 +- .../common/util/config/BOPConfig.java | 60 ++++++++++++------- 6 files changed, 57 insertions(+), 33 deletions(-) diff --git a/src/main/java/biomesoplenty/common/biome/BOPBiome.java b/src/main/java/biomesoplenty/common/biome/BOPBiome.java index 759ba6274..7f492adf5 100644 --- a/src/main/java/biomesoplenty/common/biome/BOPBiome.java +++ b/src/main/java/biomesoplenty/common/biome/BOPBiome.java @@ -72,18 +72,18 @@ public abstract class BOPBiome extends Biome implements IExtendedBiome if (conf.isEmpty()) return defaultBuilder.build(); - defaultBuilder.withTemperature(conf.getFloat("temperature")); - defaultBuilder.withRainfall(conf.getFloat("rainfall")); - defaultBuilder.withWaterColor(conf.getInt("waterColor")); + defaultBuilder.withTemperature(conf.getFloat("temperature", defaultBuilder.temperature)); + defaultBuilder.withRainfall(conf.getFloat("rainfall", defaultBuilder.rainfall)); + defaultBuilder.withWaterColor(conf.getInt("waterColor", defaultBuilder.waterColor)); - Boolean enableRain = conf.getBool("enableRain"); + Boolean enableRain = conf.getBool("enableRain", defaultBuilder.enableRain); if (enableRain != null && !enableRain) defaultBuilder.withRainDisabled(); - Boolean enableSnow = conf.getBool("enableSnow"); + Boolean enableSnow = conf.getBool("enableSnow", defaultBuilder.enableSnow); if (enableSnow != null && enableSnow) defaultBuilder.withSnowEnabled(); - defaultBuilder.withBaseBiome(conf.getString("baseBiome")); - defaultBuilder.withGuiColour(conf.getInt("guiColour")); + defaultBuilder.withBaseBiome(conf.getString("baseBiome", defaultBuilder.baseBiomeRegName)); + defaultBuilder.withGuiColour(conf.getInt("guiColour", defaultBuilder.guiColour)); return defaultBuilder.build(); } diff --git a/src/main/java/biomesoplenty/common/biome/nether/BOPHellBiome.java b/src/main/java/biomesoplenty/common/biome/nether/BOPHellBiome.java index bc5e2cbce..23f53da2d 100644 --- a/src/main/java/biomesoplenty/common/biome/nether/BOPHellBiome.java +++ b/src/main/java/biomesoplenty/common/biome/nether/BOPHellBiome.java @@ -21,6 +21,7 @@ import biomesoplenty.common.init.ModBiomes; import biomesoplenty.common.util.biome.GeneratorUtils; import biomesoplenty.common.world.generator.GeneratorHive; import biomesoplenty.common.world.generator.GeneratorSplatter; +import biomesoplenty.core.BiomesOPlenty; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.monster.EntityEnderman; @@ -72,6 +73,9 @@ public class BOPHellBiome extends BOPBiome this.roofTopBlock = conf.getBlockState("roofTopBlock", this.roofTopBlock); this.roofFillerBlock = conf.getBlockState("roofFillerBlock", this.roofFillerBlock); + // log any warnings from parsing the config file + for (String msg : conf.flushMessages()) + BiomesOPlenty.logger.info(msg); // write default values to a file ModBiomes.writeDefaultConfigFile(ModBiomes.BOP_DEFAULTS_DIR, this.getResourceLocation().getResourcePath(), conf); } diff --git a/src/main/java/biomesoplenty/common/biome/overworld/BOPOverworldBiome.java b/src/main/java/biomesoplenty/common/biome/overworld/BOPOverworldBiome.java index 05f450394..6877fe242 100644 --- a/src/main/java/biomesoplenty/common/biome/overworld/BOPOverworldBiome.java +++ b/src/main/java/biomesoplenty/common/biome/overworld/BOPOverworldBiome.java @@ -119,6 +119,9 @@ public class BOPOverworldBiome extends BOPBiome this.beachBiomeLocation = conf.getResourceLocation("beachBiomeLocation", this.beachBiomeLocation); + // log any warnings from parsing the config file + for (String msg : conf.flushMessages()) + BiomesOPlenty.logger.info(msg); // write default values to a file ModBiomes.writeDefaultConfigFile(ModBiomes.BOP_DEFAULTS_DIR, this.getResourceLocation().getResourcePath(), conf); } diff --git a/src/main/java/biomesoplenty/common/biome/vanilla/ExtendedBiomeWrapper.java b/src/main/java/biomesoplenty/common/biome/vanilla/ExtendedBiomeWrapper.java index 84f95c9ed..2bc692ac5 100644 --- a/src/main/java/biomesoplenty/common/biome/vanilla/ExtendedBiomeWrapper.java +++ b/src/main/java/biomesoplenty/common/biome/vanilla/ExtendedBiomeWrapper.java @@ -23,6 +23,7 @@ import biomesoplenty.common.util.block.BlockQuery; import biomesoplenty.common.world.GenerationManager; import biomesoplenty.common.world.generator.GeneratorColumns; import biomesoplenty.common.world.generator.GeneratorFlora; +import biomesoplenty.core.BiomesOPlenty; import net.minecraft.init.Biomes; import net.minecraft.init.Blocks; import net.minecraft.util.ResourceLocation; @@ -67,6 +68,9 @@ public class ExtendedBiomeWrapper implements IExtendedBiome IConfigObj confGenerators = conf.getObject("generators"); this.generationManager.configure(confGenerators); + // log any warnings from parsing the config file + for (String msg : conf.flushMessages()) + BiomesOPlenty.logger.info(msg); // write default values to a file ModBiomes.writeDefaultConfigFile(ModBiomes.VANILLA_DEFAULTS_DIR, this.getResourceLocation().getResourcePath(), conf); } diff --git a/src/main/java/biomesoplenty/common/init/ModBiomes.java b/src/main/java/biomesoplenty/common/init/ModBiomes.java index e96ba3d95..a69b59a75 100644 --- a/src/main/java/biomesoplenty/common/init/ModBiomes.java +++ b/src/main/java/biomesoplenty/common/init/ModBiomes.java @@ -540,10 +540,7 @@ public class ModBiomes implements BOPBiomes.IBiomeRegistry public static IConfigObj readConfigFile(String idName) { File configFile = new File(new File(BiomesOPlenty.configDirectory, "biomes"), idName + ".json"); - IConfigObj conf = new BOPConfig.ConfigFileObj(configFile); - - // log any warnings from parsing the config file - for (String msg : conf.flushMessages()) {BiomesOPlenty.logger.info(msg);} + IConfigObj conf = new BOPConfig.ConfigFileObj(configFile, false, true); return conf; } diff --git a/src/main/java/biomesoplenty/common/util/config/BOPConfig.java b/src/main/java/biomesoplenty/common/util/config/BOPConfig.java index 041cb8d2c..6ba475834 100644 --- a/src/main/java/biomesoplenty/common/util/config/BOPConfig.java +++ b/src/main/java/biomesoplenty/common/util/config/BOPConfig.java @@ -58,11 +58,13 @@ public class BOPConfig protected Map childObjs; protected List messages = new ArrayList(); protected String prefix = ""; + protected boolean warnIfDefault; - private ConfigObjBase() + private ConfigObjBase(boolean warnIfDefault) { this.defaults = Maps.newHashMap(); this.childObjs = Maps.newHashMap(); + this.warnIfDefault = warnIfDefault; } public void parse(String jsonString) @@ -155,13 +157,7 @@ public class BOPConfig @Override public IConfigObj getObject(String name, boolean warnIfMissing) { - if (!this.has(name)) - { - if (warnIfMissing) - { - this.addMessage(name, "Error - missing value"); - } - } + JsonObject obj = new JsonObject(); // attempt to return cached child first if (this.childObjs.containsKey(name)) @@ -169,16 +165,25 @@ public class BOPConfig return this.childObjs.get(name); } - JsonObject obj = new JsonObject(); - - try + // check if the object exists and read it if it does + if (!this.has(name)) { - obj = this.members.get(name).getAsJsonObject(); - } catch (Exception e) { - this.addMessage("Error fetching object " + name + ": " + e.getMessage()); + if (warnIfMissing) + { + this.addMessage(name, "Error - missing value"); + } + } + else + { + try + { + obj = this.members.get(name).getAsJsonObject(); + } catch (Exception e) { + this.addMessage("Error fetching object " + name + ": " + e.getMessage()); + } } - ConfigChildObj childObj = new ConfigChildObj(this.prefix + "." + name, obj); + ConfigChildObj childObj = new ConfigChildObj(this.prefix + "." + name, obj, this.warnIfDefault); // store the child for later serialization this.childObjs.put(name, childObj); return childObj; @@ -353,7 +358,12 @@ public class BOPConfig return defaultVal; } T out = this.as(this.members.get(name), type, name); - return out == null ? defaultVal : out; + // warn people who try to copy-paste default configs + if (this.warnIfDefault && out != null && out.equals(defaultVal)) + { + this.addMessage("You can't set a property to its default value, only changed properties can be included in config files. \n Property: " + name + ", Value: " + out); + } + return out == null ? defaultVal : out; } private ArrayList getArray(String name, ArrayList defaultVal, boolean warnIfMissing, Types type) @@ -586,7 +596,7 @@ public class BOPConfig case RESOURCELOCATION: return fromResourceLocation((ResourceLocation)value); default: - BiomesOPlenty.logger.error("Undefined type " + type); + this.addMessage("Undefined type " + type); return null; } } @@ -620,6 +630,7 @@ public class BOPConfig { public ConfigObj(String jsonString) { + super(false); this.parse(jsonString); } } @@ -629,11 +640,12 @@ public class BOPConfig { public ConfigFileObj(File configFile) { - this(configFile, false); + this(configFile, false, false); } - public ConfigFileObj(File configFile, boolean warnIfMissing) + public ConfigFileObj(File configFile, boolean warnIfMissing, boolean warnIfDefault) { + super(warnIfDefault); this.prefix = configFile.getName(); String jsonString = null; if (configFile.exists()) @@ -658,8 +670,9 @@ public class BOPConfig // Concrete class for a config object which is a child of another config object public static class ConfigChildObj extends ConfigObjBase { - protected ConfigChildObj(String prefix, JsonObject obj) + protected ConfigChildObj(String prefix, JsonObject obj, boolean warnIfDefault) { + super(warnIfDefault); this.prefix = prefix; this.members = new HashMap(); for (Entry entry : obj.entrySet()) @@ -667,7 +680,10 @@ public class BOPConfig this.members.put(entry.getKey(), entry.getValue()); } } + + protected ConfigChildObj(String prefix, JsonObject obj) + { + this(prefix, obj, false); + } } - - } \ No newline at end of file