123 lines
5.8 KiB
Java
123 lines
5.8 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.item.BlockPlaceContext;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.LevelAccessor;
|
|
import net.minecraft.world.level.Level;
|
|
import java.util.Random;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.world.level.block.state.StateDefinition;
|
|
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.level.block.state.properties.BooleanProperty;
|
|
|
|
public class ObserverBlock extends DirectionalBlock {
|
|
public static final BooleanProperty POWERED;
|
|
|
|
public ObserverBlock(final Properties c) {
|
|
super(c);
|
|
this.registerDefaultState((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)ObserverBlock.FACING, Direction.SOUTH)).<Comparable, Boolean>setValue((Property<Comparable>)ObserverBlock.POWERED, false));
|
|
}
|
|
|
|
@Override
|
|
protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
|
|
a.add(ObserverBlock.FACING, ObserverBlock.POWERED);
|
|
}
|
|
|
|
@Override
|
|
public BlockState rotate(final BlockState byg, final Rotation btr) {
|
|
return ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Direction>setValue((Property<Comparable>)ObserverBlock.FACING, btr.rotate(byg.<Direction>getValue((Property<Direction>)ObserverBlock.FACING)));
|
|
}
|
|
|
|
@Override
|
|
public BlockState mirror(final BlockState byg, final Mirror bsr) {
|
|
return byg.rotate(bsr.getRotation(byg.<Direction>getValue((Property<Direction>)ObserverBlock.FACING)));
|
|
}
|
|
|
|
@Override
|
|
public void tick(final BlockState byg, final ServerLevel xd, final BlockPos fk, final Random random) {
|
|
if (byg.<Boolean>getValue((Property<Boolean>)ObserverBlock.POWERED)) {
|
|
xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Boolean>setValue((Property<Comparable>)ObserverBlock.POWERED, false), 2);
|
|
}
|
|
else {
|
|
xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Boolean>setValue((Property<Comparable>)ObserverBlock.POWERED, true), 2);
|
|
xd.getBlockTicks().scheduleTick(fk, this, 2);
|
|
}
|
|
this.updateNeighborsInFront(xd, fk, byg);
|
|
}
|
|
|
|
@Override
|
|
public BlockState updateShape(final BlockState byg1, final Direction fp, final BlockState byg3, final LevelAccessor bju, final BlockPos fk5, final BlockPos fk6) {
|
|
if (byg1.<Comparable>getValue((Property<Comparable>)ObserverBlock.FACING) == fp && !byg1.<Boolean>getValue((Property<Boolean>)ObserverBlock.POWERED)) {
|
|
this.startSignal(bju, fk5);
|
|
}
|
|
return super.updateShape(byg1, fp, byg3, bju, fk5, fk6);
|
|
}
|
|
|
|
private void startSignal(final LevelAccessor bju, final BlockPos fk) {
|
|
if (!bju.isClientSide() && !bju.getBlockTicks().hasScheduledTick(fk, this)) {
|
|
bju.getBlockTicks().scheduleTick(fk, this, 2);
|
|
}
|
|
}
|
|
|
|
protected void updateNeighborsInFront(final Level bjt, final BlockPos fk, final BlockState byg) {
|
|
final Direction fp5 = byg.<Direction>getValue((Property<Direction>)ObserverBlock.FACING);
|
|
final BlockPos fk2 = fk.relative(fp5.getOpposite());
|
|
bjt.neighborChanged(fk2, this, fk);
|
|
bjt.updateNeighborsAtExceptFromFacing(fk2, this, fp5);
|
|
}
|
|
|
|
@Override
|
|
public boolean isSignalSource(final BlockState byg) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public int getDirectSignal(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final Direction fp) {
|
|
return byg.getSignal(bjd, fk, fp);
|
|
}
|
|
|
|
@Override
|
|
public int getSignal(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final Direction fp) {
|
|
if (byg.<Boolean>getValue((Property<Boolean>)ObserverBlock.POWERED) && byg.<Comparable>getValue((Property<Comparable>)ObserverBlock.FACING) == fp) {
|
|
return 15;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public void onPlace(final BlockState byg1, final Level bjt, final BlockPos fk, final BlockState byg4, final boolean boolean5) {
|
|
if (byg1.getBlock() == byg4.getBlock()) {
|
|
return;
|
|
}
|
|
if (!bjt.isClientSide() && byg1.<Boolean>getValue((Property<Boolean>)ObserverBlock.POWERED) && !bjt.getBlockTicks().hasScheduledTick(fk, this)) {
|
|
final BlockState byg5 = ((AbstractStateHolder<O, BlockState>)byg1).<Comparable, Boolean>setValue((Property<Comparable>)ObserverBlock.POWERED, false);
|
|
bjt.setBlock(fk, byg5, 18);
|
|
this.updateNeighborsInFront(bjt, fk, byg5);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onRemove(final BlockState byg1, final Level bjt, final BlockPos fk, final BlockState byg4, final boolean boolean5) {
|
|
if (byg1.getBlock() == byg4.getBlock()) {
|
|
return;
|
|
}
|
|
if (!bjt.isClientSide && byg1.<Boolean>getValue((Property<Boolean>)ObserverBlock.POWERED) && bjt.getBlockTicks().hasScheduledTick(fk, this)) {
|
|
this.updateNeighborsInFront(bjt, fk, ((AbstractStateHolder<O, BlockState>)byg1).<Comparable, Boolean>setValue((Property<Comparable>)ObserverBlock.POWERED, false));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public BlockState getStateForPlacement(final BlockPlaceContext bcn) {
|
|
return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)ObserverBlock.FACING, bcn.getNearestLookingDirection().getOpposite().getOpposite());
|
|
}
|
|
|
|
static {
|
|
POWERED = BlockStateProperties.POWERED;
|
|
}
|
|
}
|