diff --git a/src/main/java/techreborn/blocks/misc/BlockRubberSapling.java b/src/main/java/techreborn/blocks/misc/BlockRubberSapling.java index ced31f9c0..b9eef23d1 100644 --- a/src/main/java/techreborn/blocks/misc/BlockRubberSapling.java +++ b/src/main/java/techreborn/blocks/misc/BlockRubberSapling.java @@ -28,7 +28,7 @@ import net.fabricmc.fabric.api.block.FabricBlockSettings; import net.minecraft.block.Material; import net.minecraft.block.SaplingBlock; import net.minecraft.sound.BlockSoundGroup; -import techreborn.world.RubberTreeGenerator; +import techreborn.world.RubberSaplingGenerator; /** * Created by modmuss50 on 20/02/2016. @@ -36,6 +36,6 @@ import techreborn.world.RubberTreeGenerator; public class BlockRubberSapling extends SaplingBlock { public BlockRubberSapling() { - super(new RubberTreeGenerator(), FabricBlockSettings.of(Material.PLANT).sounds(BlockSoundGroup.GRASS).build()); + super(new RubberSaplingGenerator(), FabricBlockSettings.of(Material.PLANT).sounds(BlockSoundGroup.GRASS).build()); } } diff --git a/src/main/java/techreborn/world/RubberSaplingGenerator.java b/src/main/java/techreborn/world/RubberSaplingGenerator.java index 4a2933014..af19222a0 100644 --- a/src/main/java/techreborn/world/RubberSaplingGenerator.java +++ b/src/main/java/techreborn/world/RubberSaplingGenerator.java @@ -25,7 +25,6 @@ package techreborn.world; import net.minecraft.block.sapling.SaplingGenerator; -import net.minecraft.world.IWorld; import net.minecraft.world.gen.feature.AbstractTreeFeature; import net.minecraft.world.gen.feature.DefaultFeatureConfig; @@ -33,146 +32,8 @@ import javax.annotation.Nullable; import java.util.Random; public class RubberSaplingGenerator extends SaplingGenerator { - - boolean isWorldGen = true; - - public RubberSaplingGenerator() { - super(); - } - @Nullable - @Override protected AbstractTreeFeature createTreeFeature(Random random) { - return null; + return new RubberTreeFeature(DefaultFeatureConfig::deserialize, true); } - - public RubberSaplingGenerator(boolean isWorldGen) { - super(); - this.isWorldGen = isWorldGen; - } - -// @Override -// public boolean generate(World worldIn, Random rand, BlockPos position) { -// int retries = rand.nextInt(3) + 2; -// for (int c = 0; c < retries; c++) { -// int x = position.getX() + 8 + rand.nextInt(16); -// int z = position.getZ() + 8 + rand.nextInt(16); -// int y = worldIn.getHeight(x, z) - 1; -// while (worldIn.isAirBlock(new BlockPos(x, y, z)) && y > 0) { -// y--; -// } -// if (!growTree(worldIn, rand, x, y + 1, z)) { -// retries--; -// } -// } -// -// return false; -// } - - public boolean growTree(IWorld world, Random rand, int x, int y, int z) { - /* - int treeHeight = rand.nextInt(5) + TechReborn.worldGen.config.rubberTreeConfig.treeBaseHeight; - int worldHeight = world.getHeight(); - if (y >= 1 && y + treeHeight + 1 <= worldHeight) { - int xOffset; - int yOffset; - int zOffset; - IBlockState baseSate = world.getBlockState(new BlockPos(x, y - 1, z)); - Block baseBlock = baseSate.getBlock(); - boolean hasPlacedBlock = false; - if (baseBlock != null && baseBlock.canSustainPlant(baseSate, world, new BlockPos(x, y - 1, z), - EnumFacing.UP, (IPlantable) TRContent.RUBBER_SAPLING) && 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) { - IBlockState state = world.getBlockState(new BlockPos(xOffset, yOffset, zOffset)); - Block block = state.getBlock(); - - if (block != null - && !(block.isLeaves(state, world, new BlockPos(xOffset, yOffset, zOffset)) - || block.isAir(state, world, new BlockPos(xOffset, yOffset, zOffset)) - || block.canBeReplacedByLeaves(state, world, - new BlockPos(xOffset, yOffset, zOffset)))) { - return false; - } - } - } - } else { - return false; - } - } - - BlockPos treeBase = new BlockPos(x, y, z); - BlockPos treeRoot = treeBase.down(); - IBlockState state = world.getBlockState(treeRoot); - state.getBlock().onPlantGrow(state, 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; - IBlockState state1 = world.getBlockState(new BlockPos(xOffset, yOffset, zOffset)); - Block block = state1.getBlock(); - if (((xPos != center | zPos != center) || rand.nextInt(2) != 0 && var12 != 0) - && (block == null - || block.isLeaves(state1, world, new BlockPos(xOffset, yOffset, zOffset)) - || block.isAir(state1, world, new BlockPos(xOffset, yOffset, zOffset)) - || block.canBeReplacedByLeaves(state1, world, - new BlockPos(xOffset, yOffset, zOffset)))) { - this.setBlockAndNotifyAdequately(world, new BlockPos(xOffset, yOffset, zOffset), - TRContent.RUBBER_LEAVES.getDefaultState().with(BlockRubberLeaves.DECAYABLE, true).with(BlockRubberLeaves.CHECK_DECAY, false)); - hasPlacedBlock = true; - } - } - } - } - - BlockPos topLogPos = null; - for (yOffset = 0; yOffset < treeHeight; ++yOffset) { - BlockPos blockpos = new BlockPos(x, y + yOffset, z); - IBlockState state1 = world.getBlockState(blockpos); - Block block = state1.getBlock(); - if (block == null || block.isAir(state1, world, blockpos) || block.isLeaves(state1, world, blockpos) - || block.isReplaceable(world, blockpos)) { - IBlockState newState = TRContent.RUBBER_LOG.getDefaultState(); - boolean isAddingSap = false; - if (rand.nextInt(TechReborn.worldGen.config.rubberTreeConfig.sapRarity) == 0) { - newState = newState.with(BlockRubberLog.HAS_SAP, true) - .with(BlockRubberLog.SAP_SIDE, EnumFacing.byHorizontalIndex(rand.nextInt(4))); - isAddingSap = true; - } - if (isAddingSap) { - world.setBlockState(blockpos, newState, 2); - } else { - this.setBlockAndNotifyAdequately(world, blockpos, newState); - } - hasPlacedBlock = true; - topLogPos = blockpos; - } - } - if (topLogPos != null) { - for (int i = 0; i < TechReborn.worldGen.config.rubberTreeConfig.spireHeight; i++) { - BlockPos spikePos = topLogPos.up(i); - this.setBlockAndNotifyAdequately(world, spikePos, TRContent.RUBBER_LEAVES.getDefaultState() - .with(BlockRubberLeaves.DECAYABLE, true).with(BlockRubberLeaves.CHECK_DECAY, false)); - } - } - } - return hasPlacedBlock; - } - */ - return false; - } - } diff --git a/src/main/java/techreborn/world/RubberTreeFeature.java b/src/main/java/techreborn/world/RubberTreeFeature.java index d89f9d3a0..af68f0802 100644 --- a/src/main/java/techreborn/world/RubberTreeFeature.java +++ b/src/main/java/techreborn/world/RubberTreeFeature.java @@ -30,11 +30,15 @@ import java.util.function.Function; import com.mojang.datafixers.Dynamic; +import net.minecraft.block.BlockState; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; import net.minecraft.util.math.MutableIntBoundingBox; import net.minecraft.world.ModifiableTestableWorld; import net.minecraft.world.gen.feature.AbstractTreeFeature; import net.minecraft.world.gen.feature.DefaultFeatureConfig; +import techreborn.blocks.misc.BlockRubberLog; +import techreborn.init.TRContent; /** * @author drcrazy @@ -42,16 +46,100 @@ import net.minecraft.world.gen.feature.DefaultFeatureConfig; */ public class RubberTreeFeature extends AbstractTreeFeature { - public RubberTreeFeature(Function, ? extends DefaultFeatureConfig> featureConfig, boolean boolean_1) { - super(featureConfig, boolean_1); - // TODO Auto-generated constructor stub + // TODO: Configs + private int treeBaseHeight = 5; + private int sapRarity = 10; + private int spireHeight = 4; + + + public RubberTreeFeature(Function, ? extends DefaultFeatureConfig> featureConfig, boolean notify) { + super(featureConfig, notify); } @Override - protected boolean generate(Set changedBlocks, ModifiableTestableWorld worldIn, Random rand, BlockPos saplingPos, - MutableIntBoundingBox var5) { - // TODO Auto-generated method stub - return false; + protected boolean generate(Set changedBlocks, ModifiableTestableWorld worldIn, Random rand, + BlockPos saplingPos, MutableIntBoundingBox mutableBoundingBox) { + int treeHeight = rand.nextInt(5) + treeBaseHeight; + int baseY = saplingPos.getY(); + int baseX = saplingPos.getX(); + int baseZ = saplingPos.getZ(); + if (baseY <= 1 && baseY + treeHeight + spireHeight + 1 >= 256) { + return false; + } + + if (!isDirtOrGrass(worldIn, saplingPos.down())) { + return false; + } + + int yOffset; + int xOffset; + int zOffset; + for (yOffset = baseY; yOffset <= baseY + 1 + treeHeight; ++yOffset) { + byte radius = 1; + if (yOffset == baseY) { + radius = 0; + } + if (yOffset >= baseY + 1 + treeHeight - 2) { + radius = 2; + } + + BlockPos.Mutable blockpos$mutable = new BlockPos.Mutable(); + for (xOffset = baseX - radius; xOffset <= baseX + radius; ++xOffset) { + for (zOffset = baseZ - radius; zOffset <= baseZ + radius; ++zOffset) { + if (!isAirOrLeaves(worldIn, blockpos$mutable.set(xOffset, yOffset, zOffset))) { + return false; + } + } + } + } + + // Ok, we are cleared for take off! + this.setToDirt(worldIn, saplingPos.down()); + + // Leaves + BlockState leafState = TRContent.RUBBER_LEAVES.getDefaultState(); + 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 leafPos = new BlockPos(xOffset, yOffset, zOffset); + if (isAirOrLeaves(worldIn, leafPos) || isReplaceablePlant(worldIn, leafPos)) { + this.setBlockState(changedBlocks, worldIn, leafPos, leafState, mutableBoundingBox); + } + } + } + } + } + + // Trunk + BlockPos currentLogPos = null; + BlockState logState = TRContent.RUBBER_LOG.getDefaultState(); + for (yOffset = 0; yOffset < treeHeight; ++yOffset) { + currentLogPos = saplingPos.up(yOffset); + if (!isAirOrLeaves(worldIn, currentLogPos)) { + continue; + } + if (rand.nextInt(sapRarity) == 0) { + logState = logState.with(BlockRubberLog.HAS_SAP, true).with(BlockRubberLog.SAP_SIDE, + Direction.fromHorizontal(rand.nextInt(4))); + } + this.setBlockState(changedBlocks, worldIn, currentLogPos, logState, mutableBoundingBox); + } + + // Spire + if (currentLogPos != null) { + for (int i = 0; i < spireHeight; i++) { + BlockPos blockpos = currentLogPos.up(i); + this.setBlockState(changedBlocks, worldIn, blockpos, leafState, mutableBoundingBox); + } + } + + return true; } }