minecraft-source/src/net/minecraft/world/level/block/TripWireHookBlock.java

259 lines
12 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 net.minecraft.world.entity.player.Player;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import com.google.common.base.MoreObjects;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import javax.annotation.Nullable;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
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.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;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
public class TripWireHookBlock extends Block {
public static final DirectionProperty FACING;
public static final BooleanProperty POWERED;
public static final BooleanProperty ATTACHED;
protected static final VoxelShape NORTH_AABB;
protected static final VoxelShape SOUTH_AABB;
protected static final VoxelShape WEST_AABB;
protected static final VoxelShape EAST_AABB;
public TripWireHookBlock(final Properties c) {
super(c);
this.registerDefaultState(((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)TripWireHookBlock.FACING, Direction.NORTH)).setValue((Property<Comparable>)TripWireHookBlock.POWERED, false)).<Comparable, Boolean>setValue((Property<Comparable>)TripWireHookBlock.ATTACHED, false));
}
@Override
public VoxelShape getShape(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final CollisionContext cvn) {
switch (byg.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING)) {
default: {
return TripWireHookBlock.EAST_AABB;
}
case WEST: {
return TripWireHookBlock.WEST_AABB;
}
case SOUTH: {
return TripWireHookBlock.SOUTH_AABB;
}
case NORTH: {
return TripWireHookBlock.NORTH_AABB;
}
}
}
@Override
public boolean canSurvive(final BlockState byg, final LevelReader bjw, final BlockPos fk) {
final Direction fp5 = byg.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING);
final BlockPos fk2 = fk.relative(fp5.getOpposite());
final BlockState byg2 = bjw.getBlockState(fk2);
return fp5.getAxis().isHorizontal() && byg2.isFaceSturdy(bjw, fk2, fp5) && !byg2.isSignalSource();
}
@Override
public BlockState updateShape(final BlockState byg1, final Direction fp, final BlockState byg3, final LevelAccessor bju, final BlockPos fk5, final BlockPos fk6) {
if (fp.getOpposite() == byg1.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING) && !byg1.canSurvive(bju, fk5)) {
return Blocks.AIR.defaultBlockState();
}
return super.updateShape(byg1, fp, byg3, bju, fk5, fk6);
}
@Nullable
@Override
public BlockState getStateForPlacement(final BlockPlaceContext bcn) {
BlockState byg3 = (((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)TripWireHookBlock.POWERED, false)).<Comparable, Boolean>setValue((Property<Comparable>)TripWireHookBlock.ATTACHED, false);
final LevelReader bjw4 = bcn.getLevel();
final BlockPos fk5 = bcn.getClickedPos();
final Direction[] nearestLookingDirections;
final Direction[] arr6 = nearestLookingDirections = bcn.getNearestLookingDirections();
for (final Direction fp10 : nearestLookingDirections) {
if (fp10.getAxis().isHorizontal()) {
final Direction fp11 = fp10.getOpposite();
byg3 = ((AbstractStateHolder<O, BlockState>)byg3).<Comparable, Direction>setValue((Property<Comparable>)TripWireHookBlock.FACING, fp11);
if (byg3.canSurvive(bjw4, fk5)) {
return byg3;
}
}
}
return null;
}
@Override
public void setPlacedBy(final Level bjt, final BlockPos fk, final BlockState byg, final LivingEntity akw, final ItemStack bek) {
this.calculateState(bjt, fk, byg, false, false, -1, null);
}
public void calculateState(final Level bjt, final BlockPos fk, final BlockState byg3, final boolean boolean4, final boolean boolean5, final int integer, @Nullable final BlockState byg7) {
final Direction fp9 = byg3.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING);
final boolean boolean6 = byg3.<Boolean>getValue((Property<Boolean>)TripWireHookBlock.ATTACHED);
final boolean boolean7 = byg3.<Boolean>getValue((Property<Boolean>)TripWireHookBlock.POWERED);
boolean boolean8 = !boolean4;
boolean boolean9 = false;
int integer2 = 0;
final BlockState[] arr15 = new BlockState[42];
int integer3 = 1;
while (integer3 < 42) {
final BlockPos fk2 = fk.relative(fp9, integer3);
BlockState byg8 = bjt.getBlockState(fk2);
if (byg8.getBlock() == Blocks.TRIPWIRE_HOOK) {
if (byg8.<Comparable>getValue((Property<Comparable>)TripWireHookBlock.FACING) == fp9.getOpposite()) {
integer2 = integer3;
break;
}
break;
}
else {
if (byg8.getBlock() == Blocks.TRIPWIRE || integer3 == integer) {
if (integer3 == integer) {
byg8 = MoreObjects.<BlockState>firstNonNull(byg7, byg8);
}
final boolean boolean10 = !byg8.<Boolean>getValue((Property<Boolean>)TripWireBlock.DISARMED);
final boolean boolean11 = byg8.<Boolean>getValue((Property<Boolean>)TripWireBlock.POWERED);
boolean9 |= (boolean10 && boolean11);
arr15[integer3] = byg8;
if (integer3 == integer) {
bjt.getBlockTicks().scheduleTick(fk, this, this.getTickDelay(bjt));
boolean8 &= boolean10;
}
}
else {
arr15[integer3] = null;
boolean8 = false;
}
++integer3;
}
}
boolean8 &= (integer2 > 1);
boolean9 &= boolean8;
final BlockState byg9 = (((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)TripWireHookBlock.ATTACHED, boolean8)).<Comparable, Boolean>setValue((Property<Comparable>)TripWireHookBlock.POWERED, boolean9);
if (integer2 > 0) {
final BlockPos fk2 = fk.relative(fp9, integer2);
final Direction fp10 = fp9.getOpposite();
bjt.setBlock(fk2, ((AbstractStateHolder<O, BlockState>)byg9).<Comparable, Direction>setValue((Property<Comparable>)TripWireHookBlock.FACING, fp10), 3);
this.notifyNeighbors(bjt, fk2, fp10);
this.playSound(bjt, fk2, boolean8, boolean9, boolean6, boolean7);
}
this.playSound(bjt, fk, boolean8, boolean9, boolean6, boolean7);
if (!boolean4) {
bjt.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byg9).<Comparable, Direction>setValue((Property<Comparable>)TripWireHookBlock.FACING, fp9), 3);
if (boolean5) {
this.notifyNeighbors(bjt, fk, fp9);
}
}
if (boolean6 != boolean8) {
for (int integer4 = 1; integer4 < integer2; ++integer4) {
final BlockPos fk3 = fk.relative(fp9, integer4);
final BlockState byg10 = arr15[integer4];
if (byg10 != null) {
bjt.setBlock(fk3, ((AbstractStateHolder<O, BlockState>)byg10).<Comparable, Boolean>setValue((Property<Comparable>)TripWireHookBlock.ATTACHED, boolean8), 3);
if (!bjt.getBlockState(fk3).isAir()) {}
}
}
}
}
@Override
public void tick(final BlockState byg, final ServerLevel xd, final BlockPos fk, final Random random) {
this.calculateState(xd, fk, byg, false, true, -1, null);
}
private void playSound(final Level bjt, final BlockPos fk, final boolean boolean3, final boolean boolean4, final boolean boolean5, final boolean boolean6) {
if (boolean4 && !boolean6) {
bjt.playSound(null, fk, SoundEvents.TRIPWIRE_CLICK_ON, SoundSource.BLOCKS, 0.4f, 0.6f);
}
else if (!boolean4 && boolean6) {
bjt.playSound(null, fk, SoundEvents.TRIPWIRE_CLICK_OFF, SoundSource.BLOCKS, 0.4f, 0.5f);
}
else if (boolean3 && !boolean5) {
bjt.playSound(null, fk, SoundEvents.TRIPWIRE_ATTACH, SoundSource.BLOCKS, 0.4f, 0.7f);
}
else if (!boolean3 && boolean5) {
bjt.playSound(null, fk, SoundEvents.TRIPWIRE_DETACH, SoundSource.BLOCKS, 0.4f, 1.2f / (bjt.random.nextFloat() * 0.2f + 0.9f));
}
}
private void notifyNeighbors(final Level bjt, final BlockPos fk, final Direction fp) {
bjt.updateNeighborsAt(fk, this);
bjt.updateNeighborsAt(fk.relative(fp.getOpposite()), this);
}
@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;
}
final boolean boolean6 = byg1.<Boolean>getValue((Property<Boolean>)TripWireHookBlock.ATTACHED);
final boolean boolean7 = byg1.<Boolean>getValue((Property<Boolean>)TripWireHookBlock.POWERED);
if (boolean6 || boolean7) {
this.calculateState(bjt, fk, byg1, true, false, -1, null);
}
if (boolean7) {
bjt.updateNeighborsAt(fk, this);
bjt.updateNeighborsAt(fk.relative(byg1.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING).getOpposite()), this);
}
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>)TripWireHookBlock.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>)TripWireHookBlock.POWERED)) {
return 0;
}
if (byg.<Comparable>getValue((Property<Comparable>)TripWireHookBlock.FACING) == fp) {
return 15;
}
return 0;
}
@Override
public boolean isSignalSource(final BlockState byg) {
return true;
}
@Override
public BlockState rotate(final BlockState byg, final Rotation btr) {
return ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Direction>setValue((Property<Comparable>)TripWireHookBlock.FACING, btr.rotate(byg.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING)));
}
@Override
public BlockState mirror(final BlockState byg, final Mirror bsr) {
return byg.rotate(bsr.getRotation(byg.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING)));
}
@Override
protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
a.add(TripWireHookBlock.FACING, TripWireHookBlock.POWERED, TripWireHookBlock.ATTACHED);
}
static {
FACING = HorizontalDirectionalBlock.FACING;
POWERED = BlockStateProperties.POWERED;
ATTACHED = BlockStateProperties.ATTACHED;
NORTH_AABB = Block.box(5.0, 0.0, 10.0, 11.0, 10.0, 16.0);
SOUTH_AABB = Block.box(5.0, 0.0, 0.0, 11.0, 10.0, 6.0);
WEST_AABB = Block.box(10.0, 0.0, 5.0, 16.0, 10.0, 11.0);
EAST_AABB = Block.box(0.0, 0.0, 5.0, 6.0, 10.0, 11.0);
}
}