NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/BlockAncientSapling.java

73 lines
2.6 KiB
Java
Raw Normal View History

2018-10-13 23:46:30 +02:00
package de.ellpeck.naturesaura.blocks;
2020-01-24 17:05:41 +01:00
import de.ellpeck.naturesaura.gen.WorldGenAncientTree;
2018-10-13 23:46:30 +02:00
import de.ellpeck.naturesaura.reg.IModItem;
import de.ellpeck.naturesaura.reg.IModelProvider;
import de.ellpeck.naturesaura.reg.ModRegistry;
2019-11-04 19:08:49 +01:00
import net.minecraft.block.*;
import net.minecraft.block.material.Material;
2020-01-21 21:04:44 +01:00
import net.minecraft.state.StateContainer;
2018-10-13 23:46:30 +02:00
import net.minecraft.util.math.BlockPos;
2020-01-21 21:04:44 +01:00
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.IBlockReader;
2018-10-13 23:46:30 +02:00
import net.minecraft.world.World;
2020-01-24 17:05:41 +01:00
import net.minecraft.world.gen.feature.IFeatureConfig;
2020-01-28 18:08:56 +01:00
import net.minecraft.world.server.ServerWorld;
2020-01-21 21:04:44 +01:00
import net.minecraftforge.event.ForgeEventFactory;
2018-10-13 23:46:30 +02:00
import java.util.Random;
2019-11-04 19:08:49 +01:00
public class BlockAncientSapling extends BushBlock implements IGrowable, IModItem, IModelProvider {
2020-01-21 21:04:44 +01:00
protected static final VoxelShape SHAPE = Block.makeCuboidShape(2.0D, 0.0D, 2.0D, 14.0D, 12.0D, 14.0D);
2018-10-13 23:46:30 +02:00
public BlockAncientSapling() {
2019-11-04 19:08:49 +01:00
super(ModBlocks.prop(Material.PLANTS).hardnessAndResistance(0.0F).sound(SoundType.PLANT));
2018-11-29 17:58:47 +01:00
ModRegistry.add(this);
2018-10-13 23:46:30 +02:00
}
@Override
2020-01-21 21:04:44 +01:00
public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
return SHAPE;
2018-10-13 23:46:30 +02:00
}
@Override
2020-01-28 18:08:56 +01:00
public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
2018-10-13 23:46:30 +02:00
if (!world.isRemote) {
2020-01-21 21:04:44 +01:00
super.randomTick(state, world, pos, random);
2018-10-13 23:46:30 +02:00
2020-01-21 21:04:44 +01:00
if (world.getLight(pos.up()) >= 9 && random.nextInt(7) == 0) {
this.grow(world, random, pos, state);
2018-10-13 23:46:30 +02:00
}
}
}
@Override
public String getBaseName() {
return "ancient_sapling";
}
@Override
2020-01-21 21:04:44 +01:00
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
builder.add(SaplingBlock.STAGE);
2018-10-13 23:46:30 +02:00
}
@Override
2020-01-21 21:04:44 +01:00
public boolean canGrow(IBlockReader worldIn, BlockPos pos, BlockState state, boolean isClient) {
2018-10-13 23:46:30 +02:00
return true;
}
@Override
2019-10-20 22:30:49 +02:00
public boolean canUseBonemeal(World world, Random rand, BlockPos pos, BlockState state) {
2018-10-13 23:46:30 +02:00
return world.rand.nextFloat() < 0.45F;
}
@Override
2020-01-28 18:08:56 +01:00
public void grow(ServerWorld world, Random rand, BlockPos pos, BlockState state) {
2020-01-21 21:04:44 +01:00
if (state.get(SaplingBlock.STAGE) == 0) {
world.setBlockState(pos, state.cycle(SaplingBlock.STAGE), 4);
} else if (ForgeEventFactory.saplingGrowTree(world, rand, pos)) {
2020-01-24 17:05:41 +01:00
new WorldGenAncientTree(true).place(world, world.getChunkProvider().getChunkGenerator(), rand, pos, IFeatureConfig.NO_FEATURE_CONFIG);
2018-10-13 23:46:30 +02:00
}
}
}