New-Soviet-Era/src/main/java/su/a71/new_soviet/blocks/SwitchBlock.java

86 lines
4 KiB
Java

package su.a71.new_soviet.blocks;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.*;
import net.minecraft.block.enums.WallMountLocation;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.random.Random;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.world.event.GameEvent;
import su.a71.new_soviet.registration.NSE_Sounds;
import java.util.Objects;
public class SwitchBlock extends LeverBlock {
public static final BooleanProperty POWERED = Properties.POWERED;
protected static final VoxelShape NORTH_WALL_SHAPE = Block.createCuboidShape(5.5, 5.5, 15, 10.5, 10.5, 16);
protected static final VoxelShape SOUTH_WALL_SHAPE = Block.createCuboidShape(5.5, 5.5, 0, 10.5, 10.5, 1);
protected static final VoxelShape WEST_WALL_SHAPE = Block.createCuboidShape(15, 5.5, 5.5, 16, 10.5, 10.5);
protected static final VoxelShape EAST_WALL_SHAPE = Block.createCuboidShape(0.0, 5.5, 5.5, 1, 10.5, 10.5);
protected static final VoxelShape FLOOR_Z_AXIS_SHAPE = Block.createCuboidShape(5.5, 0.0, 5.5, 10.5, 1, 10.5);
protected static final VoxelShape FLOOR_X_AXIS_SHAPE = Block.createCuboidShape(5.5, 0.0, 5.5, 10.5, 1, 10.5);
protected static final VoxelShape CEILING_Z_AXIS_SHAPE = Block.createCuboidShape(5.5, 15.0, 5.5, 10.5, 16.0, 10.5);
protected static final VoxelShape CEILING_X_AXIS_SHAPE = Block.createCuboidShape(5.5, 15.0, 5.5, 10.5, 16.0, 10.5);
public SwitchBlock(FabricBlockSettings fabricBlockSettings) {
super(fabricBlockSettings.noCollision());
this.setDefaultState(this.stateManager.getDefaultState().with(FACING, Direction.NORTH).with(POWERED, false).with(FACE, WallMountLocation.WALL));
}
@Override
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
switch (state.get(FACE)) {
case FLOOR: {
if (Objects.requireNonNull(state.get(FACING).getAxis()) == Direction.Axis.X) {
return FLOOR_X_AXIS_SHAPE;
}
return FLOOR_Z_AXIS_SHAPE;
}
case WALL: {
return switch (state.get(FACING)) {
case EAST -> EAST_WALL_SHAPE;
case WEST -> WEST_WALL_SHAPE;
case SOUTH -> SOUTH_WALL_SHAPE;
default -> NORTH_WALL_SHAPE;
};
}
}
if (Objects.requireNonNull(state.get(FACING).getAxis()) == Direction.Axis.X) {
return CEILING_X_AXIS_SHAPE;
}
return CEILING_Z_AXIS_SHAPE;
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (world.isClient) {
state.cycle(POWERED);
return ActionResult.SUCCESS;
}
BlockState blockState = this.togglePower(state, world, pos);
float f = blockState.get(POWERED) ? 1f : 0.9f;
world.playSound(null, pos, NSE_Sounds.SWITCH_PRESS, SoundCategory.BLOCKS, 0.6f, f);
world.emitGameEvent(player, blockState.get(POWERED) ? GameEvent.BLOCK_ACTIVATE : GameEvent.BLOCK_DEACTIVATE, pos);
return ActionResult.CONSUME;
}
// Remove those annoying redstone particles
public void randomDisplayTick(BlockState state, World world, BlockPos pos, Random random) {}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(FACE, FACING, POWERED);
}
}