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

90 lines
3.3 KiB
Java
Raw Normal View History

2018-10-13 23:46:30 +02:00
package de.ellpeck.naturesaura.blocks;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.data.BlockStateGenerator;
import de.ellpeck.naturesaura.data.ItemModelGenerator;
2020-02-25 15:14:56 +01:00
import de.ellpeck.naturesaura.gen.ModFeatures;
2020-01-24 17:05:41 +01:00
import de.ellpeck.naturesaura.gen.WorldGenAncientTree;
2020-01-29 01:34:58 +01:00
import de.ellpeck.naturesaura.reg.*;
2019-11-04 19:08:49 +01:00
import net.minecraft.block.*;
import net.minecraft.block.material.Material;
2020-01-29 01:34:58 +01:00
import net.minecraft.client.renderer.RenderType;
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-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;
2020-01-29 01:34:58 +01:00
import java.util.function.Supplier;
2018-10-13 23:46:30 +02:00
2020-01-29 01:34:58 +01:00
public class BlockAncientSapling extends BushBlock implements IGrowable, IModItem, ICustomBlockState, ICustomItemModel, ICustomRenderType {
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-02-25 15:14:56 +01:00
ModFeatures.ANCIENT_TREE.place(world, world.getChunkProvider().getChunkGenerator(), rand, pos, WorldGenAncientTree.CONFIG);
2018-10-13 23:46:30 +02:00
}
}
2020-01-29 00:40:28 +01:00
@Override
public void generateCustomBlockState(BlockStateGenerator generator) {
generator.simpleBlock(this, generator.models().cross(this.getBaseName(), generator.modLoc("block/" + this.getBaseName())));
}
@Override
public void generateCustomItemModel(ItemModelGenerator generator) {
generator.withExistingParent(this.getBaseName(), "item/generated").texture("layer0", "block/" + this.getBaseName());
}
2020-01-29 01:34:58 +01:00
@Override
public Supplier<RenderType> getRenderType() {
return RenderType::cutoutMipped;
}
2018-10-13 23:46:30 +02:00
}