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;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.gen.LevelGenAncientTree;
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;
2021-12-04 15:40:09 +01:00
import net.minecraft.level.IBlockReader;
import net.minecraft.level.Level;
import net.minecraft.level.server.ServerLevel;
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() {
2020-10-17 21:13:45 +02:00
super(Properties.create(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
2021-12-04 15:40:09 +01:00
public VoxelShape getShape(BlockState state, IBlockReader levelIn, BlockPos pos, ISelectionContext context) {
2020-01-21 21:04:44 +01:00
return SHAPE;
2018-10-13 23:46:30 +02:00
}
@Override
2021-12-04 15:40:09 +01:00
public void randomTick(BlockState state, ServerLevel level, BlockPos pos, Random random) {
if (!level.isClientSide) {
super.randomTick(state, level, pos, random);
2018-10-13 23:46:30 +02:00
2021-12-04 15:40:09 +01:00
if (level.getLight(pos.up()) >= 9 && random.nextInt(7) == 0) {
this.grow(level, 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
2021-12-04 15:40:09 +01:00
public boolean canGrow(IBlockReader levelIn, BlockPos pos, BlockState state, boolean isClient) {
2018-10-13 23:46:30 +02:00
return true;
}
@Override
2021-12-04 15:40:09 +01:00
public boolean canUseBonemeal(Level level, Random rand, BlockPos pos, BlockState state) {
return level.rand.nextFloat() < 0.45F;
2018-10-13 23:46:30 +02:00
}
@Override
2021-12-04 15:40:09 +01:00
public void grow(ServerLevel level, Random rand, BlockPos pos, BlockState state) {
2020-01-21 21:04:44 +01:00
if (state.get(SaplingBlock.STAGE) == 0) {
2021-12-04 15:40:09 +01:00
level.setBlockState(pos, state.func_235896_a_(SaplingBlock.STAGE), 4);
} else if (ForgeEventFactory.saplingGrowTree(level, rand, pos)) {
ModFeatures.ANCIENT_TREE.func_241855_a(level, level.getChunkProvider().getChunkGenerator(), rand, pos, LevelGenAncientTree.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() {
2020-09-22 03:17:02 +02:00
return RenderType::getCutoutMipped;
2020-01-29 01:34:58 +01:00
}
2018-10-13 23:46:30 +02:00
}