| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.util.Mth;
|
| 5 | import net.minecraft.world.InteractionResult;
|
| 6 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 7 | import net.minecraft.world.entity.player.Player;
|
| 8 | import net.minecraft.world.level.BlockGetter;
|
| 9 | import net.minecraft.world.level.Level;
|
| 10 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 11 | import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
| 12 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 13 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 14 | import net.minecraft.world.level.block.entity.DaylightDetectorBlockEntity;
|
| 15 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 18 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 19 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 20 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 21 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 22 | import net.minecraft.world.phys.BlockHitResult;
|
| 23 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 24 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | public class DaylightDetectorBlock extends BaseEntityBlock {
|
| 28 | public static final IntegerProperty POWER = BlockStateProperties.POWER;
|
| 29 | public static final BooleanProperty INVERTED = BlockStateProperties.INVERTED;
|
| 30 | private static final VoxelShape SHAPE = Block.column(16.0, 0.0, 6.0);
|
| 31 |
|
| 32 | public DaylightDetectorBlock(final BlockBehaviour.Properties properties) {
|
| 33 | super(properties);
|
| 34 | this.registerDefaultState(this.stateDefinition.any().setValue(POWER, 0).setValue(INVERTED, false));
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 39 | return SHAPE;
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | protected boolean useShapeForLightOcclusion(final BlockState state) {
|
| 44 | return true;
|
| 45 | }
|
| 46 |
|
| 47 | private static void updateSignalStrength(final BlockState state, final Level level, final BlockPos pos) {
|
| 48 | int target = level.getEffectiveSkyBrightness(pos);
|
| 49 | float sunAngle = level.environmentAttributes().getValue(EnvironmentAttributes.SUN_ANGLE, pos) * (float) (Math.PI / 180.0);
|
| 50 | boolean isInverted = state.getValue(INVERTED);
|
| 51 | if (isInverted) {
|
| 52 | target = 15 - target;
|
| 53 | } else if (target > 0) {
|
| 54 | float offset = sunAngle < (float) Math.PI ? 0.0F : (float) (Math.PI * 2);
|
| 55 | sunAngle += (offset - sunAngle) * 0.2F;
|
| 56 | target = Math.round(target * Mth.cos(sunAngle));
|
| 57 | }
|
| 58 |
|
| 59 | target = Mth.clamp(target, 0, 15);
|
| 60 | if (state.getValue(POWER) != target) {
|
| 61 | level.setBlockAndUpdate(pos, state.setValue(POWER, target));
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 67 | if (!player.mayBuild()) {
|
| 68 | return super.useWithoutItem(state, level, pos, player, hitResult);
|
| 69 | }
|
| 70 |
|
| 71 | if (!level.isClientSide()) {
|
| 72 | BlockState newState = state.cycle(INVERTED);
|
| 73 | level.setBlock(pos, newState, 2);
|
| 74 | level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(player, newState));
|
| 75 | updateSignalStrength(newState, level, pos);
|
| 76 | }
|
| 77 |
|
| 78 | return InteractionResult.SUCCESS;
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | protected boolean isSignalSource(final BlockState state) {
|
| 83 | return true;
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | protected int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 88 | return state.getValue(POWER);
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 93 | return new DaylightDetectorBlockEntity(worldPosition, blockState);
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
|
| 98 | return !level.isClientSide() && level.dimensionType().hasSkyLight()
|
| 99 | ? createTickerHelper(type, BlockEntityTypes.DAYLIGHT_DETECTOR, DaylightDetectorBlock::tickEntity)
|
| 100 | : null;
|
| 101 | }
|
| 102 |
|
| 103 | private static void tickEntity(final Level level, final BlockPos blockPos, final BlockState blockState, final DaylightDetectorBlockEntity blockEntity) {
|
| 104 | if (level.getGameTime() % 20L == 0L) {
|
| 105 | updateSignalStrength(blockState, level, blockPos);
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 111 | builder.add(POWER, INVERTED);
|
| 112 | }
|
| 113 | }
|
| 114 |
|