From 704ad8df0796332073eda6255bf9ff836f37336f Mon Sep 17 00:00:00 2001 From: drcrazy Date: Fri, 15 Mar 2019 16:36:34 +0300 Subject: [PATCH] Rubber tree can grow now. --- .../world/feature/RubberTreeFeature.java | 76 +++++++++++++++++-- 1 file changed, 70 insertions(+), 6 deletions(-) diff --git a/src/main/java/techreborn/world/feature/RubberTreeFeature.java b/src/main/java/techreborn/world/feature/RubberTreeFeature.java index fb3bc0af7..51a10f2f2 100644 --- a/src/main/java/techreborn/world/feature/RubberTreeFeature.java +++ b/src/main/java/techreborn/world/feature/RubberTreeFeature.java @@ -27,12 +27,14 @@ package techreborn.world.feature; import java.util.Random; import java.util.Set; -import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; +import net.minecraft.tags.BlockTags; +import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IWorld; import net.minecraft.world.gen.feature.AbstractTreeFeature; import net.minecraft.world.gen.feature.NoFeatureConfig; +import techreborn.blocks.BlockRubberLog; import techreborn.init.TRContent; /** @@ -43,6 +45,9 @@ public class RubberTreeFeature extends AbstractTreeFeature { //TODO: Configs private int treeBaseHeight = 5; + private int sapRarity = 10; + private int spireHeight = 4; + final IBlockState leafState = TRContent.RUBBER_LEAVES.getDefaultState(); public RubberTreeFeature() { super(false); @@ -58,7 +63,9 @@ public class RubberTreeFeature extends AbstractTreeFeature { int baseZ = saplingPos.getZ(); if (baseY <= 1 && baseY + treeHeight + 1 >= worldHeight) { return false; } - boolean isSoil = worldIn.getBlockState(saplingPos.down()).canSustainPlant(worldIn, saplingPos.down(), + BlockPos rootBlockPos = saplingPos.down(); + IBlockState rootBlockState = worldIn.getBlockState(rootBlockPos); + boolean isSoil = rootBlockState.canSustainPlant(worldIn, rootBlockPos, net.minecraft.util.EnumFacing.UP, (net.minecraft.block.BlockSapling) TRContent.RUBBER_SAPLING.getBlock()); if (!isSoil) { return false; } @@ -86,11 +93,68 @@ public class RubberTreeFeature extends AbstractTreeFeature { } // Ok, we are cleared for take off! - // TODO: Grow tree after log blockstate fix - + boolean hasPlacedBlock = false; - return false; + // Leaves + for (yOffset = baseY - 3 + treeHeight; yOffset <= baseY + treeHeight; ++yOffset) { + int var12 = yOffset - (baseY + treeHeight), center = 1 - var12 / 2; + for (xOffset = baseX - center; xOffset <= baseX + center; ++xOffset) { + int xPos = xOffset - baseX, t = xPos >> 15; + xPos = (xPos + t) ^ t; + for (zOffset = baseZ - center; zOffset <= baseZ + center; ++zOffset) { + int zPos = zOffset - baseZ; + zPos = (zPos + (t = zPos >> 31)) ^ t; + if ((xPos != center | zPos != center) || rand.nextInt(2) != 0 && var12 != 0) { + BlockPos blockpos = new BlockPos(xOffset, yOffset, zOffset); + hasPlacedBlock = growLeaves(worldIn, blockpos); + } + } + } + } + + // Trunk + BlockPos topLogPos = null; + IBlockState logState = TRContent.RUBBER_LOG.getDefaultState(); + for (yOffset = 0; yOffset < treeHeight; ++yOffset) { + BlockPos blockpos = new BlockPos(baseX, baseY + yOffset, baseZ); + IBlockState state1 = worldIn.getBlockState(blockpos); + if (state1.isAir(worldIn, blockpos) || state1.isIn(BlockTags.LEAVES)) { + + if (rand.nextInt(sapRarity) == 0) { + logState = logState.with(BlockRubberLog.HAS_SAP, true) + .with(BlockRubberLog.SAP_SIDE, EnumFacing.byHorizontalIndex(rand.nextInt(4))); + } + + //setBlockState(worldIn, blockpos, logState); + this.setLogState(changedBlocks, worldIn, blockpos, logState); + + hasPlacedBlock = true; + topLogPos = blockpos; + } + } + + // Spire + if (topLogPos != null) { + for (int i = 0; i < spireHeight; i++) { + BlockPos blockpos = topLogPos.up(i); + growLeaves(worldIn, blockpos); + } + } + + if (hasPlacedBlock) { + setDirtAt(worldIn, rootBlockPos, saplingPos); + } + + return hasPlacedBlock; } - + private boolean growLeaves(IWorld worldIn, BlockPos pos) { + IBlockState state1 = worldIn.getBlockState(pos); + if (state1.canBeReplacedByLeaves(worldIn, pos)) { + this.setBlockState(worldIn, pos, leafState); + return true; + } + + return false; + } }