137 lines
5.7 KiB
Java
137 lines
5.7 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* com.mojang.serialization.MapCodec
|
|
*/
|
|
package net.minecraft.world.level.block;
|
|
|
|
import com.mojang.serialization.MapCodec;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.util.RandomSource;
|
|
import net.minecraft.world.item.context.BlockPlaceContext;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.LevelReader;
|
|
import net.minecraft.world.level.ScheduledTickAccess;
|
|
import net.minecraft.world.level.block.Block;
|
|
import net.minecraft.world.level.block.DirectionalBlock;
|
|
import net.minecraft.world.level.block.Mirror;
|
|
import net.minecraft.world.level.block.Rotation;
|
|
import net.minecraft.world.level.block.state.BlockBehaviour;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.level.block.state.StateDefinition;
|
|
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
|
import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
|
import net.minecraft.world.level.redstone.ExperimentalRedstoneUtils;
|
|
import net.minecraft.world.level.redstone.Orientation;
|
|
import net.minecraft.world.ticks.LevelTicks;
|
|
|
|
public class ObserverBlock
|
|
extends DirectionalBlock {
|
|
public static final MapCodec<ObserverBlock> CODEC = ObserverBlock.simpleCodec(ObserverBlock::new);
|
|
public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
|
|
|
|
public MapCodec<ObserverBlock> codec() {
|
|
return CODEC;
|
|
}
|
|
|
|
public ObserverBlock(BlockBehaviour.Properties properties) {
|
|
super(properties);
|
|
this.registerDefaultState((BlockState)((BlockState)((BlockState)this.stateDefinition.any()).setValue(FACING, Direction.SOUTH)).setValue(POWERED, false));
|
|
}
|
|
|
|
@Override
|
|
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
|
|
builder.add(FACING, POWERED);
|
|
}
|
|
|
|
@Override
|
|
protected BlockState rotate(BlockState state, Rotation rotation) {
|
|
return (BlockState)state.setValue(FACING, rotation.rotate((Direction)state.getValue(FACING)));
|
|
}
|
|
|
|
@Override
|
|
protected BlockState mirror(BlockState state, Mirror mirror) {
|
|
return state.rotate(mirror.getRotation((Direction)state.getValue(FACING)));
|
|
}
|
|
|
|
@Override
|
|
protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
|
|
if (state.getValue(POWERED).booleanValue()) {
|
|
level.setBlock(pos, (BlockState)state.setValue(POWERED, false), 2);
|
|
} else {
|
|
level.setBlock(pos, (BlockState)state.setValue(POWERED, true), 2);
|
|
level.scheduleTick(pos, this, 2);
|
|
}
|
|
this.updateNeighborsInFront(level, pos, state);
|
|
}
|
|
|
|
@Override
|
|
protected BlockState updateShape(BlockState state, LevelReader level, ScheduledTickAccess ticks, BlockPos pos, Direction directionToNeighbour, BlockPos neighbourPos, BlockState neighbourState, RandomSource random) {
|
|
if (state.getValue(FACING) == directionToNeighbour && !state.getValue(POWERED).booleanValue()) {
|
|
this.startSignal(level, ticks, pos);
|
|
}
|
|
return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
|
}
|
|
|
|
private void startSignal(LevelReader level, ScheduledTickAccess ticks, BlockPos pos) {
|
|
if (!level.isClientSide() && !ticks.getBlockTicks().hasScheduledTick(pos, this)) {
|
|
ticks.scheduleTick(pos, this, 2);
|
|
}
|
|
}
|
|
|
|
protected void updateNeighborsInFront(Level level, BlockPos pos, BlockState state) {
|
|
Direction direction = (Direction)state.getValue(FACING);
|
|
BlockPos oppositePos = pos.relative(direction.getOpposite());
|
|
Orientation orientation = ExperimentalRedstoneUtils.initialOrientation(level, direction.getOpposite(), null);
|
|
level.neighborChanged(oppositePos, this, orientation);
|
|
level.updateNeighborsAtExceptFromFacing(oppositePos, this, direction, orientation);
|
|
}
|
|
|
|
@Override
|
|
protected boolean isSignalSource(BlockState state) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
protected int getDirectSignal(BlockState state, BlockGetter level, BlockPos pos, Direction direction) {
|
|
return state.getSignal(level, pos, direction);
|
|
}
|
|
|
|
@Override
|
|
protected int getSignal(BlockState state, BlockGetter level, BlockPos pos, Direction direction) {
|
|
if (state.getValue(POWERED).booleanValue() && state.getValue(FACING) == direction) {
|
|
return 15;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
protected void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean movedByPiston) {
|
|
if (state.is(oldState.getBlock())) {
|
|
return;
|
|
}
|
|
if (!level.isClientSide() && state.getValue(POWERED).booleanValue() && !level.getBlockTicks().hasScheduledTick(pos, this)) {
|
|
BlockState newState = (BlockState)state.setValue(POWERED, false);
|
|
level.setBlock(pos, newState, 18);
|
|
this.updateNeighborsInFront(level, pos, newState);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void affectNeighborsAfterRemoval(BlockState state, ServerLevel level, BlockPos pos, boolean movedByPiston) {
|
|
if (state.getValue(POWERED).booleanValue() && ((LevelTicks)level.getBlockTicks()).hasScheduledTick(pos, this)) {
|
|
this.updateNeighborsInFront(level, pos, (BlockState)state.setValue(POWERED, false));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public BlockState getStateForPlacement(BlockPlaceContext context) {
|
|
return (BlockState)this.defaultBlockState().setValue(FACING, context.getNearestLookingDirection().getOpposite().getOpposite());
|
|
}
|
|
}
|
|
|