BiomesOPlenty/src/main/java/biomesoplenty/common/block/SaplingBlockBOP.java

116 lines
3.9 KiB
Java
Raw Normal View History

2019-05-21 08:42:25 +00:00
/*******************************************************************************
* Copyright 2014-2019, the Biomes O' Plenty Team
*
* This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License.
*
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
******************************************************************************/
package biomesoplenty.common.block;
import biomesoplenty.api.block.BOPBlocks;
2019-06-13 05:08:11 +00:00
import net.minecraft.block.*;
import net.minecraft.block.trees.Tree;
2019-05-21 08:42:25 +00:00
import net.minecraft.state.IntegerProperty;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.util.math.BlockPos;
2019-06-13 05:08:11 +00:00
import net.minecraft.util.math.shapes.ISelectionContext;
2019-05-21 08:42:25 +00:00
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
2019-06-13 05:08:11 +00:00
import net.minecraft.world.IWorldReader;
2019-05-21 08:42:25 +00:00
import net.minecraft.world.World;
2019-12-26 05:34:54 +00:00
import net.minecraft.world.server.ServerWorld;
2019-05-21 08:42:25 +00:00
2019-06-13 05:08:11 +00:00
import java.util.Random;
public class SaplingBlockBOP extends SaplingBlock implements IGrowable
2019-05-21 08:42:25 +00:00
{
2019-12-31 07:27:53 +00:00
public static final IntegerProperty STAGE = BlockStateProperties.STAGE;
public static final VoxelShape SHAPE = Block.box(2.0D, 0.0D, 2.0D, 14.0D, 12.0D, 14.0D);
2019-06-13 05:08:11 +00:00
private final Tree tree;
2019-05-21 08:42:25 +00:00
public SaplingBlockBOP(Tree tree, Block.Properties properties)
2019-05-21 08:42:25 +00:00
{
super(tree, properties);
this.tree = tree;
2019-12-31 07:27:53 +00:00
this.registerDefaultState(this.stateDefinition.any().setValue(STAGE, Integer.valueOf(0)));
2019-05-21 08:42:25 +00:00
}
@Override
2019-06-13 05:08:11 +00:00
public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext selectionContext)
2019-05-21 08:42:25 +00:00
{
return SHAPE;
}
@Override
2019-12-26 05:34:54 +00:00
public void tick(BlockState state, ServerWorld world, BlockPos pos, Random random)
2019-05-21 08:42:25 +00:00
{
2019-12-26 05:34:54 +00:00
super.tick(state, world, pos, random);
if (!world.isAreaLoaded(pos, 1)) return; // Forge: prevent loading unloaded chunks when checking neighbor's light
2019-12-31 07:27:53 +00:00
if (world.getMaxLocalRawBrightness(pos.above()) >= 9 && random.nextInt(7) == 0) {
this.performBonemeal(world, random, pos, state);
2019-05-21 08:42:25 +00:00
}
}
@Override
2019-12-31 07:27:53 +00:00
public void performBonemeal(ServerWorld world, Random rand, BlockPos pos, BlockState state)
2019-05-21 08:42:25 +00:00
{
2019-12-31 07:27:53 +00:00
if (state.getValue(STAGE) == 0)
2019-05-21 08:42:25 +00:00
{
2019-12-31 07:27:53 +00:00
world.setBlock(pos, state.cycle(STAGE), 4);
2019-05-21 08:42:25 +00:00
}
else
{
2019-12-26 05:34:54 +00:00
if (!net.minecraftforge.event.ForgeEventFactory.saplingGrowTree(world, rand, pos)) return;
2019-12-31 07:27:53 +00:00
this.tree.growTree(world, world.getChunkSource().getGenerator(), pos, state, rand);
2019-05-21 08:42:25 +00:00
}
}
/**
* Whether this IGrowable can grow
*/
@Override
2019-12-31 07:27:53 +00:00
public boolean isValidBonemealTarget(IBlockReader worldIn, BlockPos pos, BlockState state, boolean isClient)
2019-05-21 08:42:25 +00:00
{
return true;
}
@Override
2019-12-31 07:27:53 +00:00
public boolean isBonemealSuccess(World worldIn, Random rand, BlockPos pos, BlockState state)
2019-05-21 08:42:25 +00:00
{
2019-12-31 07:27:53 +00:00
return (double)worldIn.random.nextFloat() < 0.45D;
2019-05-21 08:42:25 +00:00
}
@Override
2019-12-31 07:27:53 +00:00
public void advanceTree(ServerWorld world, BlockPos pos, BlockState state, Random rand)
2019-05-21 08:42:25 +00:00
{
2019-12-31 07:27:53 +00:00
this.performBonemeal(world, rand, pos, state);
2019-05-21 08:42:25 +00:00
}
@Override
2019-12-31 07:27:53 +00:00
public boolean canSurvive(BlockState state, IWorldReader worldIn, BlockPos pos)
{
2019-12-31 07:27:53 +00:00
Block ground = worldIn.getBlockState(pos.below()).getBlock();
if (this == BOPBlocks.palm_sapling)
{
return ground == BOPBlocks.white_sand || ground == BOPBlocks.orange_sand || ground == BOPBlocks.black_sand || ground == Blocks.RED_SAND || ground == Blocks.SAND || super.canSurvive(state, worldIn, pos);
}
if (this == BOPBlocks.hellbark_sapling)
{
2019-12-31 07:27:53 +00:00
return ground == Blocks.NETHERRACK || super.canSurvive(state, worldIn, pos);
}
2019-12-31 07:27:53 +00:00
return super.canSurvive(state, worldIn, pos);
}
2019-05-21 08:42:25 +00:00
@Override
2019-12-31 07:27:53 +00:00
public void createBlockStateDefinition(StateContainer.Builder<Block, BlockState> builder)
2019-05-21 08:42:25 +00:00
{
builder.add(STAGE);
}
}