From 0020ea25f35043229e5da4e8afe3216b3491324d Mon Sep 17 00:00:00 2001 From: Adubbz Date: Thu, 9 Apr 2015 12:29:51 +1000 Subject: [PATCH] Added flowers to the Origin Valley --- .../CustomizableWeightedGenerator.java | 20 ++++- .../biome/overworld/BiomeGenOriginValley.java | 9 ++ .../common/init/ModGenerators.java | 2 + .../common/util/biome/GeneratorUtils.java | 25 ++++++ .../feature/tree/GeneratorBasicTree.java | 6 +- .../feature/weighted/GeneratorFlora.java | 85 +++++++++++++++++++ 6 files changed, 141 insertions(+), 6 deletions(-) create mode 100644 src/main/java/biomesoplenty/common/world/feature/weighted/GeneratorFlora.java diff --git a/src/main/java/biomesoplenty/api/biome/generation/CustomizableWeightedGenerator.java b/src/main/java/biomesoplenty/api/biome/generation/CustomizableWeightedGenerator.java index 11aa58631..8801e36ac 100644 --- a/src/main/java/biomesoplenty/api/biome/generation/CustomizableWeightedGenerator.java +++ b/src/main/java/biomesoplenty/api/biome/generation/CustomizableWeightedGenerator.java @@ -8,11 +8,11 @@ package biomesoplenty.api.biome.generation; -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; public abstract class CustomizableWeightedGenerator extends WeightedRandom.Item implements IGeneratorDelegate { @@ -36,6 +36,18 @@ public abstract class CustomizableWeightedGenerator extends WeightedRandom.Item throw new RuntimeException("The identifier for " + this.getClass().getCanonicalName() + " cannot be null!"); } } + + @Override + public void writeToJson(JsonObject json, JsonSerializationContext context) + { + json.addProperty("weight", this.itemWeight); + } + + @Override + public void readFromJson(JsonObject json, JsonDeserializationContext context) + { + this.itemWeight = json.get("weight").getAsInt(); + } @Override public void setStage(GeneratorStage stage) diff --git a/src/main/java/biomesoplenty/common/biome/overworld/BiomeGenOriginValley.java b/src/main/java/biomesoplenty/common/biome/overworld/BiomeGenOriginValley.java index 65edb2423..ac5340c5b 100644 --- a/src/main/java/biomesoplenty/common/biome/overworld/BiomeGenOriginValley.java +++ b/src/main/java/biomesoplenty/common/biome/overworld/BiomeGenOriginValley.java @@ -15,6 +15,8 @@ import biomesoplenty.api.biome.BOPBiome; import biomesoplenty.api.biome.generation.GeneratorStage; import biomesoplenty.api.block.BOPBlocks; import biomesoplenty.api.block.BOPTreeEnums.AllTrees; +import biomesoplenty.common.block.BlockBOPFlower2; +import biomesoplenty.common.block.BlockBOPFlower2.FlowerType; import biomesoplenty.common.block.BlockBOPGrass; import biomesoplenty.common.block.BlockBOPLeaves; import biomesoplenty.common.block.BlockBOPLeaves2; @@ -22,6 +24,7 @@ import biomesoplenty.common.block.BlockBOPGrass.BOPGrassType; import biomesoplenty.common.config.MiscConfigurationHandler; import biomesoplenty.common.world.feature.GeneratorWeighted; import biomesoplenty.common.world.feature.tree.GeneratorBasicTree; +import biomesoplenty.common.world.feature.weighted.GeneratorFlora; public class BiomeGenOriginValley extends BOPBiome { @@ -42,6 +45,12 @@ public class BiomeGenOriginValley extends BOPBiome BOPBlocks.leaves_2.getDefaultState().withProperty(BlockBOPLeaves.getVariantProperty(BlockBOPLeaves2.PAGENUM), AllTrees.ORIGIN))); this.addGenerator("trees", GeneratorStage.TREE, treeGenerator); + + GeneratorWeighted flowerGenerator = new GeneratorWeighted(4); + flowerGenerator.add(new GeneratorFlora(8, BOPBlocks.flower2.getDefaultState().withProperty(BlockBOPFlower2.VARIANT, FlowerType.ROSE))); + flowerGenerator.add(new GeneratorFlora(10, Blocks.yellow_flower.getDefaultState())); + + this.addGenerator("flowers", GeneratorStage.FLOWERS, flowerGenerator); } @Override diff --git a/src/main/java/biomesoplenty/common/init/ModGenerators.java b/src/main/java/biomesoplenty/common/init/ModGenerators.java index fa2fb607f..fac204257 100644 --- a/src/main/java/biomesoplenty/common/init/ModGenerators.java +++ b/src/main/java/biomesoplenty/common/init/ModGenerators.java @@ -13,6 +13,7 @@ import biomesoplenty.common.world.feature.GeneratorOreCluster; import biomesoplenty.common.world.feature.GeneratorOreSingle; import biomesoplenty.common.world.feature.GeneratorWeighted; import biomesoplenty.common.world.feature.tree.GeneratorBasicTree; +import biomesoplenty.common.world.feature.weighted.GeneratorFlora; public class ModGenerators { @@ -22,5 +23,6 @@ public class ModGenerators registerGenerator("ore_cluster", GeneratorOreCluster.class); registerGenerator("weighted", GeneratorWeighted.class); registerGenerator("basic_tree", GeneratorBasicTree.class); + registerGenerator("flora", GeneratorFlora.class); } } diff --git a/src/main/java/biomesoplenty/common/util/biome/GeneratorUtils.java b/src/main/java/biomesoplenty/common/util/biome/GeneratorUtils.java index c7295a811..3a4bafddd 100644 --- a/src/main/java/biomesoplenty/common/util/biome/GeneratorUtils.java +++ b/src/main/java/biomesoplenty/common/util/biome/GeneratorUtils.java @@ -8,8 +8,14 @@ package biomesoplenty.common.util.biome; +import java.util.Random; + import org.apache.commons.lang3.tuple.Pair; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonObject; +import com.google.gson.JsonSyntaxException; + import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; @@ -44,6 +50,25 @@ public class GeneratorUtils return Pair.of(minHeight, maxHeight); } + + public static int safeNextInt(Random random, int i) + { + if (i <= 1) return 0; + + return random.nextInt(i); + } + + public static IBlockState deserializeStateNonNull(JsonObject json, String memberName, JsonDeserializationContext context) + { + IBlockState state = context.deserialize(json.get(memberName), IBlockState.class); + + if (state == null) + { + throw new JsonSyntaxException("Property " + memberName + " doesn't exist"); + } + + return state; + } public static boolean isBlockTreeReplacable(Block block) { 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 3487c5474..842aa2755 100644 --- a/src/main/java/biomesoplenty/common/world/feature/tree/GeneratorBasicTree.java +++ b/src/main/java/biomesoplenty/common/world/feature/tree/GeneratorBasicTree.java @@ -303,7 +303,8 @@ public class GeneratorBasicTree extends CustomizableWeightedGenerator @Override public void writeToJson(JsonObject json, JsonSerializationContext context) { - json.addProperty("weight", this.itemWeight); + super.writeToJson(json, context); + json.addProperty("update_neighbours", this.updateNeighbours); json.addProperty("min_height", this.minHeight); json.addProperty("max_height", this.maxHeight); @@ -315,7 +316,8 @@ public class GeneratorBasicTree extends CustomizableWeightedGenerator @Override public void readFromJson(JsonObject json, JsonDeserializationContext context) { - this.itemWeight = json.get("weight").getAsInt(); + super.readFromJson(json, context); + this.updateNeighbours = json.get("update_neighbours").getAsBoolean(); Pair heights = GeneratorUtils.validateMinMaxHeight(minHeight, maxHeight); diff --git a/src/main/java/biomesoplenty/common/world/feature/weighted/GeneratorFlora.java b/src/main/java/biomesoplenty/common/world/feature/weighted/GeneratorFlora.java new file mode 100644 index 000000000..25983600c --- /dev/null +++ b/src/main/java/biomesoplenty/common/world/feature/weighted/GeneratorFlora.java @@ -0,0 +1,85 @@ +/******************************************************************************* + * Copyright 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.world.feature.weighted; + +import java.util.Random; + +import com.google.common.base.Predicates; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonObject; +import com.google.gson.JsonSerializationContext; + +import net.minecraft.block.state.IBlockState; +import net.minecraft.util.BlockPos; +import net.minecraft.world.World; +import biomesoplenty.api.biome.generation.CustomizableWeightedGenerator; +import biomesoplenty.common.block.BlockDecoration; +import biomesoplenty.common.util.biome.GeneratorUtils; + +public class GeneratorFlora extends CustomizableWeightedGenerator +{ + public IBlockState state; + + public GeneratorFlora() {} + + public GeneratorFlora(int weight, IBlockState state) + { + super(weight); + + this.state = state; + } + + @Override + public void scatter(World world, Random random, BlockPos pos, int amountPerChunk) + { + for (int i = 0; i < amountPerChunk; i++) + { + int x = random.nextInt(16) + 8; + int z = random.nextInt(16) + 8; + BlockPos genPos = pos.add(x, 0, z); + int y = GeneratorUtils.safeNextInt(random, world.getHeight(genPos).getY() + 32); + genPos = genPos.add(0, y, 0); + + generate(world, random, genPos, amountPerChunk); + } + } + + @Override + public boolean generate(World world, Random random, BlockPos pos, int amountPerChunk) + { + for (int i = 0; i < 64; ++i) + { + BlockPos blockpos1 = pos.add(random.nextInt(8) - random.nextInt(8), random.nextInt(4) - random.nextInt(4), random.nextInt(8) - random.nextInt(8)); + + if (world.isAirBlock(blockpos1) && (!world.provider.getHasNoSky() || blockpos1.getY() < 255) && + (this.state.getBlock() instanceof BlockDecoration ? ((BlockDecoration)this.state.getBlock()).canBlockStay(world, blockpos1, this.state) : this.state.getBlock().canPlaceBlockAt(world, blockpos1))) + { + world.setBlockState(blockpos1, this.state, 2); + } + } + + return true; + } + + @Override + public void writeToJson(JsonObject json, JsonSerializationContext context) + { + super.writeToJson(json, context); + + json.add("state", context.serialize(this.state)); + } + + @Override + public void readFromJson(JsonObject json, JsonDeserializationContext context) + { + super.readFromJson(json, context); + + this.state = GeneratorUtils.deserializeStateNonNull(json, "state", context); + } +}