package com.thebrokenrail.energonrelics.block.lightning; import com.thebrokenrail.energonrelics.EnergonRelics; import com.thebrokenrail.energonrelics.block.entity.LightningRodBaseBlockEntity; import com.thebrokenrail.energonrelics.api.block.energy.EnergyBlock; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.block.Material; import net.minecraft.block.MaterialColor; import net.minecraft.block.ShapeContext; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityType; import net.minecraft.block.enums.DoubleBlockHalf; import net.minecraft.block.piston.PistonBehavior; import net.minecraft.entity.LivingEntity; import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemStack; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.util.shape.VoxelShape; import net.minecraft.util.shape.VoxelShapes; import net.minecraft.world.BlockView; import net.minecraft.world.World; import net.minecraft.world.WorldAccess; import java.util.function.Function; @SuppressWarnings("deprecation") public class LightningRodBaseBlock extends EnergyBlock { public LightningRodBaseBlock() { super(FabricBlockSettings.of(Material.STONE, MaterialColor.CLEAR).nonOpaque().allowsSpawning((state, world, pos, type) -> false).solidBlock((state, world, pos) -> false).suffocates((state, world, pos) -> false).requiresTool().strength(1.5f, 6.0f).blockVision((state, world, pos) -> false)); } @Override @Environment(EnvType.CLIENT) public float getAmbientOcclusionLightLevel(BlockState state, BlockView world, BlockPos pos) { return 1f; } @Override public boolean isTranslucent(BlockState state, BlockView world, BlockPos pos) { return true; } @Override public VoxelShape getVisualShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) { return VoxelShapes.empty(); } @Override public PistonBehavior getPistonBehavior(BlockState state) { return PistonBehavior.DESTROY; } @Override protected Function, BlockEntity> getFactory() { return LightningRodBaseBlockEntity::new; } private static final VoxelShape SHAPE = VoxelShapes.union(createCuboidShape(3d, 0d, 3d, 13d, 4d, 13d), createCuboidShape(4d, 4d, 4d, 12d, 8d, 12d), createCuboidShape(5d, 8d, 5d, 11d, 12d, 11d), createCuboidShape(6d, 12d, 6d, 10d, 16d, 10d)); @Override public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) { return SHAPE; } @Override public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState newState, WorldAccess world, BlockPos pos, BlockPos posFrom) { boolean valid = true; if (direction == Direction.UP && (newState.getBlock() != EnergonRelics.Blocks.LIGHTNING_ROD || newState.get(LightningRodBlock.HALF) != DoubleBlockHalf.LOWER)) { valid = false; } if (valid) { return super.getStateForNeighborUpdate(state, direction, newState, world, pos, posFrom); } else { return Blocks.AIR.getDefaultState(); } } @Override public void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) { super.onPlaced(world, pos, state, placer, itemStack); world.setBlockState(pos.up(2), EnergonRelics.Blocks.LIGHTNING_ROD.getDefaultState().with(LightningRodBlock.HALF, DoubleBlockHalf.UPPER)); world.setBlockState(pos.up(1), EnergonRelics.Blocks.LIGHTNING_ROD.getDefaultState().with(LightningRodBlock.HALF, DoubleBlockHalf.LOWER)); } @Override public BlockState getPlacementState(ItemPlacementContext ctx) { BlockPos pos = ctx.getBlockPos(); World world = ctx.getWorld(); BlockPos top = pos.up(2); return top.getY() < world.getHeight() && world.getBlockState(pos.up(1)).canReplace(ctx) && world.getBlockState(top).canReplace(ctx) ? super.getPlacementState(ctx) : null; } }