From b617d62ca4ed205f4b866e0baee0daf17b69e3c7 Mon Sep 17 00:00:00 2001 From: Adubbz Date: Thu, 9 Apr 2015 17:56:47 +1000 Subject: [PATCH] Added a generator for grass --- .../common/init/ModGenerators.java | 2 + .../feature/weighted/GeneratorFlora.java | 12 ++-- .../feature/weighted/GeneratorGrass.java | 62 +++++++++++++++++++ 3 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 src/main/java/biomesoplenty/common/world/feature/weighted/GeneratorGrass.java diff --git a/src/main/java/biomesoplenty/common/init/ModGenerators.java b/src/main/java/biomesoplenty/common/init/ModGenerators.java index fac204257..93471c0ed 100644 --- a/src/main/java/biomesoplenty/common/init/ModGenerators.java +++ b/src/main/java/biomesoplenty/common/init/ModGenerators.java @@ -14,6 +14,7 @@ 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; +import biomesoplenty.common.world.feature.weighted.GeneratorGrass; public class ModGenerators { @@ -24,5 +25,6 @@ public class ModGenerators registerGenerator("weighted", GeneratorWeighted.class); registerGenerator("basic_tree", GeneratorBasicTree.class); registerGenerator("flora", GeneratorFlora.class); + registerGenerator("grass", GeneratorGrass.class); } } diff --git a/src/main/java/biomesoplenty/common/world/feature/weighted/GeneratorFlora.java b/src/main/java/biomesoplenty/common/world/feature/weighted/GeneratorFlora.java index 25983600c..80b32e6c1 100644 --- a/src/main/java/biomesoplenty/common/world/feature/weighted/GeneratorFlora.java +++ b/src/main/java/biomesoplenty/common/world/feature/weighted/GeneratorFlora.java @@ -15,6 +15,7 @@ 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.util.BlockPos; import net.minecraft.world.World; @@ -53,14 +54,17 @@ public class GeneratorFlora extends CustomizableWeightedGenerator @Override public boolean generate(World world, Random random, BlockPos pos, int amountPerChunk) { + Block block = this.state.getBlock(); + 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)); + BlockPos genPos = 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))) + boolean canStay = block instanceof BlockDecoration ? ((BlockDecoration)block).canBlockStay(world, genPos, this.state) : block.canPlaceBlockAt(world, genPos); + + if (world.isAirBlock(genPos) && (!world.provider.getHasNoSky() || genPos.getY() < 255) && canStay) { - world.setBlockState(blockpos1, this.state, 2); + world.setBlockState(genPos, this.state, 2); } } diff --git a/src/main/java/biomesoplenty/common/world/feature/weighted/GeneratorGrass.java b/src/main/java/biomesoplenty/common/world/feature/weighted/GeneratorGrass.java new file mode 100644 index 000000000..2741ddab8 --- /dev/null +++ b/src/main/java/biomesoplenty/common/world/feature/weighted/GeneratorGrass.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * 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 net.minecraft.block.Block; +import net.minecraft.util.BlockPos; +import net.minecraft.world.World; +import biomesoplenty.common.block.BlockDecoration; +import biomesoplenty.common.util.biome.GeneratorUtils; + +public class GeneratorGrass extends GeneratorFlora +{ + @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() * 2); + genPos = genPos.add(0, y, 0); + + generate(world, random, genPos, amountPerChunk); + } + } + + @Override + public boolean generate(World world, Random random, BlockPos pos, int amountPerChunk) + { + Block block; + + do + { + block = world.getBlockState(pos).getBlock(); + if (!block.isAir(world, pos) && !block.isLeaves(world, pos)) break; + pos = pos.down(); + } + while (pos.getY() > 0); + + for (int i = 0; i < 128; i++) + { + BlockPos genPos = pos.add(random.nextInt(8) - random.nextInt(8), random.nextInt(4) - random.nextInt(4), random.nextInt(8) - random.nextInt(8)); + boolean canStay = block instanceof BlockDecoration ? ((BlockDecoration)block).canBlockStay(world, genPos, this.state) : block.canPlaceBlockAt(world, genPos); + + if (world.isAirBlock(genPos) && canStay) + { + world.setBlockState(genPos, this.state, 2); + } + } + + return true; + } +}