Advertisement
jayhillx

evergreentree

Dec 28th, 2024
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.78 KB | None | 0 0
  1. package com.dreamlands.common.world.feature.tree;
  2.  
  3. import com.dreamlands.common.block.DreamLeavesBlock;
  4. import com.dreamlands.common.world.feature.config.DreamTreeConfiguration;
  5. import com.mojang.serialization.Codec;
  6. import net.minecraft.core.BlockPos;
  7. import net.minecraft.core.Direction;
  8. import net.minecraft.tags.BlockTags;
  9. import net.minecraft.util.RandomSource;
  10. import net.minecraft.world.level.LevelAccessor;
  11. import net.minecraft.world.level.LevelSimulatedReader;
  12. import net.minecraft.world.level.WorldGenLevel;
  13. import net.minecraft.world.level.block.Blocks;
  14. import net.minecraft.world.level.block.RotatedPillarBlock;
  15. import net.minecraft.world.level.block.state.BlockState;
  16. import net.minecraft.world.level.levelgen.feature.Feature;
  17. import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext;
  18.  
  19. public class EvergreenTreeFeature extends Feature<DreamTreeConfiguration> {
  20.  
  21.     public EvergreenTreeFeature(Codec<DreamTreeConfiguration> codec) {
  22.         super(codec);
  23.     }
  24.  
  25.     @Override
  26.     public boolean place(FeaturePlaceContext<DreamTreeConfiguration> context) {
  27.         WorldGenLevel level = context.level();
  28.         RandomSource random = context.random();
  29.         BlockPos startPos = context.origin();
  30.         DreamTreeConfiguration config = context.config();
  31.         int trunkHeight = config.minimumSize;
  32.  
  33.         level.setBlock(startPos.below(), Blocks.DIRT.defaultBlockState(), 2);
  34.  
  35.         BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos();
  36.         for (int currentY = 0; currentY < trunkHeight; ++currentY) {
  37.             this.placeLog(level, mutablePos.setWithOffset(startPos, 0, currentY, 0), random, config, Direction.Axis.Y);
  38.  
  39.             //if (currentY < 9) {
  40.             //    this.placeLog(level, mutablePos.setWithOffset(startPos, 1, currentY, 0), random, config, Direction.Axis.Y);
  41.             //    this.placeLog(level, mutablePos.setWithOffset(startPos, 1, currentY, -1), random, config, Direction.Axis.Y);
  42.             //    this.placeLog(level, mutablePos.setWithOffset(startPos, 0, currentY, -1), random, config, Direction.Axis.Y);
  43.             //}
  44.  
  45.             for (Direction direction : Direction.Plane.HORIZONTAL) {
  46.                 BlockPos branchPos = startPos.above(currentY).relative(direction);
  47.                 BlockPos offsetPos = branchPos.mutable().move(direction.getClockWise(), 1);
  48.                 BlockPos oppositeOffsetPos = branchPos.mutable().move(direction.getOpposite().getClockWise(), 1);
  49.  
  50.                 if (random.nextInt(4) == 0) {
  51.                     if (currentY > 2 && currentY < 5) {
  52.                         this.addBranchWithLeaves(level, branchPos, random, config, direction, 1, false, random.nextBoolean(), false);
  53.                     }
  54.                 }
  55.  
  56.                 if (currentY == 7) {
  57.                     this.addStraightBranch(level, branchPos, random, config, direction, 5, true);
  58.                 }
  59.  
  60.                 if (currentY == 8) {
  61.                     this.addDiagonalBranch(level, offsetPos, random, config, direction, 4, true);
  62.                 }
  63.  
  64.                 if (currentY == 9) {
  65.                     this.addDiagonalBranch(level, oppositeOffsetPos, random, config, direction, 4, false);
  66.                 }
  67.  
  68.                 //if (currentY == 10) {
  69.                 //    this.addBranchWithLeaves(level, branchPos.mutable().move(direction.getOpposite().getClockWise(), 1), random, config, direction.getOpposite(), 3, 1, true, false);
  70.                 //}
  71.  
  72.                 //if (currentY == 11) {
  73.                 //    this.addBranchWithLeaves(level, branchPos.mutable().move(direction.getClockWise(), 1), random, config, direction, 3, 1, true, false);
  74.                 //}
  75.  
  76.                 //if (currentY == 12) {
  77.                 //    this.addBranchWithLeaves(level, branchPos, random, config, direction, 4, 1, false, false);
  78.                 //}
  79.  
  80.                 //if (currentY == 13) {
  81.                 //    this.addBranchWithLeaves(level, branchPos.mutable().move(direction.getClockWise(), 1), random, config, direction, 2, 0, true, false);
  82.                 //}
  83.  
  84.                 //if (currentY == 14) {
  85.                 //    this.addBranchWithLeaves(level, branchPos.mutable().move(direction.getOpposite().getClockWise(), 1), random, config, direction, 2, 0, true, false);
  86.                 //}
  87.  
  88.                 //if (currentY == 16) {
  89.                 //    this.addBranchWithLeaves(level, branchPos.mutable().move(direction.getOpposite().getClockWise(), 1), random, config, direction.getOpposite(), 2, 0, true, false);
  90.                 //}
  91.  
  92.                 //if (currentY == 18) {
  93.                 //    this.addBranchWithLeaves(level, branchPos, random, config, direction, 2, 0, false, false);
  94.                 //}
  95.  
  96.                 //if (currentY >= 19 && currentY <= 21) {
  97.                 //    this.addBranchWithLeaves(level, branchPos, random, config, direction, 1, 0, false, false);
  98.                 //}
  99.  
  100.                 // add leaves to the top of the tree and alongside the sides.
  101.                 if (currentY >= 22) {
  102.                     //this.placeLeaves(level, startPos.above(currentY).above(4), random, config);
  103.                     //this.placeLeaves(level, startPos.above(currentY).above().relative(direction), random, config);
  104.                 }
  105.  
  106.                 // add random little branches with leaves on the side of the top of the tree.
  107.                 if (random.nextInt(22) == 0) {
  108.                     if (currentY >= 22 && currentY < trunkHeight - 1) {
  109.                         //this.addBranchWithLeaves(level, branchPos, random, config, direction, 1, 0, false, false);
  110.                     }
  111.                 }
  112.  
  113.                 // add random leaves above the sides for extra side height.
  114.                 if (currentY == 24) {
  115.                     if (random.nextInt(2) == 0) {
  116.                         //this.placeLeaves(level, startPos.above(currentY).above(2).relative(direction), random, config);
  117.                     }
  118.                 }
  119.             }
  120.         }
  121.         return true;
  122.     }
  123.  
  124.     private void addMiddleBranches(LevelAccessor level, BlockPos pos, RandomSource random, DreamTreeConfiguration config, Direction direction, int branchLength, boolean hasBottomLeaves) {
  125.  
  126.     }
  127.  
  128.     private void addBottomBranches(LevelAccessor level, BlockPos pos, RandomSource random, DreamTreeConfiguration config, Direction direction, int branchLength, boolean hasBottomLeaves) {
  129.  
  130.     }
  131.  
  132.     private void addStraightBranch(LevelAccessor level, BlockPos pos, RandomSource random, DreamTreeConfiguration config, Direction direction, int branchLength, boolean hasBottomLeaves) {
  133.         this.addBranchWithLeaves(level, pos, random, config, direction, branchLength, false, false, hasBottomLeaves);
  134.     }
  135.  
  136.     private void addDiagonalBranch(LevelAccessor level, BlockPos pos, RandomSource random, DreamTreeConfiguration config, Direction direction, int branchLength, boolean hasBottomLeaves) {
  137.         this.addBranchWithLeaves(level, pos, random, config, direction, branchLength, true, true, hasBottomLeaves);
  138.     }
  139.  
  140.     /**
  141.      * places a straight branch, with preset leaves surrounding it.
  142.      *
  143.      * @param branchLength for how long the branch will be. make it longer the further down it is on the tree.
  144.      * @param diagonal determines is the branch will be placed straight or diagonally. *longer straight branches will branch out diagonally very slightly*
  145.      */
  146.     private void addBranchWithLeaves(LevelAccessor level, BlockPos pos, RandomSource random, DreamTreeConfiguration config, Direction direction, int branchLength, boolean diagonal, boolean hasOffsetLeaves, boolean hasBottomLeaves) {
  147.         int verticalOffset = 0;
  148.         for (int i = 0; i < branchLength; i++) {
  149.             BlockPos branchPos = diagonal ? pos.relative(direction, i).relative(direction.getClockWise(), i).below(verticalOffset) : pos.relative(direction, i).below(verticalOffset);
  150.             this.placeLog(level, branchPos, random, config, direction.getAxis());
  151.             this.placeSurroundingLeaves(level, branchPos, random, direction, config, hasOffsetLeaves, hasBottomLeaves);
  152.  
  153.             verticalOffset = i > 0 ? 1 : 0;
  154.         }
  155.     }
  156.  
  157.     private void placeLog(LevelAccessor level, BlockPos pos, RandomSource random, DreamTreeConfiguration config, Direction.Axis axis) {
  158.         level.setBlock(pos, config.trunkProvider.getState(random, pos).setValue(RotatedPillarBlock.AXIS, axis), 2);
  159.     }
  160.  
  161.     private void placeSurroundingLeaves(LevelAccessor level, BlockPos pos, RandomSource random, Direction direction, DreamTreeConfiguration config, boolean hasOffsetLeaves, boolean hasBottomLeaves) {
  162.         this.placeLeaves(level, pos.above(), random, config);
  163.         this.placeLeaves(level, pos.north(), random, config);
  164.         this.placeLeaves(level, pos.east(), random, config);
  165.         this.placeLeaves(level, pos.south(), random, config);
  166.         this.placeLeaves(level, pos.west(), random, config);
  167.  
  168.         if (hasBottomLeaves) {
  169.             this.placeLeaves(level, pos.below(), random, config);
  170.         }
  171.  
  172.         if (hasOffsetLeaves) {
  173.             this.placeLeaves(level, pos.offset(direction.getNormal().relative(direction.getClockWise())), random, config);
  174.         }
  175.     }
  176.  
  177.     private void placeLeaves(LevelAccessor level, BlockPos pos, RandomSource random, DreamTreeConfiguration config) {
  178.         if (isReplaceable(level, pos)) {
  179.             level.setBlock(pos, config.foliageProvider.getState(random, pos).setValue(DreamLeavesBlock.DISTANCE, 1), 2);
  180.         }
  181.     }
  182.  
  183.     private static boolean isReplaceable(LevelSimulatedReader level, BlockPos pos) {
  184.         return level.isStateAtPosition(pos, EvergreenTreeFeature::isReplaceableBlock);
  185.     }
  186.  
  187.     private static boolean isReplaceableBlock(BlockState state) {
  188.         return state.is(BlockTags.REPLACEABLE_BY_TREES) || state.canBeReplaced();
  189.     }
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement