Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mysticsbiomes.common.world.feature.trunk;
- import com.mojang.serialization.Codec;
- import com.mojang.serialization.codecs.RecordCodecBuilder;
- import com.mysticsbiomes.init.MysticFeatures;
- import net.minecraft.core.BlockPos;
- import net.minecraft.core.Direction;
- import net.minecraft.util.RandomSource;
- import net.minecraft.world.level.LevelSimulatedReader;
- import net.minecraft.world.level.block.RotatedPillarBlock;
- import net.minecraft.world.level.block.state.BlockBehaviour;
- import net.minecraft.world.level.block.state.BlockState;
- import net.minecraft.world.level.levelgen.feature.configurations.TreeConfiguration;
- import net.minecraft.world.level.levelgen.feature.foliageplacers.FoliagePlacer;
- import net.minecraft.world.level.levelgen.feature.trunkplacers.TrunkPlacer;
- import net.minecraft.world.level.levelgen.feature.trunkplacers.TrunkPlacerType;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.function.BiConsumer;
- public class TropicalTrunkPlacer extends TrunkPlacer {
- public static final Codec<TropicalTrunkPlacer> CODEC = RecordCodecBuilder.create((instance) -> trunkPlacerParts(instance).apply(instance, TropicalTrunkPlacer::new));
- public TropicalTrunkPlacer(int height, int heightRandomA, int randomHeightB) {
- super(height, heightRandomA, randomHeightB);
- }
- @Override
- protected TrunkPlacerType<?> type() {
- return MysticFeatures.TROPICAL_TRUNK_PLACER.get();
- }
- @Override
- public List<FoliagePlacer.FoliageAttachment> placeTrunk(LevelSimulatedReader level, BiConsumer<BlockPos, BlockState> consumer, RandomSource random, int trunkHeight, BlockPos originalPos, TreeConfiguration config) {
- setDirtAt(level, consumer, random, originalPos.below(), config);
- List<FoliagePlacer.FoliageAttachment> attachments = new ArrayList<>();
- for (int h = 0; h < trunkHeight; ++h) {
- this.placeLog(level, consumer, random, originalPos.above(h), config); // generate main trunk.
- }
- attachments.add(new FoliagePlacer.FoliageAttachment(originalPos.above(trunkHeight + 1), 0, false));
- // the height a branch can generate at on the main trunk.
- int highestBranchHeightLimit = trunkHeight - 4;
- int lowerBranchHeightLimit = highestBranchHeightLimit - (3 - random.nextInt(1));
- Direction randomDirection = Direction.Plane.HORIZONTAL.getRandomDirection(random);
- Direction secondRandomDirection = Direction.Plane.HORIZONTAL.getRandomDirection(random);
- BlockPos branchPos = originalPos.above(highestBranchHeightLimit).relative(randomDirection);
- BlockPos lowBranchPos = originalPos.above(lowerBranchHeightLimit).relative(secondRandomDirection);
- if (level.isStateAtPosition(branchPos, BlockBehaviour.BlockStateBase::isAir)) {
- attachments.add(this.generateBranch(level, consumer, random, branchPos, randomDirection, config));
- if (trunkHeight > 10) { // can generate 2 branches on a tree 7 blocks or taller.
- if (secondRandomDirection != randomDirection) {
- attachments.add(this.generateBranch(level, consumer, random, lowBranchPos, secondRandomDirection, config));
- }
- }
- }
- return attachments;
- }
- public FoliagePlacer.FoliageAttachment generateBranch(LevelSimulatedReader level, BiConsumer<BlockPos, BlockState> consumer, RandomSource random, BlockPos pos, Direction direction, TreeConfiguration config) {
- BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos();
- int currentX = pos.getX();
- int currentZ = pos.getZ();
- this.placeLog(level, consumer, random, pos, config, state -> state.setValue(RotatedPillarBlock.AXIS, direction.getAxis()));
- for (int height = 1; height < 3 + random.nextInt(1); ++height) {
- currentX += direction.getStepX();
- currentZ += direction.getStepZ();
- this.placeLog(level, consumer, random, mutablePos.set(currentX, pos.getY() + height, currentZ), config, state -> state.setValue(RotatedPillarBlock.AXIS, direction.getAxis()));
- }
- return new FoliagePlacer.FoliageAttachment(mutablePos.above(), 0, false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement