Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dreamlands.common.world.feature.tree;
- import com.dreamlands.common.block.DreamLeavesBlock;
- import com.dreamlands.common.world.feature.config.DreamTreeConfiguration;
- import com.mojang.serialization.Codec;
- import net.minecraft.core.BlockPos;
- import net.minecraft.core.Direction;
- import net.minecraft.tags.BlockTags;
- import net.minecraft.util.RandomSource;
- import net.minecraft.world.level.LevelAccessor;
- import net.minecraft.world.level.LevelSimulatedReader;
- import net.minecraft.world.level.WorldGenLevel;
- import net.minecraft.world.level.block.Blocks;
- import net.minecraft.world.level.block.RotatedPillarBlock;
- import net.minecraft.world.level.block.state.BlockState;
- import net.minecraft.world.level.levelgen.feature.Feature;
- import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext;
- public class EvergreenTreeFeature extends Feature<DreamTreeConfiguration> {
- public EvergreenTreeFeature(Codec<DreamTreeConfiguration> codec) {
- super(codec);
- }
- @Override
- public boolean place(FeaturePlaceContext<DreamTreeConfiguration> context) {
- WorldGenLevel level = context.level();
- RandomSource random = context.random();
- BlockPos startPos = context.origin();
- DreamTreeConfiguration config = context.config();
- int trunkHeight = config.minimumSize;
- level.setBlock(startPos.below(), Blocks.DIRT.defaultBlockState(), 2);
- BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos();
- for (int currentY = 0; currentY < trunkHeight; ++currentY) {
- this.placeLog(level, mutablePos.setWithOffset(startPos, 0, currentY, 0), random, config, Direction.Axis.Y);
- //if (currentY < 9) {
- // this.placeLog(level, mutablePos.setWithOffset(startPos, 1, currentY, 0), random, config, Direction.Axis.Y);
- // this.placeLog(level, mutablePos.setWithOffset(startPos, 1, currentY, -1), random, config, Direction.Axis.Y);
- // this.placeLog(level, mutablePos.setWithOffset(startPos, 0, currentY, -1), random, config, Direction.Axis.Y);
- //}
- for (Direction direction : Direction.Plane.HORIZONTAL) {
- BlockPos branchPos = startPos.above(currentY).relative(direction);
- BlockPos offsetPos = branchPos.mutable().move(direction.getClockWise(), 1);
- BlockPos oppositeOffsetPos = branchPos.mutable().move(direction.getOpposite().getClockWise(), 1);
- if (random.nextInt(4) == 0) {
- if (currentY > 2 && currentY < 5) {
- this.addBranchWithLeaves(level, branchPos, random, config, direction, 1, false, random.nextBoolean(), false);
- }
- }
- if (currentY == 7) {
- this.addStraightBranch(level, branchPos, random, config, direction, 5, true);
- }
- if (currentY == 8) {
- this.addDiagonalBranch(level, offsetPos, random, config, direction, 4, true);
- }
- if (currentY == 9) {
- this.addDiagonalBranch(level, oppositeOffsetPos, random, config, direction, 4, false);
- }
- //if (currentY == 10) {
- // this.addBranchWithLeaves(level, branchPos.mutable().move(direction.getOpposite().getClockWise(), 1), random, config, direction.getOpposite(), 3, 1, true, false);
- //}
- //if (currentY == 11) {
- // this.addBranchWithLeaves(level, branchPos.mutable().move(direction.getClockWise(), 1), random, config, direction, 3, 1, true, false);
- //}
- //if (currentY == 12) {
- // this.addBranchWithLeaves(level, branchPos, random, config, direction, 4, 1, false, false);
- //}
- //if (currentY == 13) {
- // this.addBranchWithLeaves(level, branchPos.mutable().move(direction.getClockWise(), 1), random, config, direction, 2, 0, true, false);
- //}
- //if (currentY == 14) {
- // this.addBranchWithLeaves(level, branchPos.mutable().move(direction.getOpposite().getClockWise(), 1), random, config, direction, 2, 0, true, false);
- //}
- //if (currentY == 16) {
- // this.addBranchWithLeaves(level, branchPos.mutable().move(direction.getOpposite().getClockWise(), 1), random, config, direction.getOpposite(), 2, 0, true, false);
- //}
- //if (currentY == 18) {
- // this.addBranchWithLeaves(level, branchPos, random, config, direction, 2, 0, false, false);
- //}
- //if (currentY >= 19 && currentY <= 21) {
- // this.addBranchWithLeaves(level, branchPos, random, config, direction, 1, 0, false, false);
- //}
- // add leaves to the top of the tree and alongside the sides.
- if (currentY >= 22) {
- //this.placeLeaves(level, startPos.above(currentY).above(4), random, config);
- //this.placeLeaves(level, startPos.above(currentY).above().relative(direction), random, config);
- }
- // add random little branches with leaves on the side of the top of the tree.
- if (random.nextInt(22) == 0) {
- if (currentY >= 22 && currentY < trunkHeight - 1) {
- //this.addBranchWithLeaves(level, branchPos, random, config, direction, 1, 0, false, false);
- }
- }
- // add random leaves above the sides for extra side height.
- if (currentY == 24) {
- if (random.nextInt(2) == 0) {
- //this.placeLeaves(level, startPos.above(currentY).above(2).relative(direction), random, config);
- }
- }
- }
- }
- return true;
- }
- private void addMiddleBranches(LevelAccessor level, BlockPos pos, RandomSource random, DreamTreeConfiguration config, Direction direction, int branchLength, boolean hasBottomLeaves) {
- }
- private void addBottomBranches(LevelAccessor level, BlockPos pos, RandomSource random, DreamTreeConfiguration config, Direction direction, int branchLength, boolean hasBottomLeaves) {
- }
- private void addStraightBranch(LevelAccessor level, BlockPos pos, RandomSource random, DreamTreeConfiguration config, Direction direction, int branchLength, boolean hasBottomLeaves) {
- this.addBranchWithLeaves(level, pos, random, config, direction, branchLength, false, false, hasBottomLeaves);
- }
- private void addDiagonalBranch(LevelAccessor level, BlockPos pos, RandomSource random, DreamTreeConfiguration config, Direction direction, int branchLength, boolean hasBottomLeaves) {
- this.addBranchWithLeaves(level, pos, random, config, direction, branchLength, true, true, hasBottomLeaves);
- }
- /**
- * places a straight branch, with preset leaves surrounding it.
- *
- * @param branchLength for how long the branch will be. make it longer the further down it is on the tree.
- * @param diagonal determines is the branch will be placed straight or diagonally. *longer straight branches will branch out diagonally very slightly*
- */
- private void addBranchWithLeaves(LevelAccessor level, BlockPos pos, RandomSource random, DreamTreeConfiguration config, Direction direction, int branchLength, boolean diagonal, boolean hasOffsetLeaves, boolean hasBottomLeaves) {
- int verticalOffset = 0;
- for (int i = 0; i < branchLength; i++) {
- BlockPos branchPos = diagonal ? pos.relative(direction, i).relative(direction.getClockWise(), i).below(verticalOffset) : pos.relative(direction, i).below(verticalOffset);
- this.placeLog(level, branchPos, random, config, direction.getAxis());
- this.placeSurroundingLeaves(level, branchPos, random, direction, config, hasOffsetLeaves, hasBottomLeaves);
- verticalOffset = i > 0 ? 1 : 0;
- }
- }
- private void placeLog(LevelAccessor level, BlockPos pos, RandomSource random, DreamTreeConfiguration config, Direction.Axis axis) {
- level.setBlock(pos, config.trunkProvider.getState(random, pos).setValue(RotatedPillarBlock.AXIS, axis), 2);
- }
- private void placeSurroundingLeaves(LevelAccessor level, BlockPos pos, RandomSource random, Direction direction, DreamTreeConfiguration config, boolean hasOffsetLeaves, boolean hasBottomLeaves) {
- this.placeLeaves(level, pos.above(), random, config);
- this.placeLeaves(level, pos.north(), random, config);
- this.placeLeaves(level, pos.east(), random, config);
- this.placeLeaves(level, pos.south(), random, config);
- this.placeLeaves(level, pos.west(), random, config);
- if (hasBottomLeaves) {
- this.placeLeaves(level, pos.below(), random, config);
- }
- if (hasOffsetLeaves) {
- this.placeLeaves(level, pos.offset(direction.getNormal().relative(direction.getClockWise())), random, config);
- }
- }
- private void placeLeaves(LevelAccessor level, BlockPos pos, RandomSource random, DreamTreeConfiguration config) {
- if (isReplaceable(level, pos)) {
- level.setBlock(pos, config.foliageProvider.getState(random, pos).setValue(DreamLeavesBlock.DISTANCE, 1), 2);
- }
- }
- private static boolean isReplaceable(LevelSimulatedReader level, BlockPos pos) {
- return level.isStateAtPosition(pos, EvergreenTreeFeature::isReplaceableBlock);
- }
- private static boolean isReplaceableBlock(BlockState state) {
- return state.is(BlockTags.REPLACEABLE_BY_TREES) || state.canBeReplaced();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement