177 lines
7.9 KiB
Java
177 lines
7.9 KiB
Java
package net.minecraft.world.level.block;
|
|
|
|
import net.minecraft.world.level.block.state.AbstractStateHolder;
|
|
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
|
import net.minecraft.world.level.block.state.StateDefinition;
|
|
import java.util.Random;
|
|
import net.minecraft.core.particles.ParticleOptions;
|
|
import net.minecraft.core.particles.DustParticleOptions;
|
|
import net.minecraft.sounds.SoundSource;
|
|
import net.minecraft.sounds.SoundEvents;
|
|
import net.minecraft.world.level.LevelAccessor;
|
|
import net.minecraft.world.InteractionResult;
|
|
import net.minecraft.world.phys.BlockHitResult;
|
|
import net.minecraft.world.InteractionHand;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.phys.shapes.CollisionContext;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.block.state.properties.AttachFace;
|
|
import net.minecraft.world.level.block.state.properties.Property;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
|
import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
|
|
|
public class LeverBlock extends FaceAttachedHorizontalDirectionalBlock {
|
|
public static final BooleanProperty POWERED;
|
|
protected static final VoxelShape NORTH_AABB;
|
|
protected static final VoxelShape SOUTH_AABB;
|
|
protected static final VoxelShape WEST_AABB;
|
|
protected static final VoxelShape EAST_AABB;
|
|
protected static final VoxelShape UP_AABB_Z;
|
|
protected static final VoxelShape UP_AABB_X;
|
|
protected static final VoxelShape DOWN_AABB_Z;
|
|
protected static final VoxelShape DOWN_AABB_X;
|
|
|
|
protected LeverBlock(final Properties c) {
|
|
super(c);
|
|
this.registerDefaultState(((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)LeverBlock.FACING, Direction.NORTH)).setValue((Property<Comparable>)LeverBlock.POWERED, false)).<AttachFace, AttachFace>setValue(LeverBlock.FACE, AttachFace.WALL));
|
|
}
|
|
|
|
@Override
|
|
public VoxelShape getShape(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final CollisionContext cvn) {
|
|
switch (byg.<AttachFace>getValue(LeverBlock.FACE)) {
|
|
case FLOOR: {
|
|
switch (byg.<Direction>getValue((Property<Direction>)LeverBlock.FACING).getAxis()) {
|
|
case X: {
|
|
return LeverBlock.UP_AABB_X;
|
|
}
|
|
default: {
|
|
return LeverBlock.UP_AABB_Z;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case WALL: {
|
|
switch (byg.<Direction>getValue((Property<Direction>)LeverBlock.FACING)) {
|
|
case EAST: {
|
|
return LeverBlock.EAST_AABB;
|
|
}
|
|
case WEST: {
|
|
return LeverBlock.WEST_AABB;
|
|
}
|
|
case SOUTH: {
|
|
return LeverBlock.SOUTH_AABB;
|
|
}
|
|
default: {
|
|
return LeverBlock.NORTH_AABB;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
default: {
|
|
switch (byg.<Direction>getValue((Property<Direction>)LeverBlock.FACING).getAxis()) {
|
|
case X: {
|
|
return LeverBlock.DOWN_AABB_X;
|
|
}
|
|
default: {
|
|
return LeverBlock.DOWN_AABB_Z;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public InteractionResult use(final BlockState byg, final Level bjt, final BlockPos fk, final Player ayg, final InteractionHand ajh, final BlockHitResult cvd) {
|
|
if (bjt.isClientSide) {
|
|
final BlockState byg2 = ((AbstractStateHolder<O, BlockState>)byg).<Comparable>cycle((Property<Comparable>)LeverBlock.POWERED);
|
|
if (byg2.<Boolean>getValue((Property<Boolean>)LeverBlock.POWERED)) {
|
|
makeParticle(byg2, bjt, fk, 1.0f);
|
|
}
|
|
return InteractionResult.SUCCESS;
|
|
}
|
|
final BlockState byg2 = this.pull(byg, bjt, fk);
|
|
final float float9 = byg2.<Boolean>getValue((Property<Boolean>)LeverBlock.POWERED) ? 0.6f : 0.5f;
|
|
bjt.playSound(null, fk, SoundEvents.LEVER_CLICK, SoundSource.BLOCKS, 0.3f, float9);
|
|
return InteractionResult.SUCCESS;
|
|
}
|
|
|
|
public BlockState pull(BlockState byg, final Level bjt, final BlockPos fk) {
|
|
byg = ((AbstractStateHolder<O, BlockState>)byg).<Comparable>cycle((Property<Comparable>)LeverBlock.POWERED);
|
|
bjt.setBlock(fk, byg, 3);
|
|
this.updateNeighbours(byg, bjt, fk);
|
|
return byg;
|
|
}
|
|
|
|
private static void makeParticle(final BlockState byg, final LevelAccessor bju, final BlockPos fk, final float float4) {
|
|
final Direction fp5 = byg.<Direction>getValue((Property<Direction>)LeverBlock.FACING).getOpposite();
|
|
final Direction fp6 = FaceAttachedHorizontalDirectionalBlock.getConnectedDirection(byg).getOpposite();
|
|
final double double7 = fk.getX() + 0.5 + 0.1 * fp5.getStepX() + 0.2 * fp6.getStepX();
|
|
final double double8 = fk.getY() + 0.5 + 0.1 * fp5.getStepY() + 0.2 * fp6.getStepY();
|
|
final double double9 = fk.getZ() + 0.5 + 0.1 * fp5.getStepZ() + 0.2 * fp6.getStepZ();
|
|
bju.addParticle(new DustParticleOptions(1.0f, 0.0f, 0.0f, float4), double7, double8, double9, 0.0, 0.0, 0.0);
|
|
}
|
|
|
|
@Override
|
|
public void animateTick(final BlockState byg, final Level bjt, final BlockPos fk, final Random random) {
|
|
if (byg.<Boolean>getValue((Property<Boolean>)LeverBlock.POWERED) && random.nextFloat() < 0.25f) {
|
|
makeParticle(byg, bjt, fk, 0.5f);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onRemove(final BlockState byg1, final Level bjt, final BlockPos fk, final BlockState byg4, final boolean boolean5) {
|
|
if (boolean5 || byg1.getBlock() == byg4.getBlock()) {
|
|
return;
|
|
}
|
|
if (byg1.<Boolean>getValue((Property<Boolean>)LeverBlock.POWERED)) {
|
|
this.updateNeighbours(byg1, bjt, fk);
|
|
}
|
|
super.onRemove(byg1, bjt, fk, byg4, boolean5);
|
|
}
|
|
|
|
@Override
|
|
public int getSignal(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final Direction fp) {
|
|
return byg.<Boolean>getValue((Property<Boolean>)LeverBlock.POWERED) ? 15 : 0;
|
|
}
|
|
|
|
@Override
|
|
public int getDirectSignal(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final Direction fp) {
|
|
if (byg.<Boolean>getValue((Property<Boolean>)LeverBlock.POWERED) && FaceAttachedHorizontalDirectionalBlock.getConnectedDirection(byg) == fp) {
|
|
return 15;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public boolean isSignalSource(final BlockState byg) {
|
|
return true;
|
|
}
|
|
|
|
private void updateNeighbours(final BlockState byg, final Level bjt, final BlockPos fk) {
|
|
bjt.updateNeighborsAt(fk, this);
|
|
bjt.updateNeighborsAt(fk.relative(FaceAttachedHorizontalDirectionalBlock.getConnectedDirection(byg).getOpposite()), this);
|
|
}
|
|
|
|
@Override
|
|
protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
|
|
a.add(LeverBlock.FACE, LeverBlock.FACING, LeverBlock.POWERED);
|
|
}
|
|
|
|
static {
|
|
POWERED = BlockStateProperties.POWERED;
|
|
NORTH_AABB = Block.box(5.0, 4.0, 10.0, 11.0, 12.0, 16.0);
|
|
SOUTH_AABB = Block.box(5.0, 4.0, 0.0, 11.0, 12.0, 6.0);
|
|
WEST_AABB = Block.box(10.0, 4.0, 5.0, 16.0, 12.0, 11.0);
|
|
EAST_AABB = Block.box(0.0, 4.0, 5.0, 6.0, 12.0, 11.0);
|
|
UP_AABB_Z = Block.box(5.0, 0.0, 4.0, 11.0, 6.0, 12.0);
|
|
UP_AABB_X = Block.box(4.0, 0.0, 5.0, 12.0, 6.0, 11.0);
|
|
DOWN_AABB_Z = Block.box(5.0, 10.0, 4.0, 11.0, 16.0, 12.0);
|
|
DOWN_AABB_X = Block.box(4.0, 10.0, 5.0, 12.0, 16.0, 11.0);
|
|
}
|
|
}
|