61 lines
2 KiB
Java
61 lines
2 KiB
Java
package net.minecraft.world.level.block;
|
|
|
|
import com.mojang.serialization.MapCodec;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.util.RandomSource;
|
|
import net.minecraft.world.item.context.BlockPlaceContext;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.block.state.BlockBehaviour;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.level.block.state.StateDefinition.Builder;
|
|
import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
|
import net.minecraft.world.level.redstone.Orientation;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
public class RedstoneLampBlock extends Block {
|
|
public static final MapCodec<RedstoneLampBlock> CODEC = simpleCodec(RedstoneLampBlock::new);
|
|
public static final BooleanProperty LIT = RedstoneTorchBlock.LIT;
|
|
|
|
@Override
|
|
public MapCodec<RedstoneLampBlock> codec() {
|
|
return CODEC;
|
|
}
|
|
|
|
public RedstoneLampBlock(BlockBehaviour.Properties properties) {
|
|
super(properties);
|
|
this.registerDefaultState(this.defaultBlockState().setValue(LIT, false));
|
|
}
|
|
|
|
@Nullable
|
|
@Override
|
|
public BlockState getStateForPlacement(BlockPlaceContext context) {
|
|
return this.defaultBlockState().setValue(LIT, context.getLevel().hasNeighborSignal(context.getClickedPos()));
|
|
}
|
|
|
|
@Override
|
|
protected void neighborChanged(BlockState state, Level level, BlockPos pos, Block neighborBlock, @Nullable Orientation orientation, boolean movedByPiston) {
|
|
if (!level.isClientSide) {
|
|
boolean bl = (Boolean)state.getValue(LIT);
|
|
if (bl != level.hasNeighborSignal(pos)) {
|
|
if (bl) {
|
|
level.scheduleTick(pos, this, 4);
|
|
} else {
|
|
level.setBlock(pos, state.cycle(LIT), 2);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
|
|
if ((Boolean)state.getValue(LIT) && !level.hasNeighborSignal(pos)) {
|
|
level.setBlock(pos, state.cycle(LIT), 2);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void createBlockStateDefinition(Builder<Block, BlockState> builder) {
|
|
builder.add(LIT);
|
|
}
|
|
}
|