| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.function.ToIntFunction;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.core.component.DataComponents;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.InteractionResult;
|
| 9 | import net.minecraft.world.entity.player.Player;
|
| 10 | import net.minecraft.world.item.ItemStack;
|
| 11 | import net.minecraft.world.item.Items;
|
| 12 | import net.minecraft.world.item.component.BlockItemStateProperties;
|
| 13 | import net.minecraft.world.level.BlockGetter;
|
| 14 | import net.minecraft.world.level.Level;
|
| 15 | import net.minecraft.world.level.LevelReader;
|
| 16 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 17 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 18 | import net.minecraft.world.level.block.state.BlockState;
|
| 19 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 20 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 21 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 22 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 23 | import net.minecraft.world.level.material.FluidState;
|
| 24 | import net.minecraft.world.level.material.Fluids;
|
| 25 | import net.minecraft.world.phys.BlockHitResult;
|
| 26 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 27 | import net.minecraft.world.phys.shapes.Shapes;
|
| 28 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 29 |
|
| 30 | public class LightBlock extends Block implements SimpleWaterloggedBlock {
|
| 31 | public static final int MAX_LEVEL = 15;
|
| 32 | public static final IntegerProperty LEVEL = BlockStateProperties.LEVEL;
|
| 33 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 34 | public static final ToIntFunction<BlockState> LIGHT_EMISSION = state -> state.getValue(LEVEL);
|
| 35 |
|
| 36 | public LightBlock(final BlockBehaviour.Properties properties) {
|
| 37 | super(properties);
|
| 38 | this.registerDefaultState(this.stateDefinition.any().setValue(LEVEL, 15).setValue(WATERLOGGED, false));
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 43 | builder.add(LEVEL, WATERLOGGED);
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 48 | if (!level.isClientSide() && player.canUseGameMasterBlocks()) {
|
| 49 | level.setBlock(pos, state.cycle(LEVEL), 2);
|
| 50 | return InteractionResult.SUCCESS_SERVER;
|
| 51 | } else {
|
| 52 | return InteractionResult.CONSUME;
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 58 | return context.isHoldingItem(Items.LIGHT) ? Shapes.block() : Shapes.empty();
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | protected boolean propagatesSkylightDown(final BlockState state) {
|
| 63 | return state.getFluidState().isEmpty();
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected RenderShape getRenderShape(final BlockState state) {
|
| 68 | return RenderShape.INVISIBLE;
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | protected float getShadeBrightness(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 73 | return 1.0F;
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | protected BlockState updateShape(
|
| 78 | final BlockState state,
|
| 79 | final LevelReader level,
|
| 80 | final ScheduledTickAccess ticks,
|
| 81 | final BlockPos pos,
|
| 82 | final Direction direction,
|
| 83 | final BlockPos neighbourPos,
|
| 84 | final BlockState neighbour,
|
| 85 | final RandomSource random
|
| 86 | ) {
|
| 87 | if (state.getValue(WATERLOGGED)) {
|
| 88 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 89 | }
|
| 90 |
|
| 91 | return super.updateShape(state, level, ticks, pos, direction, neighbourPos, neighbour, random);
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | protected FluidState getFluidState(final BlockState state) {
|
| 96 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) {
|
| 101 | return setLightOnStack(super.getCloneItemStack(level, pos, state, includeData), state.getValue(LEVEL));
|
| 102 | }
|
| 103 |
|
| 104 | public static ItemStack setLightOnStack(final ItemStack result, final int lightLevel) {
|
| 105 | result.set(DataComponents.BLOCK_STATE, BlockItemStateProperties.EMPTY.with(LEVEL, lightLevel));
|
| 106 | return result;
|
| 107 | }
|
| 108 | }
|
| 109 |
|