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
Raw Normal View History

2020-07-29 01:46:04 +00:00
package com.thebrokenrail.energonrelics.block.lightning;
import com.thebrokenrail.energonrelics.EnergonRelics;
2020-08-04 17:06:11 +00:00
import com.thebrokenrail.energonrelics.api.block.SimpleBlock;
2020-07-29 01:46:04 +00:00
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;
2020-07-30 22:57:20 +00:00
import net.minecraft.item.Item;
2020-07-29 01:46:04 +00:00
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() {
2020-08-20 23:03:47 +00:00
super(FabricBlockSettings.copy(EnergonRelics.Blocks.LIGHTNING_ROD_BASE).dropsNothing().emissiveLighting((state, world, pos) -> true).lightLevel(state -> 10));
2020-07-29 01:46:04 +00:00
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) {
2020-08-20 23:03:47 +00:00
if (newState.getBlock() != EnergonRelics.Blocks.LIGHTNING_ROD || newState.get(HALF) != DoubleBlockHalf.UPPER) {
2020-07-29 01:46:04 +00:00
valid = false;
}
} else if (direction == Direction.DOWN) {
2020-08-20 23:03:47 +00:00
if (newState.getBlock() != EnergonRelics.Blocks.LIGHTNING_ROD_BASE) {
2020-07-29 01:46:04 +00:00
valid = false;
}
}
} else if (state.get(HALF) == DoubleBlockHalf.UPPER && direction == Direction.DOWN) {
2020-08-20 23:03:47 +00:00
if (newState.getBlock() != EnergonRelics.Blocks.LIGHTNING_ROD || newState.get(HALF) != DoubleBlockHalf.LOWER) {
2020-07-29 01:46:04 +00:00
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);
2020-07-29 21:24:03 +00:00
BlockState targetState = world.getBlockState(targetPos);
2020-08-20 23:03:47 +00:00
if (targetState.getBlock() == EnergonRelics.Blocks.LIGHTNING_ROD_BASE) {
2020-07-29 21:24:03 +00:00
world.breakBlock(targetPos, false, player);
}
2020-07-29 01:46:04 +00:00
}
super.onBreak(world, pos, state, player);
}
2020-07-30 22:57:20 +00:00
@Override
public Item asItem() {
2020-08-20 23:03:47 +00:00
return EnergonRelics.Blocks.LIGHTNING_ROD_BASE.asItem();
2020-07-30 22:57:20 +00:00
}
2020-07-29 01:46:04 +00:00
}