From b9f3d9c60a18340ee881436f81af0587b78c14e9 Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Sun, 21 Feb 2016 13:40:20 +0000 Subject: [PATCH] Custom rubber tree generation algorithm, tress now spawn with sap, and they dont clip other tress. They also have a spike --- src/main/java/techreborn/Core.java | 2 +- .../techreborn/blocks/BlockRubberSapling.java | 3 +- .../techreborn/world/RubberTreeGenerator.java | 140 +++++++++++++++++- .../java/techreborn/world/TreeGenerator.java | 37 +++-- 4 files changed, 160 insertions(+), 22 deletions(-) diff --git a/src/main/java/techreborn/Core.java b/src/main/java/techreborn/Core.java index 7b390470e..07ee6e04f 100644 --- a/src/main/java/techreborn/Core.java +++ b/src/main/java/techreborn/Core.java @@ -107,7 +107,7 @@ public class Core { } // WorldGen GameRegistry.registerWorldGenerator(new TROreGen(), 0); - GameRegistry.registerWorldGenerator(new TreeGenerator(), 2); + GameRegistry.registerWorldGenerator(new TreeGenerator(), 0); // DungeonLoot.init(); // Register Gui Handler NetworkRegistry.INSTANCE.registerGuiHandler(INSTANCE, new GuiHandler()); diff --git a/src/main/java/techreborn/blocks/BlockRubberSapling.java b/src/main/java/techreborn/blocks/BlockRubberSapling.java index 9251b3dfc..ddda77a7a 100644 --- a/src/main/java/techreborn/blocks/BlockRubberSapling.java +++ b/src/main/java/techreborn/blocks/BlockRubberSapling.java @@ -8,6 +8,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.util.BlockPos; import net.minecraft.world.World; import techreborn.client.TechRebornCreativeTabMisc; +import techreborn.world.RubberTreeGenerator; import techreborn.world.TreeGenerator; import java.util.List; @@ -30,7 +31,7 @@ public class BlockRubberSapling extends BlockSapling { return; } worldIn.setBlockToAir(pos); - TreeGenerator.treeGenerator.generate(worldIn, rand, pos); + new RubberTreeGenerator(false).generate(worldIn, rand, pos); } @Override diff --git a/src/main/java/techreborn/world/RubberTreeGenerator.java b/src/main/java/techreborn/world/RubberTreeGenerator.java index f0f92d408..5427d21a8 100644 --- a/src/main/java/techreborn/world/RubberTreeGenerator.java +++ b/src/main/java/techreborn/world/RubberTreeGenerator.java @@ -1,15 +1,141 @@ package techreborn.world; +import net.minecraft.block.Block; +import net.minecraft.block.state.BlockState; import net.minecraft.block.state.IBlockState; -import net.minecraft.world.gen.feature.WorldGenTrees; +import net.minecraft.util.BlockPos; +import net.minecraft.util.EnumFacing; +import net.minecraft.world.World; +import net.minecraft.world.gen.feature.WorldGenerator; +import net.minecraftforge.common.IPlantable; +import techreborn.blocks.BlockRubberLog; +import techreborn.init.ModBlocks; -/** - * Created by Mark on 19/02/2016. - */ -public class RubberTreeGenerator extends WorldGenTrees { +import java.util.Random; +public class RubberTreeGenerator extends WorldGenerator { - public RubberTreeGenerator(boolean doBlockNotify, int minTreeHeight, IBlockState woodState, IBlockState leaveState, boolean vinesGrow) { - super(doBlockNotify, minTreeHeight, woodState, leaveState, vinesGrow); + boolean isWorldGen = true; + + public RubberTreeGenerator() { + super(); + } + + public RubberTreeGenerator(boolean isWorldGen) { + super(); + this.isWorldGen = isWorldGen; + } + + @Override + public boolean generate(World worldIn, Random rand, BlockPos position) { + int x = position.getX(); + int z = position.getZ(); + int retries = rand.nextInt(3) + 2; + if (isWorldGen) { + for (int c = 0; c < retries; c++) { + int y = worldIn.getActualHeight() - 1; + while (worldIn.isAirBlock(new BlockPos(x, y, z)) && y > 0) { + y--; + } + if (!growTree(worldIn, rand, x, y + 1, z)) { + retries--; + } + x += rand.nextInt(16) - 8; + z += rand.nextInt(16) - 8; + } + } else { + int y = worldIn.getActualHeight() - 1; + while (worldIn.isAirBlock(new BlockPos(x, y, z)) && y > 0) { + y--; + } + return growTree(worldIn, rand, x, y + 1, z); + } + + return false; + } + + public boolean growTree(World world, Random rand, int x, int y, int z) { + int treeHeight = rand.nextInt(5) + 5; + int worldHeight = world.getHeight(); + if (y >= 1 && y + treeHeight + 1 <= worldHeight) { + int xOffset; + int yOffset; + int zOffset; + Block baseBlock = world.getBlockState(new BlockPos(x, y - 1, z)).getBlock(); + if (baseBlock != null && baseBlock.canSustainPlant(world, new BlockPos(x, y - 1, z), EnumFacing.UP, (IPlantable) ModBlocks.rubberSapling) && y < worldHeight - treeHeight - 1) { + for (yOffset = y; yOffset <= y + 1 + treeHeight; ++yOffset) { + byte radius = 1; + if (yOffset == y) { + radius = 0; + } + if (yOffset >= y + 1 + treeHeight - 2) { + radius = 2; + } + if (yOffset >= 0 & yOffset < worldHeight) { + for (xOffset = x - radius; xOffset <= x + radius; ++xOffset) { + for (zOffset = z - radius; zOffset <= z + radius; ++zOffset) { + Block block = world.getBlockState(new BlockPos(xOffset, yOffset, zOffset)).getBlock(); + + if (block != null && !(block.isLeaves(world, new BlockPos(xOffset, yOffset, zOffset)) || + block.isAir(world, new BlockPos(xOffset, yOffset, zOffset)) || + block.canBeReplacedByLeaves(world, new BlockPos(xOffset, yOffset, zOffset)))) { + return false; + } + } + } + } else { + return false; + } + } + + BlockPos treeBase = new BlockPos(x, y, z); + BlockPos treeRoot = treeBase.down(); + world.getBlockState(treeRoot).getBlock().onPlantGrow(world, treeRoot, treeBase); + for (yOffset = y - 3 + treeHeight; yOffset <= y + treeHeight; ++yOffset) { + int var12 = yOffset - (y + treeHeight), + center = 1 - var12 / 2; + for (xOffset = x - center; xOffset <= x + center; ++xOffset) { + int xPos = xOffset - x, t = xPos >> 15; + xPos = (xPos + t) ^ t; + for (zOffset = z - center; zOffset <= z + center; ++zOffset) { + int zPos = zOffset - z; + zPos = (zPos + (t = zPos >> 31)) ^ t; + Block block = world.getBlockState(new BlockPos(xOffset, yOffset, zOffset)).getBlock(); + if (((xPos != center | zPos != center) || rand.nextInt(2) != 0 && var12 != 0) && (block == null || block.isLeaves(world, new BlockPos(xOffset, yOffset, zOffset)) || block.isAir(world, new BlockPos(xOffset, yOffset, zOffset)) || block.canBeReplacedByLeaves(world, new BlockPos(xOffset, yOffset, zOffset)))) { + this.setBlockAndNotifyAdequately(world, new BlockPos(xOffset, yOffset, zOffset), ModBlocks.rubberLeaves.getDefaultState()); + } + } + } + } + + BlockPos topLogPos = null; + for (yOffset = 0; yOffset < treeHeight; ++yOffset) { + BlockPos blockpos = new BlockPos(x, y + yOffset, z); + Block block = world.getBlockState(blockpos).getBlock(); + if (block == null || block.isAir(world, blockpos) || block.isLeaves(world, blockpos) || block.isReplaceable(world, blockpos)) { + IBlockState newState = ModBlocks.rubberLog.getDefaultState(); + boolean isAddingSap = false; + if(rand.nextInt(10) == 0){ + newState = newState.withProperty(BlockRubberLog.HAS_SAP, true).withProperty(BlockRubberLog.SAP_SIDE, EnumFacing.getHorizontal(rand.nextInt(3))); + isAddingSap = true; + } + if(isAddingSap){ + world.setBlockState(blockpos, newState, 2); + } else { + this.setBlockAndNotifyAdequately(world, blockpos, newState); + } + topLogPos = blockpos; + } + } + if(topLogPos != null){ + for (int i = 0; i < 4; i++) { + BlockPos spikePos = topLogPos.up(i); + this.setBlockAndNotifyAdequately(world, spikePos, ModBlocks.rubberLeaves.getDefaultState()); + } + } + } + return true; + } + return false; } } diff --git a/src/main/java/techreborn/world/TreeGenerator.java b/src/main/java/techreborn/world/TreeGenerator.java index a82eeecaa..8356927a3 100644 --- a/src/main/java/techreborn/world/TreeGenerator.java +++ b/src/main/java/techreborn/world/TreeGenerator.java @@ -1,14 +1,11 @@ package techreborn.world; -import net.minecraft.init.Blocks; import net.minecraft.util.BlockPos; import net.minecraft.world.World; import net.minecraft.world.biome.BiomeGenBase; import net.minecraft.world.chunk.IChunkProvider; import net.minecraftforge.common.BiomeDictionary; import net.minecraftforge.fml.common.IWorldGenerator; -import net.minecraftforge.fml.common.Mod; -import techreborn.init.ModBlocks; import java.util.Random; @@ -17,22 +14,36 @@ import java.util.Random; */ public class TreeGenerator implements IWorldGenerator { - public static RubberTreeGenerator treeGenerator = new RubberTreeGenerator(false, 8, ModBlocks.rubberLog.getDefaultState(), ModBlocks.rubberLeaves.getDefaultState(), false); + public static RubberTreeGenerator treeGenerator = new RubberTreeGenerator(); @Override public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider) { - int chance = 10; - BiomeGenBase biomeGenBase = world.getBiomeGenForCoords( new BlockPos(chunkX * 16, 72, chunkZ * 16)); - if(BiomeDictionary.isBiomeOfType(biomeGenBase, BiomeDictionary.Type.SWAMP)){ - chance -= random.nextInt(5) + 5; + int chance = 75; + boolean isValidSpawn = false; + BiomeGenBase biomeGenBase = world.getBiomeGenForCoords(new BlockPos(chunkX * 16, 72, chunkZ * 16)); + if (BiomeDictionary.isBiomeOfType(biomeGenBase, BiomeDictionary.Type.SWAMP)) { + chance -= random.nextInt(10) + 10; + isValidSpawn = true; } - if(BiomeDictionary.isBiomeOfType(biomeGenBase, BiomeDictionary.Type.FOREST) || BiomeDictionary.isBiomeOfType(biomeGenBase, BiomeDictionary.Type.JUNGLE)){ - chance -= random.nextInt(2) + 1; + if (BiomeDictionary.isBiomeOfType(biomeGenBase, BiomeDictionary.Type.FOREST) || BiomeDictionary.isBiomeOfType(biomeGenBase, BiomeDictionary.Type.JUNGLE)) { + chance -= random.nextInt(5) + 3; + isValidSpawn = true; } - if(world.provider.isSurfaceWorld()){ - if(random.nextInt(chance) == 0){ + if (!isValidSpawn) { + return; + } + if (world.provider.isSurfaceWorld()) { + if (random.nextInt(chance) == 0) { + int x = (chunkX * 16) + random.nextInt(15); + int z = (chunkZ * 16) + random.nextInt(15); for (int i = 0; i < 7; i++) { - treeGenerator.generate(world, random, new BlockPos((chunkX + random.nextInt(3)) * 16, 72, (chunkZ + random.nextInt(3)) * 16)); + int y = world.getActualHeight() - 1; + while (world.isAirBlock(new BlockPos(x, y, z)) && y > 0) { + y--; + } + treeGenerator.generate(world, random, new BlockPos(x, 72, z)); + x += random.nextInt(16) - 8; + z += random.nextInt(16) - 8; } }