This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/block/lightning/LightningRodBlock.java

136 lines
4.9 KiB
Java

package com.thebrokenrail.energonrelics.block.lightning;
import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.api.block.SimpleBlock;
import com.thebrokenrail.energonrelics.util.MissingCaseException;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.ShapeContext;
import net.minecraft.block.enums.DoubleBlockHalf;
import net.minecraft.block.piston.PistonBehavior;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.EnumProperty;
import net.minecraft.state.property.Properties;
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;
@SuppressWarnings("deprecation")
public class LightningRodBlock extends SimpleBlock {
public static final EnumProperty<DoubleBlockHalf> HALF = Properties.DOUBLE_BLOCK_HALF;
public LightningRodBlock() {
super(FabricBlockSettings.copy(EnergonRelics.LIGHTNING_ROD_BASE_BLOCK).dropsNothing().emissiveLighting((state, world, pos) -> true).lightLevel(state -> 10));
setDefaultState(getDefaultState().with(HALF, DoubleBlockHalf.LOWER));
}
@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 void appendProperties(StateManager.Builder<Block, BlockState> builder) {
super.appendProperties(builder);
builder.add(HALF);
}
@Override
protected boolean registerItem() {
return false;
}
private static final VoxelShape LOWER_SHAPE = createCuboidShape(7.5d, 0d, 7.5d, 8.5d, 16d, 8.5d);
private static final VoxelShape UPPER_SHAPE = createCuboidShape(7.5d, 0d, 7.5d, 8.5d, 8d, 8.5d);
@Override
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
DoubleBlockHalf half = state.get(HALF);
switch (half) {
case LOWER: {
return LOWER_SHAPE;
}
case UPPER: {
return UPPER_SHAPE;
}
default: {
throw new MissingCaseException(half);
}
}
}
@Override
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState newState, WorldAccess world, BlockPos pos, BlockPos posFrom) {
boolean valid = true;
if (state.get(HALF) == DoubleBlockHalf.LOWER) {
if (direction == Direction.UP) {
if (newState.getBlock() != EnergonRelics.LIGHTNING_ROD_BLOCK || newState.get(HALF) != DoubleBlockHalf.UPPER) {
valid = false;
}
} else if (direction == Direction.DOWN) {
if (newState.getBlock() != EnergonRelics.LIGHTNING_ROD_BASE_BLOCK) {
valid = false;
}
}
} else if (state.get(HALF) == DoubleBlockHalf.UPPER && direction == Direction.DOWN) {
if (newState.getBlock() != EnergonRelics.LIGHTNING_ROD_BLOCK || newState.get(HALF) != DoubleBlockHalf.LOWER) {
valid = false;
}
}
if (valid) {
return super.getStateForNeighborUpdate(state, direction, newState, world, pos, posFrom);
} else {
return Blocks.AIR.getDefaultState();
}
}
@Override
public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
if (!world.isClient() && player.isCreative()) {
int distance = state.get(HALF) == DoubleBlockHalf.LOWER ? 1 : 2;
BlockPos targetPos = pos.down(distance);
BlockState targetState = world.getBlockState(targetPos);
if (targetState.getBlock() == EnergonRelics.LIGHTNING_ROD_BASE_BLOCK) {
world.breakBlock(targetPos, false, player);
}
}
super.onBreak(world, pos, state, player);
}
@Override
public Item asItem() {
return EnergonRelics.LIGHTNING_ROD_BASE_BLOCK.asItem();
}
}