diff --git a/src/main/java/biomesoplenty/api/biome/IExtendedDecorator.java b/src/main/java/biomesoplenty/api/biome/IExtendedDecorator.java new file mode 100644 index 000000000..3e186579e --- /dev/null +++ b/src/main/java/biomesoplenty/api/biome/IExtendedDecorator.java @@ -0,0 +1,18 @@ +/******************************************************************************* + * 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.api.biome; + +import java.util.Map; + +public interface IExtendedDecorator +{ + public void addGenerator(String key, IGenerator generator); + public void configureGenerators(Map> generatorMap); + public Map> getGeneratorMap(); +} diff --git a/src/main/java/biomesoplenty/api/biome/IGenerator.java b/src/main/java/biomesoplenty/api/biome/IGenerator.java new file mode 100644 index 000000000..d4236aa90 --- /dev/null +++ b/src/main/java/biomesoplenty/api/biome/IGenerator.java @@ -0,0 +1,17 @@ +/******************************************************************************* + * 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.api.biome; + +import com.google.gson.JsonElement; + +public interface IGenerator +{ + public JsonElement serialize(IGenerator src); + public IGenerator deserialize(JsonElement json); +} diff --git a/src/main/java/biomesoplenty/common/config/BiomeConfigurationHandler.java b/src/main/java/biomesoplenty/common/config/BiomeConfigurationHandler.java index cec9130bd..155d3a9b6 100644 --- a/src/main/java/biomesoplenty/common/config/BiomeConfigurationHandler.java +++ b/src/main/java/biomesoplenty/common/config/BiomeConfigurationHandler.java @@ -14,11 +14,15 @@ import java.util.HashMap; import java.util.Map.Entry; import net.minecraft.world.biome.BiomeGenBase; +import net.minecraft.world.gen.feature.WorldGenCactus; import org.apache.commons.io.FileUtils; import biomesoplenty.api.biome.BiomeOwner; import biomesoplenty.api.biome.IExtendedBiome; +import biomesoplenty.api.biome.IExtendedDecorator; +import biomesoplenty.api.biome.IGenerator; +import biomesoplenty.common.decoration.extensions.IExtendedCactusGen; import biomesoplenty.common.util.config.JsonBiome; import biomesoplenty.common.util.config.JsonEntitySpawn; @@ -126,9 +130,18 @@ public class BiomeConfigurationHandler private static void translateVanillaValues(BiomeGenBase biome) { IExtendedBiome extendedBiome = (IExtendedBiome)biome; + IExtendedDecorator extendedDecorator = (IExtendedDecorator)biome.theBiomeDecorator; if (extendedBiome.getBiomeOwner() == BiomeOwner.OTHER) { + if (biome.theBiomeDecorator.cactiPerChunk > 0) + { + WorldGenCactus cactusGen = new WorldGenCactus(); + IExtendedCactusGen extendedCactusGen = (IExtendedCactusGen)cactusGen; + + extendedCactusGen.setCactiPerChunk(biome.theBiomeDecorator.cactiPerChunk); + extendedDecorator.addGenerator("cactus", extendedCactusGen); + } } } @@ -146,6 +159,8 @@ public class BiomeConfigurationHandler biome.waterColorMultiplier = jsonBiome.waterColorMultiplier; JsonEntitySpawn.addBiomeEntitySpawns(biome, jsonBiome); - IExtendedBiome extendedBiome = (IExtendedBiome)biome; + IExtendedDecorator extendedDecorator = (IExtendedDecorator)biome.theBiomeDecorator; + + extendedDecorator.configureGenerators(jsonBiome.decoration); } } diff --git a/src/main/java/biomesoplenty/common/decoration/extensions/IExtendedCactusGen.java b/src/main/java/biomesoplenty/common/decoration/extensions/IExtendedCactusGen.java new file mode 100644 index 000000000..30af84747 --- /dev/null +++ b/src/main/java/biomesoplenty/common/decoration/extensions/IExtendedCactusGen.java @@ -0,0 +1,18 @@ +/******************************************************************************* + * 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.decoration.extensions; + +import net.minecraft.world.gen.feature.WorldGenCactus; +import biomesoplenty.api.biome.IGenerator; + +public interface IExtendedCactusGen extends IGenerator +{ + public void setCactiPerChunk(int amount); + public int getCactiPerChunk(); +} diff --git a/src/main/java/biomesoplenty/common/mixin/biome/MixinBiomeDecorator.java b/src/main/java/biomesoplenty/common/mixin/biome/MixinBiomeDecorator.java new file mode 100644 index 000000000..849d0e6bd --- /dev/null +++ b/src/main/java/biomesoplenty/common/mixin/biome/MixinBiomeDecorator.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * 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.mixin.biome; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import net.minecraft.world.biome.BiomeDecorator; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import biomesoplenty.api.biome.IExtendedDecorator; +import biomesoplenty.api.biome.IGenerator; + +@Mixin(BiomeDecorator.class) +public class MixinBiomeDecorator implements IExtendedDecorator +{ + private Map> generatorMap; + + @Inject(method = "", at = @At("RETURN")) + private void onConstructed(CallbackInfo callbackInfo) + { + this.generatorMap = new HashMap(); + } + + @Override + public void addGenerator(String key, IGenerator generator) + { + this.generatorMap.put(key, generator); + } + + @Override + public void configureGenerators(Map> generatorMap) + { + this.generatorMap.putAll(generatorMap); + } + + @Override + public Map> getGeneratorMap() + { + return Collections.unmodifiableMap(this.generatorMap); + } +} + + diff --git a/src/main/java/biomesoplenty/common/mixin/decoration/MixinWorldGenCactus.java b/src/main/java/biomesoplenty/common/mixin/decoration/MixinWorldGenCactus.java new file mode 100644 index 000000000..31c8f6b0e --- /dev/null +++ b/src/main/java/biomesoplenty/common/mixin/decoration/MixinWorldGenCactus.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * 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.mixin.decoration; + +import net.minecraft.world.gen.feature.WorldGenCactus; +import net.minecraft.world.gen.feature.WorldGenerator; + +import org.spongepowered.asm.mixin.Mixin; + +import biomesoplenty.api.biome.IGenerator; +import biomesoplenty.common.decoration.extensions.IExtendedCactusGen; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +@Mixin(WorldGenCactus.class) +public abstract class MixinWorldGenCactus extends WorldGenerator implements IExtendedCactusGen +{ + private int cactiPerChunk; + + @Override + public void setCactiPerChunk(int amount) + { + this.cactiPerChunk = amount; + } + + @Override + public int getCactiPerChunk() + { + return this.cactiPerChunk; + } + + @Override + public JsonElement serialize(IGenerator src) + { + JsonObject jsonCactusGen = new JsonObject(); + + jsonCactusGen.addProperty("cacti_per_chunk", ((IExtendedCactusGen)src).getCactiPerChunk()); + + return jsonCactusGen; + } + + @Override + public IGenerator deserialize(JsonElement json) + { + JsonObject jsonCactusGen = json.getAsJsonObject(); + WorldGenCactus cactusGen = new WorldGenCactus(); + IExtendedCactusGen extendedCactusGen = (IExtendedCactusGen)cactusGen; + + extendedCactusGen.setCactiPerChunk(jsonCactusGen.get("cacti_per_chunk").getAsInt()); + + return (IGenerator)new WorldGenCactus(); + } +} diff --git a/src/main/java/biomesoplenty/common/util/config/GeneratorTypeAdaptor.java b/src/main/java/biomesoplenty/common/util/config/GeneratorTypeAdaptor.java new file mode 100644 index 000000000..1a4937175 --- /dev/null +++ b/src/main/java/biomesoplenty/common/util/config/GeneratorTypeAdaptor.java @@ -0,0 +1,61 @@ +/******************************************************************************* + * 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.IGenerator; + +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 GeneratorTypeAdaptor implements JsonSerializer>, JsonDeserializer> +{ + @Override + public JsonElement serialize(IGenerator src, Type typeOfSrc, JsonSerializationContext context) + { + JsonObject jsonObject = src.serialize((IGenerator)src).getAsJsonObject(); + + jsonObject.addProperty("class", src.getClass().getCanonicalName()); + + return jsonObject; + } + + @Override + public IGenerator deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException + { + JsonObject jsonObject = json.getAsJsonObject(); + + if (jsonObject.has("class")) + { + try + { + Class generatorClass = Class.forName(jsonObject.get("class").getAsString()); + + if (IGenerator.class.isAssignableFrom(generatorClass)) + { + IGenerator generator = (IGenerator)generatorClass.newInstance(); + + return generator.deserialize(json); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + return null; + } +} diff --git a/src/main/java/biomesoplenty/common/util/config/JsonBiome.java b/src/main/java/biomesoplenty/common/util/config/JsonBiome.java index f3fbb777e..190500f3d 100644 --- a/src/main/java/biomesoplenty/common/util/config/JsonBiome.java +++ b/src/main/java/biomesoplenty/common/util/config/JsonBiome.java @@ -9,9 +9,12 @@ package biomesoplenty.common.util.config; import java.util.ArrayList; +import java.util.Map; import net.minecraft.block.state.IBlockState; import net.minecraft.world.biome.BiomeGenBase; +import biomesoplenty.api.biome.IExtendedDecorator; +import biomesoplenty.api.biome.IGenerator; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; @@ -21,7 +24,9 @@ public class JsonBiome { public static final Gson serializer = new GsonBuilder() .setPrettyPrinting().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) - .registerTypeAdapter(IBlockState.class, new JsonBlockState()).create(); + .registerTypeAdapter(IBlockState.class, new JsonBlockState()) + .registerTypeAdapter(IGenerator.class, new GeneratorTypeAdaptor()) + .create(); public String biomeName; public int biomeId; @@ -34,8 +39,9 @@ public class JsonBiome public int color; public int waterColorMultiplier; public ArrayList entities; + public Map> decoration; - public static JsonBiome createFromBiomeGenBase(BiomeGenBase baseBiome) + public static JsonBiome createFromBiomeGenBase(BiomeGenBase baseBiome) { JsonBiome biome = new JsonBiome(); @@ -51,6 +57,10 @@ public class JsonBiome biome.waterColorMultiplier = baseBiome.waterColorMultiplier; biome.entities = JsonEntitySpawn.getBiomeEntitySpawns(baseBiome); + IExtendedDecorator extendedDecorator = (IExtendedDecorator)baseBiome.theBiomeDecorator; + + biome.decoration = extendedDecorator.getGeneratorMap(); + return biome; } } diff --git a/src/main/resources/mixins.biomesoplenty.json b/src/main/resources/mixins.biomesoplenty.json index 8d12b96bb..98c6919e0 100644 --- a/src/main/resources/mixins.biomesoplenty.json +++ b/src/main/resources/mixins.biomesoplenty.json @@ -1,7 +1,9 @@ { "package": "biomesoplenty.common.mixin", "mixins": [ + "biome.MixinBiomeDecorator", "biome.MixinBiomeGenBase", + "decoration.MixinWorldGenCactus", "renderer.MixinBlockModelShapes" ] }