Advertisement
jayhillx

tropicaltree

Sep 3rd, 2024
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. package com.mysticsbiomes.common.world.feature.trunk;
  2.  
  3. import com.mojang.serialization.Codec;
  4. import com.mojang.serialization.codecs.RecordCodecBuilder;
  5. import com.mysticsbiomes.init.MysticFeatures;
  6. import net.minecraft.core.BlockPos;
  7. import net.minecraft.core.Direction;
  8. import net.minecraft.util.RandomSource;
  9. import net.minecraft.world.level.LevelSimulatedReader;
  10. import net.minecraft.world.level.block.RotatedPillarBlock;
  11. import net.minecraft.world.level.block.state.BlockBehaviour;
  12. import net.minecraft.world.level.block.state.BlockState;
  13. import net.minecraft.world.level.levelgen.feature.configurations.TreeConfiguration;
  14. import net.minecraft.world.level.levelgen.feature.foliageplacers.FoliagePlacer;
  15. import net.minecraft.world.level.levelgen.feature.trunkplacers.TrunkPlacer;
  16. import net.minecraft.world.level.levelgen.feature.trunkplacers.TrunkPlacerType;
  17.  
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.function.BiConsumer;
  21.  
  22. public class TropicalTrunkPlacer extends TrunkPlacer {
  23.     public static final Codec<TropicalTrunkPlacer> CODEC = RecordCodecBuilder.create((instance) -> trunkPlacerParts(instance).apply(instance, TropicalTrunkPlacer::new));
  24.  
  25.     public TropicalTrunkPlacer(int height, int heightRandomA, int randomHeightB) {
  26.         super(height, heightRandomA, randomHeightB);
  27.     }
  28.  
  29.     @Override
  30.     protected TrunkPlacerType<?> type() {
  31.         return MysticFeatures.TROPICAL_TRUNK_PLACER.get();
  32.     }
  33.  
  34.     @Override
  35.     public List<FoliagePlacer.FoliageAttachment> placeTrunk(LevelSimulatedReader level, BiConsumer<BlockPos, BlockState> consumer, RandomSource random, int trunkHeight, BlockPos originalPos, TreeConfiguration config) {
  36.         setDirtAt(level, consumer, random, originalPos.below(), config);
  37.  
  38.         List<FoliagePlacer.FoliageAttachment> attachments = new ArrayList<>();
  39.         for (int h = 0; h < trunkHeight; ++h) {
  40.             this.placeLog(level, consumer, random, originalPos.above(h), config); // generate main trunk.
  41.         }
  42.  
  43.         attachments.add(new FoliagePlacer.FoliageAttachment(originalPos.above(trunkHeight + 1), 0, false));
  44.  
  45.         // the height a branch can generate at on the main trunk.
  46.         int highestBranchHeightLimit = trunkHeight - 4;
  47.         int lowerBranchHeightLimit = highestBranchHeightLimit - (3 - random.nextInt(1));
  48.  
  49.         Direction randomDirection = Direction.Plane.HORIZONTAL.getRandomDirection(random);
  50.         Direction secondRandomDirection = Direction.Plane.HORIZONTAL.getRandomDirection(random);
  51.         BlockPos branchPos = originalPos.above(highestBranchHeightLimit).relative(randomDirection);
  52.         BlockPos lowBranchPos = originalPos.above(lowerBranchHeightLimit).relative(secondRandomDirection);
  53.  
  54.         if (level.isStateAtPosition(branchPos, BlockBehaviour.BlockStateBase::isAir)) {
  55.             attachments.add(this.generateBranch(level, consumer, random, branchPos, randomDirection, config));
  56.  
  57.             if (trunkHeight > 10) { // can generate 2 branches on a tree 7 blocks or taller.
  58.                 if (secondRandomDirection != randomDirection) {
  59.                     attachments.add(this.generateBranch(level, consumer, random, lowBranchPos, secondRandomDirection, config));
  60.                 }
  61.             }
  62.         }
  63.         return attachments;
  64.     }
  65.  
  66.     public FoliagePlacer.FoliageAttachment generateBranch(LevelSimulatedReader level, BiConsumer<BlockPos, BlockState> consumer, RandomSource random, BlockPos pos, Direction direction, TreeConfiguration config) {
  67.         BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos();
  68.         int currentX = pos.getX();
  69.         int currentZ = pos.getZ();
  70.  
  71.         this.placeLog(level, consumer, random, pos, config, state -> state.setValue(RotatedPillarBlock.AXIS, direction.getAxis()));
  72.         for (int height = 1; height < 3 + random.nextInt(1); ++height) {
  73.             currentX += direction.getStepX();
  74.             currentZ += direction.getStepZ();
  75.  
  76.             this.placeLog(level, consumer, random, mutablePos.set(currentX, pos.getY() + height, currentZ), config, state -> state.setValue(RotatedPillarBlock.AXIS, direction.getAxis()));
  77.         }
  78.         return new FoliagePlacer.FoliageAttachment(mutablePos.above(), 0, false);
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement