Search in sources :

Example 1 with AbstractTreeGrower

use of net.minecraft.world.level.block.grower.AbstractTreeGrower in project Tropicraft by Tropicraft.

the class TropicraftTrees method create.

private static AbstractTreeGrower create(FeatureProvider featureProvider) {
    return new AbstractTreeGrower() {

        @Nullable
        @Override
        protected ConfiguredFeature<TreeConfiguration, ?> getConfiguredFeature(Random random, boolean beehive) {
            return null;
        }

        @Override
        public boolean growTree(ServerLevel world, ChunkGenerator generator, BlockPos pos, BlockState sapling, Random random) {
            ConfiguredFeature<?, ?> feature = featureProvider.getFeature(world.getServer(), random, this.hasFlowers(world, pos));
            if (feature == null) {
                return false;
            }
            world.setBlock(pos, Blocks.AIR.defaultBlockState(), Block.UPDATE_INVISIBLE);
            if (feature.place(world, generator, random, pos)) {
                return true;
            } else {
                world.setBlock(pos, sapling, Block.UPDATE_INVISIBLE);
                return false;
            }
        }

        private boolean hasFlowers(LevelAccessor world, BlockPos origin) {
            BlockPos min = origin.offset(-2, -1, -2);
            BlockPos max = origin.offset(2, 1, 2);
            for (BlockPos pos : BlockPos.MutableBlockPos.betweenClosed(min, max)) {
                if (world.getBlockState(pos).is(BlockTags.FLOWERS)) {
                    return true;
                }
            }
            return false;
        }
    };
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) AbstractTreeGrower(net.minecraft.world.level.block.grower.AbstractTreeGrower) LevelAccessor(net.minecraft.world.level.LevelAccessor) BlockState(net.minecraft.world.level.block.state.BlockState) Random(java.util.Random) BlockPos(net.minecraft.core.BlockPos) ChunkGenerator(net.minecraft.world.level.chunk.ChunkGenerator) TreeConfiguration(net.minecraft.world.level.levelgen.feature.configurations.TreeConfiguration)

Aggregations

Random (java.util.Random)1 BlockPos (net.minecraft.core.BlockPos)1 ServerLevel (net.minecraft.server.level.ServerLevel)1 LevelAccessor (net.minecraft.world.level.LevelAccessor)1 AbstractTreeGrower (net.minecraft.world.level.block.grower.AbstractTreeGrower)1 BlockState (net.minecraft.world.level.block.state.BlockState)1 ChunkGenerator (net.minecraft.world.level.chunk.ChunkGenerator)1 TreeConfiguration (net.minecraft.world.level.levelgen.feature.configurations.TreeConfiguration)1