forked from TheBrokenRail/EnergonRelics
113 lines
4.6 KiB
Java
113 lines
4.6 KiB
Java
package com.thebrokenrail.energonrelics.block.forcefield;
|
|
|
|
import com.thebrokenrail.energonrelics.EnergonRelics;
|
|
import com.thebrokenrail.energonrelics.block.entity.forcefield.ForcefieldProjectorBlockEntity;
|
|
import com.thebrokenrail.energonrelics.block.util.SimpleBlock;
|
|
import com.thebrokenrail.energonrelics.config.HardcodedConfig;
|
|
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.entity.BlockEntity;
|
|
import net.minecraft.block.piston.PistonBehavior;
|
|
import net.minecraft.sound.BlockSoundGroup;
|
|
import net.minecraft.state.StateManager;
|
|
import net.minecraft.state.property.DirectionProperty;
|
|
import net.minecraft.state.property.Properties;
|
|
import net.minecraft.util.BlockMirror;
|
|
import net.minecraft.util.BlockRotation;
|
|
import net.minecraft.util.math.BlockPos;
|
|
import net.minecraft.util.math.Direction;
|
|
import net.minecraft.world.BlockView;
|
|
import net.minecraft.world.WorldAccess;
|
|
|
|
@SuppressWarnings("deprecation")
|
|
public class ForcefieldBlock extends SimpleBlock {
|
|
public static final DirectionProperty FACING = Properties.FACING;
|
|
|
|
public ForcefieldBlock() {
|
|
super(FabricBlockSettings.copy(Blocks.BARRIER).dropsNothing().lightLevel(state -> 4).emissiveLighting((state, world, pos) -> true).nonOpaque().sounds(BlockSoundGroup.GLASS).allowsSpawning((state, world, pos, type) -> false).solidBlock((state, world, pos) -> false).suffocates((state, world, pos) -> false));
|
|
setDefaultState(getDefaultState().with(FACING, Direction.NORTH));
|
|
}
|
|
|
|
@Override
|
|
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
|
|
builder.add(FACING);
|
|
}
|
|
|
|
@Override
|
|
public BlockState rotate(BlockState state, BlockRotation rotation) {
|
|
return state.with(FACING, rotation.rotate(state.get(FACING)));
|
|
}
|
|
|
|
@Override
|
|
public BlockState mirror(BlockState state, BlockMirror mirror) {
|
|
return state.rotate(mirror.getRotation(state.get(FACING)));
|
|
}
|
|
|
|
@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
|
|
protected boolean registerItem() {
|
|
return false;
|
|
}
|
|
|
|
public ForcefieldProjectorBlockEntity findProjector(BlockView world, BlockPos pos, BlockState state) {
|
|
Direction facing = state.get(FACING).getOpposite();
|
|
ForcefieldProjectorBlockEntity found = null;
|
|
for (int i = 0; i < HardcodedConfig.FORCEFIELD_MAX_SIZE + 1; i++) {
|
|
BlockPos targetPos = pos.offset(facing, i);
|
|
BlockState targetState = world.getBlockState(targetPos);
|
|
if (targetState.getBlock() == this) {
|
|
if (!facing.equals(targetState.get(FACING).getOpposite())) {
|
|
break;
|
|
}
|
|
} else if (targetState.getBlock() == EnergonRelics.FORCEFIELD_PROJECTOR_BLOCK) {
|
|
if (facing.equals(targetState.get(FACING).getOpposite()) && targetState.get(ForcefieldProjectorBlock.POWERED)) {
|
|
BlockEntity entity = world.getBlockEntity(targetPos);
|
|
if (entity instanceof ForcefieldProjectorBlockEntity) {
|
|
found = (ForcefieldProjectorBlockEntity) entity;
|
|
}
|
|
}
|
|
break;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
return found;
|
|
}
|
|
|
|
@Override
|
|
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState newState, WorldAccess world, BlockPos pos, BlockPos posFrom) {
|
|
BlockState defaultState = super.getStateForNeighborUpdate(state, direction, newState, world, pos, posFrom);
|
|
Direction facing = state.get(FACING).getOpposite();
|
|
if (direction.equals(facing)) {
|
|
return findProjector(world, pos, state) != null ? defaultState : Blocks.AIR.getDefaultState();
|
|
} else {
|
|
return defaultState;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
@Environment(EnvType.CLIENT)
|
|
public boolean isSideInvisible(BlockState state, BlockState stateFrom, Direction direction) {
|
|
return stateFrom == state || super.isSideInvisible(state, stateFrom, direction);
|
|
}
|
|
|
|
@Override
|
|
public PistonBehavior getPistonBehavior(BlockState state) {
|
|
return PistonBehavior.DESTROY;
|
|
}
|
|
}
|