diff --git a/src/main/java/biomesoplenty/api/biome/BOPBiome.java b/src/main/java/biomesoplenty/api/biome/BOPBiome.java index 85109f5c1..8b3c359c0 100644 --- a/src/main/java/biomesoplenty/api/biome/BOPBiome.java +++ b/src/main/java/biomesoplenty/api/biome/BOPBiome.java @@ -70,6 +70,16 @@ public class BOPBiome extends BiomeGenBase implements IExtendedBiome } } + // Allow generators to be configured + WrappedJsonObject confGenerators = conf.getObject("generators"); + if (confGenerators != null) + { + for (String name : confGenerators.getKeys()) + { + this.generationManager.configureWith(name, confGenerators.getObject(name)); + } + } + } diff --git a/src/main/java/biomesoplenty/api/biome/generation/GenerationManager.java b/src/main/java/biomesoplenty/api/biome/generation/GenerationManager.java index c162a9247..0023fdeb5 100644 --- a/src/main/java/biomesoplenty/api/biome/generation/GenerationManager.java +++ b/src/main/java/biomesoplenty/api/biome/generation/GenerationManager.java @@ -8,68 +8,124 @@ package biomesoplenty.api.biome.generation; -import java.util.Collection; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; -import java.util.Map.Entry; -import com.google.common.collect.HashBasedTable; +import biomesoplenty.common.util.config.ConfigHelper.WrappedJsonObject; +import biomesoplenty.common.world.feature.*; +import biomesoplenty.common.world.feature.tree.*; + import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; -import com.google.common.collect.Table; public class GenerationManager { - private Table generatorTable = HashBasedTable.create(); - + private Map generators = new HashMap(); + public void addGenerator(String name, GeneratorStage stage, IGenerator generator) { - if (!this.generatorTable.containsColumn(name)) - { - generator.setName(name); - generator.setStage(stage); - - this.generatorTable.put(stage, name, generator); - } - else + if (this.generators.containsKey(name)) { throw new RuntimeException("A generator with name " + name + " already exists!"); } + generator.setName(name); + generator.setStage(stage); + this.generators.put(name, generator); } public ImmutableCollection getGeneratorsForStage(GeneratorStage stage) { - Map columnMap = this.generatorTable.rowMap().get(stage); - Collection result = columnMap == null ? null : columnMap.values(); - - return result == null ? ImmutableList.of() : ImmutableList.copyOf(result); + ArrayList out = new ArrayList(); + for (IGenerator generator : this.generators.values()) + { + if (generator.getStage() == stage) + { + out.add(generator); + } + } + return ImmutableList.copyOf(out); } - public Map createGeneratorMap() + public void removeGenerator(String name) { - Map result = new HashMap(); - - for (IGenerator generator : this.generatorTable.values()) - { - result.put(generator.getName(), generator); - } - - return result; + this.generators.remove(name); } - public void createGeneratorTable(Map generators) + public IGenerator getGenerator(String name) { - Table result = HashBasedTable.create(); - - for (Entry entry : generators.entrySet()) - { - String name = entry.getKey(); - IGenerator generator = entry.getValue(); + return this.generators.get(name); + } - generator.setName(name); - result.put(generator.getStage(), generator.getName(), generator); + public void configureWith(String name, WrappedJsonObject conf) + { + if (this.generators.containsKey(name)) + { + if (conf.getBool("enable", true)) + { + // configure the existing generator + this.generators.get(name).configure(conf); + } + else + { + // remove this generator + this.generators.remove(name); + } + } + else + { + // there was previously no generator of this name - attempt to add it + IGenerator generator = GeneratorFactory.create(conf); + if (generator != null) + { + this.generators.put(name, generator); + } + } + } + + // TODO: use GeneratorRegistry instead? + public enum GeneratorFactory + { + FLORA, DOUBLE_FLORA, GRASS, ORE_CLUSTER, ORE_SINGLE, WATERSIDE, BASIC_TREE, BIG_TREE, BUSH; + + public static IGenerator create(WrappedJsonObject conf) + { + GeneratorStage stage = conf.getEnum("stage", null, GeneratorStage.class); + GeneratorFactory factory = conf.getEnum("type", null, GeneratorFactory.class); + if (stage == null || factory == null) {return null;} + IGenerator generator = factory.create(); + generator.setStage(stage); + generator.configure(conf); + return generator; + } + + public IGenerator create() + { + switch (this) + { + case FLORA: + return new GeneratorFlora(); + case DOUBLE_FLORA: + return new GeneratorDoubleFlora(); + case GRASS: + return new GeneratorGrass(); + case ORE_CLUSTER: + return new GeneratorOreCluster(); + case ORE_SINGLE: + return new GeneratorOreSingle(); + case WATERSIDE: + return new GeneratorWaterside(); + case BASIC_TREE: + return new GeneratorBasicTree(); + case BIG_TREE: + return new GeneratorBigTree(); + case BUSH: + return new GeneratorBush(); + default: + return null; + } } - this.generatorTable = result; } + } \ No newline at end of file diff --git a/src/main/java/biomesoplenty/api/biome/generation/GeneratorCustomizable.java b/src/main/java/biomesoplenty/api/biome/generation/GeneratorCustomizable.java index e66230685..c15d862f6 100644 --- a/src/main/java/biomesoplenty/api/biome/generation/GeneratorCustomizable.java +++ b/src/main/java/biomesoplenty/api/biome/generation/GeneratorCustomizable.java @@ -8,6 +8,8 @@ package biomesoplenty.api.biome.generation; +import biomesoplenty.common.util.config.ConfigHelper.WrappedJsonObject; + public abstract class GeneratorCustomizable implements IGenerator { private final String identifier; @@ -53,4 +55,10 @@ public abstract class GeneratorCustomizable implements IGenerator { return this.identifier; } + + @Override + public void configure(WrappedJsonObject conf) + { + ; + } } diff --git a/src/main/java/biomesoplenty/api/biome/generation/GeneratorWeighted.java b/src/main/java/biomesoplenty/api/biome/generation/GeneratorWeighted.java index e3670a257..c499b9ad0 100644 --- a/src/main/java/biomesoplenty/api/biome/generation/GeneratorWeighted.java +++ b/src/main/java/biomesoplenty/api/biome/generation/GeneratorWeighted.java @@ -15,12 +15,9 @@ import java.util.Random; import net.minecraft.util.BlockPos; import net.minecraft.util.WeightedRandom; import net.minecraft.world.World; +import biomesoplenty.common.util.config.ConfigHelper.WrappedJsonObject; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; -import com.google.gson.reflect.TypeToken; - +// TODO implement so that we don't rely on minecraft WeightedRandom class and GeneratorWeightedEntry class - can be much simpler public class GeneratorWeighted extends GeneratorCustomizable { private int amountPerChunk; @@ -57,18 +54,28 @@ public class GeneratorWeighted extends GeneratorCustomizable return generator.generate(world, random, pos); } - + @Override - public void writeToJson(JsonObject json, JsonSerializationContext context) + public void configure(WrappedJsonObject conf) { - json.addProperty("amount_per_chunk", this.amountPerChunk); - json.add("entries", context.serialize(this.weightedEntries)); - } - - @Override - public void readFromJson(JsonObject json, JsonDeserializationContext context) - { - this.amountPerChunk = json.get("amount_per_chunk").getAsInt(); - this.weightedEntries = context.deserialize(json.get("entries"), new TypeToken>() {}.getType()); + this.amountPerChunk = conf.getInt("amountPerChunk", this.amountPerChunk); + ArrayList weightedEntriesConf = conf.getObjectArray("weightedEntries"); + if (!weightedEntriesConf.isEmpty()) + { + this.weightedEntries.clear(); + for (WrappedJsonObject weightedEntryConf : weightedEntriesConf) + { + Integer weight = weightedEntryConf.getInt("weight", null); + if (weight == null || weight.intValue() < 1) {continue;} + WrappedJsonObject generatorConf = weightedEntryConf.getObject("generator"); + if (generatorConf == null) {continue;} + IGenerator generator = GenerationManager.GeneratorFactory.create(generatorConf); + if (generator == null) {continue;} + this.add(weight, generator); + } + } + // TODO: at the moment, because the weighted entries aren't named, there's no way to just adjust one part of one of them + // The only thing you can do is replace the whole array. Perhaps should use named entries in a Map kind of arrangement + // Then they could be individually altered } } diff --git a/src/main/java/biomesoplenty/api/biome/generation/GeneratorWeightedEntry.java b/src/main/java/biomesoplenty/api/biome/generation/GeneratorWeightedEntry.java index 4e2718281..8b249ad94 100644 --- a/src/main/java/biomesoplenty/api/biome/generation/GeneratorWeightedEntry.java +++ b/src/main/java/biomesoplenty/api/biome/generation/GeneratorWeightedEntry.java @@ -13,10 +13,7 @@ import java.util.Random; import net.minecraft.util.BlockPos; import net.minecraft.util.WeightedRandom; import net.minecraft.world.World; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; +import biomesoplenty.common.util.config.ConfigHelper.WrappedJsonObject; public final class GeneratorWeightedEntry extends WeightedRandom.Item implements IGenerator { @@ -56,18 +53,12 @@ public final class GeneratorWeightedEntry extends WeightedRandom.Item implements } @Override - public void writeToJson(JsonObject json, JsonSerializationContext context) + public void configure(WrappedJsonObject conf) { - json.addProperty("weight", this.itemWeight); - json.add("wrapped_generator", context.serialize(this.wrappedGenerator)); - } - - @Override - public void readFromJson(JsonObject json, JsonDeserializationContext context) - { - this.itemWeight = json.get("weight").getAsInt(); - this.wrappedGenerator = context.deserialize(json.get("wrapped_generator"), IGenerator.class); + // this should never be used directly + ; } + @Override public void setStage(GeneratorStage stage) @@ -96,4 +87,5 @@ public final class GeneratorWeightedEntry extends WeightedRandom.Item implements { return null; } + } diff --git a/src/main/java/biomesoplenty/api/biome/generation/IGenerator.java b/src/main/java/biomesoplenty/api/biome/generation/IGenerator.java index 46a7d96d9..cb0b23b74 100644 --- a/src/main/java/biomesoplenty/api/biome/generation/IGenerator.java +++ b/src/main/java/biomesoplenty/api/biome/generation/IGenerator.java @@ -12,10 +12,7 @@ import java.util.Random; import net.minecraft.util.BlockPos; import net.minecraft.world.World; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; +import biomesoplenty.common.util.config.ConfigHelper.WrappedJsonObject; public interface IGenerator { @@ -33,6 +30,5 @@ public interface IGenerator public String getIdentifier(); public GeneratorStage getStage(); - public void writeToJson(JsonObject json, JsonSerializationContext context); - public void readFromJson(JsonObject json, JsonDeserializationContext context); + public void configure(WrappedJsonObject conf); } diff --git a/src/main/java/biomesoplenty/common/biome/overworld/BiomeGenShrubland.java b/src/main/java/biomesoplenty/common/biome/overworld/BiomeGenShrubland.java index 5ab9d3083..6cd672766 100644 --- a/src/main/java/biomesoplenty/common/biome/overworld/BiomeGenShrubland.java +++ b/src/main/java/biomesoplenty/common/biome/overworld/BiomeGenShrubland.java @@ -8,8 +8,6 @@ package biomesoplenty.common.biome.overworld; -import static biomesoplenty.common.block.BlockBOPDoublePlant.VARIANT; -import static biomesoplenty.common.block.BlockDoubleDecoration.HALF; import net.minecraft.block.BlockFlower.EnumFlowerType; import net.minecraft.block.BlockTallGrass; import net.minecraft.entity.passive.EntityHorse; @@ -21,7 +19,6 @@ import biomesoplenty.api.biome.generation.GeneratorWeighted; import biomesoplenty.api.block.BOPBlocks; import biomesoplenty.common.block.BlockBOPDoublePlant; import biomesoplenty.common.block.BlockBOPPlant; -import biomesoplenty.common.block.BlockDoubleDecoration.Half; import biomesoplenty.common.block.BlockGem; import biomesoplenty.common.enums.BOPGems; import biomesoplenty.common.enums.BOPPlants; @@ -49,7 +46,7 @@ public class BiomeGenShrubland extends BOPBiome this.addGenerator("gravel", GeneratorStage.SAND_PASS2, new GeneratorWaterside(4, 7, Blocks.gravel.getDefaultState())); this.addGenerator("bushes", GeneratorStage.FLOWERS, new GeneratorFlora(7, BlockBOPPlant.paging.getVariantState(BOPPlants.BUSH))); this.addGenerator("shrubs", GeneratorStage.FLOWERS, new GeneratorFlora(5, BlockBOPPlant.paging.getVariantState(BOPPlants.SHRUB))); - this.addGenerator("flax", GeneratorStage.FLOWERS, new GeneratorDoubleFlora(1, BOPBlocks.double_plant.getDefaultState().withProperty(VARIANT, BlockBOPDoublePlant.DoublePlantType.FLAX).withProperty(HALF, Half.LOWER), BOPBlocks.double_plant.getDefaultState().withProperty(VARIANT, BlockBOPDoublePlant.DoublePlantType.FLAX).withProperty(HALF, Half.UPPER), 24)); + this.addGenerator("flax", GeneratorStage.FLOWERS, new GeneratorDoubleFlora(1, BlockBOPDoublePlant.DoublePlantType.FLAX, 24)); this.addGenerator("water_reeds", GeneratorStage.LILYPAD, new GeneratorFlora(3, BlockBOPPlant.paging.getVariantState(BOPPlants.REED), 128)); this.addGenerator("trees", GeneratorStage.TREE, new GeneratorBush(1, Blocks.log.getDefaultState(), Blocks.leaves.getDefaultState())); diff --git a/src/main/java/biomesoplenty/common/util/config/ConfigHelper.java b/src/main/java/biomesoplenty/common/util/config/ConfigHelper.java index 59c39f034..a87a2c7d2 100644 --- a/src/main/java/biomesoplenty/common/util/config/ConfigHelper.java +++ b/src/main/java/biomesoplenty/common/util/config/ConfigHelper.java @@ -27,7 +27,6 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import biomesoplenty.common.util.block.BlockStateUtils; -import biomesoplenty.common.util.config.JsonBlockState; import biomesoplenty.core.BiomesOPlenty; public class ConfigHelper @@ -35,7 +34,6 @@ public class ConfigHelper public static Gson serializer = new GsonBuilder().setPrettyPrinting().create(); public static JsonParser parser = new JsonParser(); - public JsonBlockState blockStateParser = new JsonBlockState(); public WrappedJsonObject root = null; public ArrayList messages = new ArrayList(); @@ -115,41 +113,41 @@ public class ConfigHelper { return this.root == null ? new ArrayList() : this.root.getObjectArray(name); } - public ArrayList getBoolArray(String name) + public ArrayList getBoolArray(String name, ArrayList defaultVal) { - return this.root == null ? new ArrayList() : this.root.getBoolArray(name); + return this.root == null ? new ArrayList() : this.root.getBoolArray(name, defaultVal); } public Boolean getBool(String name, Boolean defaultVal) { return this.root == null ? defaultVal : this.root.getBool(name, defaultVal); } - public ArrayList getStringArray(String name) + public ArrayList getStringArray(String name, ArrayList defaultVal) { - return this.root == null ? new ArrayList() : this.root.getStringArray(name); + return this.root == null ? new ArrayList() : this.root.getStringArray(name, defaultVal); } public String getString(String name, String defaultVal) { return this.root == null ? defaultVal : this.root.getString(name, defaultVal); } - public ArrayList getIntArray(String name) + public ArrayList getIntArray(String name, ArrayList defaultVal) { - return this.root == null ? new ArrayList() : this.root.getIntArray(name); + return this.root == null ? new ArrayList() : this.root.getIntArray(name, defaultVal); } public Integer getInt(String name, Integer defaultVal) { return this.root == null ? defaultVal : this.root.getInt(name, defaultVal); } - public ArrayList getFloatArray(String name) + public ArrayList getFloatArray(String name, ArrayList defaultVal) { - return this.root == null ? new ArrayList() : this.root.getFloatArray(name); + return this.root == null ? new ArrayList() : this.root.getFloatArray(name, defaultVal); } public Float getFloat(String name, Float defaultVal) { return this.root == null ? defaultVal : this.root.getFloat(name, defaultVal); } - public ArrayList getBlockStateArray(String name) + public ArrayList getBlockStateArray(String name, ArrayList defaultVal) { - return this.root == null ? new ArrayList() : this.root.getBlockStateArray(name); + return this.root == null ? new ArrayList() : this.root.getBlockStateArray(name, defaultVal); } public IBlockState getBlockState(String name, IBlockState defaultVal) { @@ -174,6 +172,16 @@ public class ConfigHelper this.conf = conf; } + public ArrayList getKeys() + { + ArrayList out = new ArrayList(); + for (Entry entry : this.obj.entrySet()) + { + out.add(entry.getKey()); + } + return out; + } + public WrappedJsonObject getObject(String name) { if (this.obj == null || !this.obj.has(name)) {return null;} @@ -213,20 +221,41 @@ public class ConfigHelper } return list; } - - public ArrayList getBoolArray(String name) {return this.getArray(name, Types.BOOLEAN);} + + public ArrayList getBoolArray(String name, ArrayList defaultVal) {return this.getArray(name, defaultVal, Types.BOOLEAN);} public Boolean getBool(String name, Boolean defaultVal) {return this.get(name, defaultVal, Types.BOOLEAN);} - public ArrayList getStringArray(String name) {return this.getArray(name, Types.STRING);} + public ArrayList getStringArray(String name, ArrayList defaultVal) {return this.getArray(name, defaultVal, Types.STRING);} public String getString(String name, String defaultVal) {return this.get(name, defaultVal, Types.STRING);} - public ArrayList getIntArray(String name) {return this.getArray(name, Types.INTEGER);} + public ArrayList getIntArray(String name, ArrayList defaultVal) {return this.getArray(name, defaultVal, Types.INTEGER);} public Integer getInt(String name, Integer defaultVal) {return this.get(name, defaultVal, Types.INTEGER);} - public ArrayList getFloatArray(String name) {return this.getArray(name, Types.FLOAT);} + public ArrayList getFloatArray(String name, ArrayList defaultVal) {return this.getArray(name, defaultVal, Types.FLOAT);} public Float getFloat(String name, Float defaultVal) {return this.get(name, defaultVal, Types.FLOAT);} - public ArrayList getBlockStateArray(String name) {return this.getArray(name, Types.BLOCKSTATE);} + public ArrayList getBlockStateArray(String name, ArrayList defaultVal) {return this.getArray(name, defaultVal, Types.BLOCKSTATE);} public IBlockState getBlockState(String name, IBlockState defaultVal) {return this.get(name, defaultVal, Types.BLOCKSTATE);} - - + public ArrayList getEnumArray(String name, ArrayList defaultVal, Class clazz) + { + if (this.obj == null || !this.obj.has(name)) {return defaultVal;} + ArrayList list = new ArrayList(); + try + { + JsonArray arr = this.obj.getAsJsonArray(name); + for (int i = 0; i < arr.size(); i++) + { + E ele = this.asEnum(arr.get(i), clazz); + if (ele != null) {list.add(ele);} + } + } catch (Exception e) { + this.conf.addMessage("Error fetching " + clazz.getName() + " array: " + e.getMessage()); + } + return list; + } + public E getEnum(String name, E defaultVal, Class clazz) + { + if (this.obj == null || !this.obj.has(name)) {return defaultVal;} + E out = this.asEnum(this.obj.get(name), clazz); + return out == null ? defaultVal : out; + } private T get(String name, T defaultVal, Types type) { @@ -235,21 +264,20 @@ public class ConfigHelper return out == null ? defaultVal : out; } - private ArrayList getArray(String name, Types type) + private ArrayList getArray(String name, ArrayList defaultVal, Types type) { + if (this.obj == null || !this.obj.has(name)) {return defaultVal;} ArrayList list = new ArrayList(); - if (this.obj != null && this.obj.has(name)) { - try + try + { + JsonArray arr = this.obj.getAsJsonArray(name); + for (int i = 0; i < arr.size(); i++) { - JsonArray arr = this.obj.getAsJsonArray(name); - for (int i = 0; i < arr.size(); i++) - { - T ele = this.as(arr.get(i), type); - if (ele != null) {list.add(ele);} - } - } catch (Exception e) { - this.conf.addMessage("Error fetching " + type.toString().toLowerCase() + " array: " + e.getMessage()); + T ele = this.as(arr.get(i), type); + if (ele != null) {list.add(ele);} } + } catch (Exception e) { + this.conf.addMessage("Error fetching " + type.toString().toLowerCase() + " array: " + e.getMessage()); } return list; } @@ -271,6 +299,32 @@ public class ConfigHelper return null; } } + + public E asEnum(JsonElement ele, Classclazz) + { + try + { + String val = ele.getAsString(); + E[] enums = clazz.getEnumConstants(); + if (enums == null) + { + this.conf.addMessage("Class " + clazz.getName() + " contains no enum constants"); + return null; + } + for (E enumVal : enums) + { + if (enumVal.name().equalsIgnoreCase(val)) + { + return enumVal; + } + } + this.conf.addMessage("Value " + val + " does not exist in enum " + clazz); + return null; + } catch (Exception e) { + this.conf.addMessage("Error fetching string: " + e.getMessage()); + return null; + } + } public Boolean asBool(JsonElement ele) { diff --git a/src/main/java/biomesoplenty/common/util/config/GeneratorTypeAdaptor.java b/src/main/java/biomesoplenty/common/util/config/GeneratorTypeAdaptor.java deleted file mode 100644 index 4bed47de7..000000000 --- a/src/main/java/biomesoplenty/common/util/config/GeneratorTypeAdaptor.java +++ /dev/null @@ -1,95 +0,0 @@ -/******************************************************************************* - * Copyright 2014-2015, the Biomes O' Plenty Team - * - * This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License. - * - * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/. - ******************************************************************************/ - -package biomesoplenty.common.util.config; - -import java.lang.reflect.Type; - -import biomesoplenty.api.biome.generation.GeneratorRegistry; -import biomesoplenty.api.biome.generation.GeneratorStage; -import biomesoplenty.api.biome.generation.IGenerator; - -import com.google.common.reflect.TypeToken; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; -import com.google.gson.JsonSyntaxException; - -public class GeneratorTypeAdaptor implements JsonSerializer, JsonDeserializer -{ - @Override - public JsonElement serialize(IGenerator src, Type typeOfSrc, JsonSerializationContext context) - { - JsonObject jsonObject = new JsonObject(); - src.writeToJson(jsonObject, context); - - jsonObject.addProperty("generator", src.getIdentifier()); - jsonObject.add("stage", context.serialize(src.getStage())); - - return jsonObject; - } - - @Override - public IGenerator deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException - { - JsonObject jsonObject = json.getAsJsonObject(); - - if (jsonObject.has("generator")) - { - String generatorIdentifier = jsonObject.get("generator").getAsString(); - Class generatorClass = GeneratorRegistry.getGeneratorClass(generatorIdentifier); - - if (generatorClass == null) - { - throw new JsonSyntaxException("Generator " + generatorIdentifier + " doesn't exist"); - } - else - { - IGenerator generator; - try - { - generator = (IGenerator)generatorClass.newInstance(); - - Type generatorStageType = new TypeToken() {}.getType(); - GeneratorStage generatorStage = (GeneratorStage)context.deserialize(jsonObject.get("stage"), generatorStageType); - String generatorStageName = jsonObject.get("stage") != null ? jsonObject.get("stage").getAsString() : null; - - if (generatorStage == null) - { - throw new JsonSyntaxException("Generator stage " + generatorStageName + " is invalid"); - } - else - { - generator.setStage((GeneratorStage)context.deserialize(jsonObject.get("stage"), generatorStageType)); - generator.readFromJson(jsonObject, context); - - return generator; - } - } - catch (InstantiationException e) - { - throw new RuntimeException("Generators must have a no-args constructor", e); - } - catch (IllegalAccessException e) - { - e.printStackTrace(); - } - } - } - else - { - throw new JsonSyntaxException("Entry missing generator property"); - } - - return null; - } -} diff --git a/src/main/java/biomesoplenty/common/util/config/JsonBiome.java b/src/main/java/biomesoplenty/common/util/config/JsonBiome.java deleted file mode 100644 index 27a49be80..000000000 --- a/src/main/java/biomesoplenty/common/util/config/JsonBiome.java +++ /dev/null @@ -1,158 +0,0 @@ -/******************************************************************************* - * Copyright 2014, the Biomes O' Plenty Team - * - * This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License. - * - * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/. - ******************************************************************************/ - -package biomesoplenty.common.util.config; - -import java.util.ArrayList; -import java.util.Map; -import java.util.Map.Entry; - -import net.minecraft.block.state.IBlockState; -import net.minecraft.world.biome.BiomeGenBase; -import net.minecraftforge.common.BiomeManager.BiomeEntry; -import net.minecraftforge.common.BiomeManager.BiomeType; -import biomesoplenty.api.biome.BiomeOwner; -import biomesoplenty.api.biome.IExtendedBiome; -import biomesoplenty.api.biome.generation.GenerationManager; -import biomesoplenty.api.biome.generation.IGenerator; -import biomesoplenty.common.biome.BOPBiomeManager; -import biomesoplenty.common.biome.ExtendedBiomeRegistry; - -import com.google.common.base.Optional; -import com.google.gson.FieldNamingPolicy; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -public class JsonBiome -{ - public static final Gson serializer = new GsonBuilder().setPrettyPrinting().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).registerTypeHierarchyAdapter(IBlockState.class, new JsonBlockState()).registerTypeHierarchyAdapter(IGenerator.class, new GeneratorTypeAdaptor()).create(); - - public String biomeName; - public int biomeId; - public Map weights; - public IBlockState topBlock; - public IBlockState fillerBlock; - public float rootHeight; - public float rootVariation; - public float temperature; - public float rainfall; - public int color; - public int waterColorMultiplier; - public ArrayList entities; - public Map decoration; - - public static JsonBiome createFromBiomeGenBase(BiomeGenBase baseBiome) - { - JsonBiome biome = new JsonBiome(); - - biome.biomeId = baseBiome.biomeID; - biome.biomeName = baseBiome.biomeName; - biome.topBlock = baseBiome.topBlock; - biome.fillerBlock = baseBiome.fillerBlock; - biome.rootHeight = baseBiome.minHeight; - biome.rootVariation = baseBiome.maxHeight; - biome.temperature = baseBiome.temperature; - biome.rainfall = baseBiome.rainfall; - biome.color = baseBiome.color; - biome.waterColorMultiplier = baseBiome.waterColorMultiplier; - biome.entities = JsonEntitySpawn.getBiomeEntitySpawns(baseBiome); - - IExtendedBiome extendedBiome = ExtendedBiomeRegistry.getExtension(baseBiome); - - if (extendedBiome != null) - { - GenerationManager generationManager = extendedBiome.getGenerationManager(); - - biome.weights = extendedBiome.getWeightMap(); - biome.decoration = generationManager.createGeneratorMap(); - - if (extendedBiome.getBiomeOwner() == BiomeOwner.BIOMESOPLENTY) - { - //Add the biome to the array if it is ours because the registration in configureBiomeWithJson is - //not called on the first run - BiomeGenBase.getBiomeGenArray()[baseBiome.biomeID] = baseBiome; - } - - //TODO: Add a system for making Vanilla biome weights configurable. This won't necessarily be in this class, however it's worth noting. - for (Entry entry : extendedBiome.getWeightMap().entrySet()) - { - if (entry != null) - { - BiomeType biomeType = entry.getKey(); - int weight = entry.getValue(); - - BOPBiomeManager.addBiome(biomeType, new BiomeEntry(baseBiome, weight)); - } - } - } - - return biome; - } - - public static Optional configureBiomeWithJson(BiomeGenBase biome, JsonBiome jsonBiome) - { - IExtendedBiome extendedBiome = ExtendedBiomeRegistry.getExtension(biome); - - if (extendedBiome != null) - { - if (extendedBiome.getBiomeOwner() == BiomeOwner.BIOMESOPLENTY) - { - if (jsonBiome.biomeId != -1) - { - biome.biomeID = jsonBiome.biomeId; - BiomeGenBase.getBiomeGenArray()[jsonBiome.biomeId] = biome; - } - else - { - return Optional.absent(); - } - } - - Map weightMap = jsonBiome.weights; - - //Removes the default weights set by us as they are about to be set from the config file - extendedBiome.clearWeights(); - - //TODO: Add a system for making Vanilla biome weights configurable. This won't necessarily be in this class, however it's worth noting. - if (biome.biomeID != -1) - { - for (Entry entry : weightMap.entrySet()) - { - if (entry != null) - { - BiomeType biomeType = entry.getKey(); - int weight = entry.getValue(); - - //Updates the biome's weights to be in line with the config file - extendedBiome.addWeight(biomeType, weight); - BOPBiomeManager.addBiome(biomeType, new BiomeEntry(biome, weight)); - } - } - } - - biome.biomeName = jsonBiome.biomeName; - biome.topBlock = jsonBiome.topBlock; - biome.fillerBlock = jsonBiome.fillerBlock; - biome.setHeight(new BiomeGenBase.Height(jsonBiome.rootHeight, jsonBiome.rootVariation)); - biome.temperature = jsonBiome.temperature; - biome.rainfall = jsonBiome.rainfall; - // TODO: Reflect and modify enableRain and enableSnow - biome.color = jsonBiome.color; - biome.waterColorMultiplier = jsonBiome.waterColorMultiplier; - JsonEntitySpawn.addBiomeEntitySpawns(biome, jsonBiome); - - GenerationManager generationManager = extendedBiome.getGenerationManager(); - - generationManager.createGeneratorTable(jsonBiome.decoration); - - return Optional.of(biome); - } - - return Optional.absent(); - } -} diff --git a/src/main/java/biomesoplenty/common/util/config/JsonBlockState.java b/src/main/java/biomesoplenty/common/util/config/JsonBlockState.java deleted file mode 100644 index ddd463d53..000000000 --- a/src/main/java/biomesoplenty/common/util/config/JsonBlockState.java +++ /dev/null @@ -1,99 +0,0 @@ -/******************************************************************************* - * Copyright 2014, the Biomes O' Plenty Team - * - * This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License. - * - * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/. - ******************************************************************************/ - -package biomesoplenty.common.util.config; - -import java.lang.reflect.Type; -import java.util.Map.Entry; - -import net.minecraft.block.Block; -import net.minecraft.block.properties.IProperty; -import net.minecraft.block.state.IBlockState; -import net.minecraftforge.fml.common.registry.GameRegistry; -import biomesoplenty.common.util.block.BlockStateUtils; - -import com.google.common.collect.ImmutableSet; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; - -public class JsonBlockState implements JsonDeserializer, JsonSerializer -{ - @Override - public JsonElement serialize(IBlockState blockState, Type typeOfSrc, JsonSerializationContext context) - { - JsonObject jsonBlockState = new JsonObject(); - JsonObject jsonStateProperties = new JsonObject(); - - jsonBlockState.addProperty("block", GameRegistry.findUniqueIdentifierFor(blockState.getBlock()).toString()); - - for (Entry entry : (ImmutableSet>) blockState.getProperties().entrySet()) - { - IProperty property = entry.getKey(); - Comparable value = entry.getValue(); - - jsonStateProperties.addProperty(property.getName(), value.toString()); - } - - jsonBlockState.add("properties", jsonStateProperties); - - return jsonBlockState; - } - - @Override - public IBlockState deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException - { - JsonObject jsonBlockState = json.getAsJsonObject(); - - if (jsonBlockState.has("block")) - { - Block block = Block.getBlockFromName(jsonBlockState.get("block").getAsString()); - - if (block != null) - { - IBlockState blockState = block.getDefaultState(); - - if (jsonBlockState.has("properties")) - { - JsonObject jsonProperties = jsonBlockState.getAsJsonObject("properties"); - - for (Entry entry : jsonProperties.entrySet()) - { - IProperty property = BlockStateUtils.getPropertyByName(blockState, entry.getKey()); - - if (property != null) - { - Comparable propertyValue = BlockStateUtils.getPropertyValueByName(blockState, property, entry.getValue().getAsString()); - - if (propertyValue != null) - { - blockState = blockState.withProperty(property, propertyValue); - } - else - { - throw new JsonParseException("Invalid value " + entry.getValue().getAsString() + " for property " + entry.getKey()); - } - } - else - { - throw new JsonParseException("Invalid property name: " + entry.getKey()); - } - } - } - - return blockState; - } - } - - throw new JsonParseException("Invalid block state: " + json.toString()); - } -} diff --git a/src/main/java/biomesoplenty/common/util/config/JsonEntitySpawn.java b/src/main/java/biomesoplenty/common/util/config/JsonEntitySpawn.java deleted file mode 100644 index ee9d7defd..000000000 --- a/src/main/java/biomesoplenty/common/util/config/JsonEntitySpawn.java +++ /dev/null @@ -1,76 +0,0 @@ -/******************************************************************************* - * Copyright 2014, the Biomes O' Plenty Team - * - * This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License. - * - * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/. - ******************************************************************************/ - -package biomesoplenty.common.util.config; - -import java.util.ArrayList; -import java.util.List; - -import net.minecraft.entity.EnumCreatureType; -import net.minecraft.world.biome.BiomeGenBase; -import net.minecraft.world.biome.BiomeGenBase.SpawnListEntry; - -public class JsonEntitySpawn -{ - public String entityType; - public String entityClass; - public int weight; - public int minGroupCount; - public int maxGroupCount; - - public static ArrayList getBiomeEntitySpawns(BiomeGenBase biome) - { - ArrayList entitySpawns = new ArrayList(); - - for (EnumCreatureType creatureType : EnumCreatureType.values()) - { - List spawnableList = biome.getSpawnableList(creatureType); - - for (SpawnListEntry spawnListEntry : spawnableList) - { - JsonEntitySpawn entitySpawn = new JsonEntitySpawn(); - - entitySpawn.entityType = creatureType.toString().toLowerCase(); - entitySpawn.entityClass = spawnListEntry.entityClass.getCanonicalName(); - entitySpawn.weight = spawnListEntry.itemWeight; - entitySpawn.minGroupCount = spawnListEntry.minGroupCount; - entitySpawn.maxGroupCount = spawnListEntry.maxGroupCount; - - entitySpawns.add(entitySpawn); - } - } - - return entitySpawns; - } - - public static void addBiomeEntitySpawns(BiomeGenBase biome, JsonBiome jsonBiome) - { - for (EnumCreatureType creatureType : EnumCreatureType.values()) - { - biome.getSpawnableList(creatureType).clear(); - } - - for (JsonEntitySpawn entitySpawn : jsonBiome.entities) - { - if (entitySpawn != null) - { - try - { - EnumCreatureType creatureType = EnumCreatureType.valueOf(entitySpawn.entityType.toUpperCase()); - Class entityClass = Class.forName(entitySpawn.entityClass); - - biome.getSpawnableList(creatureType).add(new BiomeGenBase.SpawnListEntry(entityClass, entitySpawn.weight, entitySpawn.minGroupCount, entitySpawn.maxGroupCount)); - } - catch (ClassNotFoundException e) - { - e.printStackTrace(); - } - } - } - } -} diff --git a/src/main/java/biomesoplenty/common/world/feature/GeneratorDoubleFlora.java b/src/main/java/biomesoplenty/common/world/feature/GeneratorDoubleFlora.java index ee37be99d..554e7033e 100644 --- a/src/main/java/biomesoplenty/common/world/feature/GeneratorDoubleFlora.java +++ b/src/main/java/biomesoplenty/common/world/feature/GeneratorDoubleFlora.java @@ -10,13 +10,13 @@ package biomesoplenty.common.world.feature; import java.util.Random; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; - import biomesoplenty.api.biome.generation.GeneratorCustomizable; +import biomesoplenty.api.block.BOPBlocks; +import biomesoplenty.common.block.BlockBOPDoublePlant; import biomesoplenty.common.block.BlockDecoration; +import biomesoplenty.common.block.BlockDoubleDecoration; import biomesoplenty.common.util.biome.GeneratorUtils; +import biomesoplenty.common.util.config.ConfigHelper.WrappedJsonObject; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.util.BlockPos; @@ -29,7 +29,17 @@ public class GeneratorDoubleFlora extends GeneratorCustomizable private IBlockState topState; private int generationAttempts; - public GeneratorDoubleFlora() {} + public GeneratorDoubleFlora() + { + // default + this(1, BlockBOPDoublePlant.DoublePlantType.FLAX, 64); + } + + // convenient shortcut constructor for use with a BlockBOPDoublePlant variant + public GeneratorDoubleFlora(int amountPerChunk, BlockBOPDoublePlant.DoublePlantType type, int generationAttempts) + { + this(amountPerChunk, BOPBlocks.double_plant.getDefaultState().withProperty(BlockBOPDoublePlant.VARIANT, type).withProperty(BlockBOPDoublePlant.HALF, BlockDoubleDecoration.Half.LOWER), BOPBlocks.double_plant.getDefaultState().withProperty(BlockBOPDoublePlant.VARIANT, type).withProperty(BlockBOPDoublePlant.HALF, BlockDoubleDecoration.Half.UPPER), generationAttempts); + } public GeneratorDoubleFlora(int amountPerChunk, IBlockState bottomState, IBlockState topState, int generationAttempts) { @@ -79,22 +89,13 @@ public class GeneratorDoubleFlora extends GeneratorCustomizable return true; } - + @Override - public void writeToJson(JsonObject json, JsonSerializationContext context) + public void configure(WrappedJsonObject conf) { - json.addProperty("amount_per_chunk", this.amountPerChunk); - json.add("bottom_state", context.serialize(this.bottomState)); - json.add("top_state", context.serialize(this.topState)); - json.addProperty("generation_attempts", this.generationAttempts); - } - - @Override - public void readFromJson(JsonObject json, JsonDeserializationContext context) - { - this.amountPerChunk = json.get("amount_per_chunk").getAsInt(); - this.bottomState = GeneratorUtils.deserializeStateNonNull(json, "bottom_state", context); - this.topState = GeneratorUtils.deserializeStateNonNull(json, "top_state", context); - this.generationAttempts = json.get("generation_attempts").getAsInt(); + this.amountPerChunk = conf.getInt("amountPerChunk", this.amountPerChunk); + this.bottomState = conf.getBlockState("bottomState", this.bottomState); + this.topState = conf.getBlockState("topState", this.topState); + this.generationAttempts = conf.getInt("generationAttempts", this.generationAttempts); } } diff --git a/src/main/java/biomesoplenty/common/world/feature/GeneratorFlora.java b/src/main/java/biomesoplenty/common/world/feature/GeneratorFlora.java index 711120a2e..dcb8c5ca4 100644 --- a/src/main/java/biomesoplenty/common/world/feature/GeneratorFlora.java +++ b/src/main/java/biomesoplenty/common/world/feature/GeneratorFlora.java @@ -10,17 +10,15 @@ package biomesoplenty.common.world.feature; import java.util.Random; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; - import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; +import net.minecraft.init.Blocks; import net.minecraft.util.BlockPos; import net.minecraft.world.World; import biomesoplenty.api.biome.generation.GeneratorCustomizable; import biomesoplenty.common.block.BlockDecoration; import biomesoplenty.common.util.biome.GeneratorUtils; +import biomesoplenty.common.util.config.ConfigHelper.WrappedJsonObject; public class GeneratorFlora extends GeneratorCustomizable { @@ -28,7 +26,11 @@ public class GeneratorFlora extends GeneratorCustomizable protected IBlockState state; protected int generationAttempts; - public GeneratorFlora() {} + public GeneratorFlora() + { + // default + this(1, Blocks.red_flower.getDefaultState(), 64); + } public GeneratorFlora(int amountPerChunk, IBlockState state, int generationAttempts) { @@ -76,20 +78,13 @@ public class GeneratorFlora extends GeneratorCustomizable return true; } - + @Override - public void writeToJson(JsonObject json, JsonSerializationContext context) + public void configure(WrappedJsonObject conf) { - json.addProperty("amount_per_chunk", this.amountPerChunk); - json.add("state", context.serialize(this.state)); - json.addProperty("generation_attempts", this.generationAttempts); + this.amountPerChunk = conf.getInt("amountPerChunk", this.amountPerChunk); + this.state = conf.getBlockState("state", this.state); + this.generationAttempts = conf.getInt("generationAttempts", this.generationAttempts); } - @Override - public void readFromJson(JsonObject json, JsonDeserializationContext context) - { - this.amountPerChunk = json.get("amount_per_chunk").getAsInt(); - this.state = GeneratorUtils.deserializeStateNonNull(json, "state", context); - this.generationAttempts = json.get("generation_attempts").getAsInt(); - } } diff --git a/src/main/java/biomesoplenty/common/world/feature/GeneratorGrass.java b/src/main/java/biomesoplenty/common/world/feature/GeneratorGrass.java index 71672953f..de5f81dff 100644 --- a/src/main/java/biomesoplenty/common/world/feature/GeneratorGrass.java +++ b/src/main/java/biomesoplenty/common/world/feature/GeneratorGrass.java @@ -12,6 +12,7 @@ import java.util.Random; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; +import net.minecraft.init.Blocks; import net.minecraft.util.BlockPos; import net.minecraft.world.World; import biomesoplenty.common.block.BlockDecoration; @@ -19,7 +20,11 @@ import biomesoplenty.common.util.biome.GeneratorUtils; public class GeneratorGrass extends GeneratorFlora { - public GeneratorGrass() {} + public GeneratorGrass() + { + // default + this(1, Blocks.tallgrass.getDefaultState(), 128); + } public GeneratorGrass(int amountPerChunk, IBlockState state, int generationAttempts) { diff --git a/src/main/java/biomesoplenty/common/world/feature/GeneratorOreBase.java b/src/main/java/biomesoplenty/common/world/feature/GeneratorOreBase.java index 4c2e8bf37..1e998c37f 100644 --- a/src/main/java/biomesoplenty/common/world/feature/GeneratorOreBase.java +++ b/src/main/java/biomesoplenty/common/world/feature/GeneratorOreBase.java @@ -17,10 +17,7 @@ import org.apache.commons.lang3.tuple.Pair; import biomesoplenty.api.biome.generation.GeneratorCustomizable; import biomesoplenty.common.util.biome.GeneratorUtils; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; +import biomesoplenty.common.util.config.ConfigHelper.WrappedJsonObject; public abstract class GeneratorOreBase extends GeneratorCustomizable { @@ -28,8 +25,6 @@ public abstract class GeneratorOreBase extends GeneratorCustomizable protected int minHeight; protected int maxHeight; - protected GeneratorOreBase() {} - protected GeneratorOreBase(int amountPerChunk, int minHeight, int maxHeight) { this.amountPerChunk = amountPerChunk; @@ -51,22 +46,15 @@ public abstract class GeneratorOreBase extends GeneratorCustomizable } @Override - public void writeToJson(JsonObject json, JsonSerializationContext context) + public void configure(WrappedJsonObject conf) { - json.addProperty("amount_per_chunk", this.amountPerChunk); - json.addProperty("min_height", this.minHeight); - json.addProperty("max_height", this.maxHeight); - } - - @Override - public void readFromJson(JsonObject json, JsonDeserializationContext context) - { - this.amountPerChunk = json.get("amount_per_chunk").getAsInt(); - int minHeight = json.get("min_height").getAsInt(); - int maxHeight = json.get("max_height").getAsInt(); + this.amountPerChunk = conf.getInt("amountPerChunk", this.amountPerChunk); + int minHeight = conf.getInt("minHeight", this.minHeight).intValue(); + int maxHeight = conf.getInt("maxHeight", this.maxHeight).intValue(); Pair heights = GeneratorUtils.validateMinMaxHeight(minHeight, maxHeight); this.minHeight = heights.getLeft(); this.maxHeight = heights.getRight(); } + } diff --git a/src/main/java/biomesoplenty/common/world/feature/GeneratorOreCluster.java b/src/main/java/biomesoplenty/common/world/feature/GeneratorOreCluster.java index 0adbb2640..1b32e18db 100644 --- a/src/main/java/biomesoplenty/common/world/feature/GeneratorOreCluster.java +++ b/src/main/java/biomesoplenty/common/world/feature/GeneratorOreCluster.java @@ -11,19 +11,21 @@ package biomesoplenty.common.world.feature; import java.util.Random; import net.minecraft.block.state.IBlockState; +import net.minecraft.init.Blocks; import net.minecraft.util.BlockPos; import net.minecraft.world.World; import net.minecraft.world.gen.feature.WorldGenMinable; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; +import biomesoplenty.common.util.config.ConfigHelper.WrappedJsonObject; public class GeneratorOreCluster extends GeneratorOreBase { private WorldGenMinable generator; - public GeneratorOreCluster() {} + public GeneratorOreCluster() + { + // default + this(Blocks.emerald_ore.getDefaultState(), 12, 4, 4, 32); + } public GeneratorOreCluster(IBlockState state, int amountPerChunk, int clusterSize, int minHeight, int maxHeight) { @@ -37,24 +39,14 @@ public class GeneratorOreCluster extends GeneratorOreBase { return this.generator.generate(world, random, pos); } - + @Override - public void writeToJson(JsonObject json, JsonSerializationContext context) + public void configure(WrappedJsonObject conf) { - super.writeToJson(json, context); + super.configure(conf); - json.add("state", context.serialize(this.generator.oreBlock)); - json.addProperty("cluster_size", this.generator.numberOfBlocks); - } - - @Override - public void readFromJson(JsonObject json, JsonDeserializationContext context) - { - super.readFromJson(json, context); - - IBlockState state = context.deserialize(json.get("state"), IBlockState.class); - int clusterSize = json.get("cluster_size").getAsInt(); - - this.generator = new WorldGenMinable(state, clusterSize); + this.generator.oreBlock = conf.getBlockState("state", this.generator.oreBlock); + this.generator.numberOfBlocks = conf.getInt("clusterSize", this.generator.numberOfBlocks); } + } diff --git a/src/main/java/biomesoplenty/common/world/feature/GeneratorOreSingle.java b/src/main/java/biomesoplenty/common/world/feature/GeneratorOreSingle.java index 679022013..b2d4d9b90 100644 --- a/src/main/java/biomesoplenty/common/world/feature/GeneratorOreSingle.java +++ b/src/main/java/biomesoplenty/common/world/feature/GeneratorOreSingle.java @@ -15,18 +15,20 @@ import net.minecraft.block.state.pattern.BlockHelper; import net.minecraft.init.Blocks; import net.minecraft.util.BlockPos; import net.minecraft.world.World; +import biomesoplenty.common.util.config.ConfigHelper.WrappedJsonObject; import com.google.common.base.Predicate; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; public class GeneratorOreSingle extends GeneratorOreBase { private IBlockState state; private Predicate replace; - public GeneratorOreSingle() {} + public GeneratorOreSingle() + { + // default + this(Blocks.emerald_ore.getDefaultState(), 12, 4, 32); + } public GeneratorOreSingle(IBlockState state, int amountPerChunk, int minHeight, int maxHeight) { @@ -46,21 +48,14 @@ public class GeneratorOreSingle extends GeneratorOreBase return false; } - + @Override - public void writeToJson(JsonObject json, JsonSerializationContext context) + public void configure(WrappedJsonObject conf) { - super.writeToJson(json, context); + super.configure(conf); - json.add("state", context.serialize(this.state)); - } - - @Override - public void readFromJson(JsonObject json, JsonDeserializationContext context) - { - super.readFromJson(json, context); - - this.state = context.deserialize(json.get("state"), IBlockState.class); + this.state = conf.getBlockState("state", this.state); this.replace = BlockHelper.forBlock(Blocks.stone); } + } diff --git a/src/main/java/biomesoplenty/common/world/feature/GeneratorWaterside.java b/src/main/java/biomesoplenty/common/world/feature/GeneratorWaterside.java index 680d48e6f..ecb687a61 100644 --- a/src/main/java/biomesoplenty/common/world/feature/GeneratorWaterside.java +++ b/src/main/java/biomesoplenty/common/world/feature/GeneratorWaterside.java @@ -8,6 +8,7 @@ package biomesoplenty.common.world.feature; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; @@ -18,11 +19,7 @@ import net.minecraft.init.Blocks; import net.minecraft.util.BlockPos; import net.minecraft.world.World; import biomesoplenty.api.biome.generation.GeneratorCustomizable; -import biomesoplenty.common.util.biome.GeneratorUtils; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; +import biomesoplenty.common.util.config.ConfigHelper.WrappedJsonObject; public class GeneratorWaterside extends GeneratorCustomizable { @@ -31,7 +28,11 @@ public class GeneratorWaterside extends GeneratorCustomizable private IBlockState state; private List replacedStates; - public GeneratorWaterside() {} + public GeneratorWaterside() + { + // default + this(4, 7, Blocks.gravel.getDefaultState()); + } public GeneratorWaterside(int amountPerChunk, int maxRadius, IBlockState state, IBlockState... replacedStates) { @@ -103,22 +104,14 @@ public class GeneratorWaterside extends GeneratorCustomizable return true; } } - + @Override - public void writeToJson(JsonObject json, JsonSerializationContext context) + public void configure(WrappedJsonObject conf) { - json.addProperty("amount_per_chunk", this.amountPerChunk); - json.addProperty("max_radius", this.maxRadius); - json.add("state", context.serialize(this.state)); - json.add("replaced_states", context.serialize(this.replacedStates)); + this.amountPerChunk = conf.getInt("amountPerChunk", this.amountPerChunk); + this.maxRadius = conf.getInt("maxRadius", this.maxRadius); + this.state = conf.getBlockState("state", this.state); + this.replacedStates = conf.getBlockStateArray("replacedStates", new ArrayList(this.replacedStates) ); } - @Override - public void readFromJson(JsonObject json, JsonDeserializationContext context) - { - this.amountPerChunk = json.get("amount_per_chunk").getAsInt(); - this.maxRadius = json.get("max_radius").getAsInt(); - this.state = GeneratorUtils.deserializeStateNonNull(json, "state", context); - this.replacedStates = context.deserialize(json.get("replaced_states"), List.class); - } } diff --git a/src/main/java/biomesoplenty/common/world/feature/tree/GeneratorBasicTree.java b/src/main/java/biomesoplenty/common/world/feature/tree/GeneratorBasicTree.java index 8050db9f0..2e4a4a75f 100644 --- a/src/main/java/biomesoplenty/common/world/feature/tree/GeneratorBasicTree.java +++ b/src/main/java/biomesoplenty/common/world/feature/tree/GeneratorBasicTree.java @@ -24,10 +24,7 @@ import org.apache.commons.lang3.tuple.Pair; import biomesoplenty.api.biome.generation.GeneratorCustomizable; import biomesoplenty.common.util.biome.GeneratorUtils; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; +import biomesoplenty.common.util.config.ConfigHelper.WrappedJsonObject; public class GeneratorBasicTree extends GeneratorCustomizable { @@ -39,7 +36,11 @@ public class GeneratorBasicTree extends GeneratorCustomizable private IBlockState leaves; private IBlockState vine; - public GeneratorBasicTree() {} + public GeneratorBasicTree() + { + // default + this(1, false, 4, 7, Blocks.log.getDefaultState(), Blocks.leaves.getDefaultState()); + } public GeneratorBasicTree(int amountPerChunk, boolean updateNeighbours, int minHeight, int maxHeight, IBlockState log, IBlockState leaves, IBlockState vine) { @@ -299,33 +300,21 @@ public class GeneratorBasicTree extends GeneratorCustomizable world.setBlockState(pos, state, 2); } } - + @Override - public void writeToJson(JsonObject json, JsonSerializationContext context) + public void configure(WrappedJsonObject conf) { - json.addProperty("amount_per_chunk", this.amountPerChunk); - json.addProperty("update_neighbours", this.updateNeighbours); - json.addProperty("min_height", this.minHeight); - json.addProperty("max_height", this.maxHeight); - json.add("log_state", context.serialize(this.log)); - json.add("leaves_state", context.serialize(this.leaves)); - json.add("vine_state", context.serialize(this.vine)); - } - - @Override - public void readFromJson(JsonObject json, JsonDeserializationContext context) - { - this.amountPerChunk = json.get("amount_per_chunk").getAsInt(); - this.updateNeighbours = json.get("update_neighbours").getAsBoolean(); - int minHeight = json.get("min_height").getAsInt(); - int maxHeight = json.get("max_height").getAsInt(); + this.amountPerChunk = conf.getInt("amountPerChunk", this.amountPerChunk); + this.updateNeighbours = conf.getBool("updateNeighbours", this.updateNeighbours); + int minHeight = conf.getInt("minHeight", this.minHeight); + int maxHeight = conf.getInt("maxHeight", this.maxHeight); Pair heights = GeneratorUtils.validateMinMaxHeight(minHeight, maxHeight); this.minHeight = heights.getLeft(); this.maxHeight = heights.getRight(); - this.log = context.deserialize(json.get("log_state"), IBlockState.class); - this.leaves = context.deserialize(json.get("leaves_state"), IBlockState.class); - this.vine = context.deserialize(json.get("vine_state"), IBlockState.class); + this.log = conf.getBlockState("logState", this.log); + this.leaves = conf.getBlockState("leavesState", this.leaves); + this.vine = conf.getBlockState("vineState", this.vine); } } diff --git a/src/main/java/biomesoplenty/common/world/feature/tree/GeneratorBigTree.java b/src/main/java/biomesoplenty/common/world/feature/tree/GeneratorBigTree.java index dc871d0c0..81484fe55 100644 --- a/src/main/java/biomesoplenty/common/world/feature/tree/GeneratorBigTree.java +++ b/src/main/java/biomesoplenty/common/world/feature/tree/GeneratorBigTree.java @@ -23,11 +23,9 @@ import net.minecraft.util.MathHelper; import net.minecraft.world.World; import biomesoplenty.api.biome.generation.GeneratorCustomizable; import biomesoplenty.common.util.biome.GeneratorUtils; +import biomesoplenty.common.util.config.ConfigHelper.WrappedJsonObject; import com.google.common.collect.Lists; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; /*This class is heavily based on https://gist.github.com/grum/62cfdec0537e8db24eb3#file-bigtreefeature-java additional information has been added from http://pastebin.com/XBLdGqXQ. This class has been cross-checked @@ -58,7 +56,11 @@ public class GeneratorBigTree extends GeneratorCustomizable private List foliageCoords; - public GeneratorBigTree() {} + public GeneratorBigTree() + { + // default + this(1, false, 4, 7, Blocks.log.getDefaultState(), Blocks.leaves.getDefaultState()); + } public GeneratorBigTree(int amountPerChunk, boolean updateNeighbours, int minHeight, int maxHeight, IBlockState log, IBlockState leaves) { @@ -531,31 +533,21 @@ public class GeneratorBigTree extends GeneratorCustomizable return branchBase; } } - + @Override - public void writeToJson(JsonObject json, JsonSerializationContext context) + public void configure(WrappedJsonObject conf) { - json.addProperty("amount_per_chunk", this.amountPerChunk); - json.addProperty("update_neighbours", this.updateNeighbours); - json.addProperty("min_height", this.minHeight); - json.addProperty("max_height", this.maxHeight); - json.add("log_state", context.serialize(this.log)); - json.add("leaves_state", context.serialize(this.leaves)); - } - - @Override - public void readFromJson(JsonObject json, JsonDeserializationContext context) - { - this.amountPerChunk = json.get("amount_per_chunk").getAsInt(); - this.updateNeighbours = json.get("update_neighbours").getAsBoolean(); - int minHeight = json.get("min_height").getAsInt(); - int maxHeight = json.get("max_height").getAsInt(); + this.amountPerChunk = conf.getInt("amountPerChunk", this.amountPerChunk); + this.updateNeighbours = conf.getBool("updateNeighbours", this.updateNeighbours); + int minHeight = conf.getInt("minHeight", this.minHeight); + int maxHeight = conf.getInt("maxHeight", this.maxHeight); Pair heights = GeneratorUtils.validateMinMaxHeight(minHeight, maxHeight); this.minHeight = heights.getLeft(); this.maxHeight = heights.getRight(); - this.log = context.deserialize(json.get("log_state"), IBlockState.class); - this.leaves = context.deserialize(json.get("leaves_state"), IBlockState.class); + this.log = conf.getBlockState("logState", this.log); + this.leaves = conf.getBlockState("leavesState", this.leaves); } + } diff --git a/src/main/java/biomesoplenty/common/world/feature/tree/GeneratorBush.java b/src/main/java/biomesoplenty/common/world/feature/tree/GeneratorBush.java index 73a4e4d63..163044abc 100644 --- a/src/main/java/biomesoplenty/common/world/feature/tree/GeneratorBush.java +++ b/src/main/java/biomesoplenty/common/world/feature/tree/GeneratorBush.java @@ -18,12 +18,8 @@ import net.minecraft.util.BlockPos; import net.minecraft.util.EnumFacing; import net.minecraft.world.World; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; - import biomesoplenty.api.biome.generation.GeneratorCustomizable; -import biomesoplenty.common.util.biome.GeneratorUtils; +import biomesoplenty.common.util.config.ConfigHelper.WrappedJsonObject; public class GeneratorBush extends GeneratorCustomizable { @@ -31,7 +27,11 @@ public class GeneratorBush extends GeneratorCustomizable private IBlockState log; private IBlockState leaves; - public GeneratorBush() {} + public GeneratorBush() + { + // default + this(1, Blocks.log.getDefaultState(), Blocks.leaves.getDefaultState()); + } public GeneratorBush(int amountPerChunk, IBlockState log, IBlockState leaves) { @@ -108,20 +108,13 @@ public class GeneratorBush extends GeneratorCustomizable return true; } - + @Override - public void writeToJson(JsonObject json, JsonSerializationContext context) + public void configure(WrappedJsonObject conf) { - json.addProperty("amount_per_chunk", this.amountPerChunk); - json.add("log_state", context.serialize(this.log)); - json.add("leaves_state", context.serialize(this.leaves)); - } - - @Override - public void readFromJson(JsonObject json, JsonDeserializationContext context) - { - this.amountPerChunk = json.get("amount_per_chunk").getAsInt(); - this.log = GeneratorUtils.deserializeStateNonNull(json, "log_state", context); - this.leaves = GeneratorUtils.deserializeStateNonNull(json, "leaves_state", context); + this.amountPerChunk = conf.getInt("amountPerChunk", this.amountPerChunk); + this.log = conf.getBlockState("logState", this.log); + this.leaves = conf.getBlockState("leavesState", this.leaves); } + }