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

100 lines
3.8 KiB
Java
Raw Normal View History

2020-02-25 15:14:56 +01:00
package de.ellpeck.naturesaura.blocks;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.blocks.tiles.BlockEntityAuraBloom;
2021-12-19 15:32:45 +01:00
import de.ellpeck.naturesaura.blocks.tiles.ITickableBlockEntity;
import de.ellpeck.naturesaura.blocks.tiles.ModBlockEntities;
2020-02-25 15:14:56 +01:00
import de.ellpeck.naturesaura.data.BlockStateGenerator;
import de.ellpeck.naturesaura.data.ItemModelGenerator;
import de.ellpeck.naturesaura.reg.*;
import net.minecraft.client.renderer.RenderType;
2021-12-05 23:32:31 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.BushBlock;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.entity.BlockEntity;
2021-12-19 15:32:45 +01:00
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
2021-12-05 23:32:31 +01:00
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
2021-12-19 15:32:45 +01:00
import org.jetbrains.annotations.Nullable;
2020-02-25 15:14:56 +01:00
2020-10-19 03:05:13 +02:00
import java.util.Arrays;
2020-02-25 15:14:56 +01:00
import java.util.function.Supplier;
2021-12-05 23:32:31 +01:00
public class BlockAuraBloom extends BushBlock implements IModItem, ICustomBlockState, ICustomItemModel, ICustomRenderType, EntityBlock {
2020-02-25 15:14:56 +01:00
2021-12-05 23:32:31 +01:00
protected static final VoxelShape SHAPE = Block.box(5.0D, 0.0D, 5.0D, 11.0D, 10.0D, 11.0D);
2020-02-25 15:56:46 +01:00
private final String baseName;
2020-10-19 03:05:13 +02:00
private final Block[] allowedGround;
2020-02-25 15:14:56 +01:00
2020-10-19 03:05:13 +02:00
public BlockAuraBloom(String baseName, Block... allowedGround) {
2021-12-05 23:32:31 +01:00
super(Properties.of(Material.PLANT).noCollission().strength(0).sound(SoundType.GRASS));
2020-02-25 15:56:46 +01:00
this.baseName = baseName;
2020-10-19 03:05:13 +02:00
this.allowedGround = allowedGround;
2020-02-25 15:14:56 +01:00
ModRegistry.add(this);
2020-02-25 15:56:46 +01:00
}
@Override
2021-12-05 23:32:31 +01:00
public boolean canSurvive(BlockState state, LevelReader levelIn, BlockPos pos) {
2021-12-15 16:30:22 +01:00
var down = pos.below();
2021-12-05 23:32:31 +01:00
return this.mayPlaceOn(levelIn.getBlockState(down), levelIn, down);
2020-10-19 03:05:13 +02:00
}
@Override
2021-12-05 23:32:31 +01:00
protected boolean mayPlaceOn(BlockState state, BlockGetter levelIn, BlockPos pos) {
return Arrays.stream(this.allowedGround).anyMatch(state::is);
2020-02-25 15:56:46 +01:00
}
@Override
2021-12-05 23:32:31 +01:00
public void entityInside(BlockState state, Level levelIn, BlockPos pos, Entity entityIn) {
2020-02-25 15:56:46 +01:00
if (this == ModBlocks.AURA_CACTUS)
2021-12-05 23:32:31 +01:00
entityIn.hurt(DamageSource.CACTUS, 1);
2020-02-25 15:14:56 +01:00
}
@Override
2021-12-05 23:32:31 +01:00
public VoxelShape getShape(BlockState state, BlockGetter levelIn, BlockPos pos, CollisionContext context) {
2021-12-15 16:30:22 +01:00
var vec3d = state.getOffset(levelIn, pos);
2021-12-05 23:32:31 +01:00
return SHAPE.move(vec3d.x, vec3d.y, vec3d.z);
2020-02-25 15:14:56 +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());
}
@Override
public Supplier<RenderType> getRenderType() {
2021-12-05 23:32:31 +01:00
return RenderType::cutout;
2020-02-25 15:14:56 +01:00
}
@Override
public String getBaseName() {
2020-02-25 15:56:46 +01:00
return this.baseName;
2020-02-25 15:14:56 +01:00
}
2021-12-05 23:32:31 +01:00
@org.jetbrains.annotations.Nullable
2020-02-25 15:14:56 +01:00
@Override
2021-12-05 23:32:31 +01:00
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
2021-12-15 16:24:53 +01:00
return new BlockEntityAuraBloom(pos, state);
2020-02-25 15:14:56 +01:00
}
2021-12-19 15:32:45 +01:00
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
return ITickableBlockEntity.createTickerHelper(type, ModBlockEntities.AURA_BLOOM);
}
2020-02-25 15:14:56 +01:00
}