package net.minecraft.world.level.block; import com.mojang.serialization.MapCodec; import java.util.function.ToIntFunction; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.component.DataComponents; import net.minecraft.util.RandomSource; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.item.component.BlockItemStateProperties; 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.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.BlockStateProperties; import net.minecraft.world.level.block.state.properties.BooleanProperty; import net.minecraft.world.level.block.state.properties.IntegerProperty; import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.Fluids; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; public class LightBlock extends Block implements SimpleWaterloggedBlock { public static final MapCodec CODEC = simpleCodec(LightBlock::new); public static final int MAX_LEVEL = 15; public static final IntegerProperty LEVEL = BlockStateProperties.LEVEL; public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED; public static final ToIntFunction LIGHT_EMISSION = state -> (Integer)state.getValue(LEVEL); @Override public MapCodec codec() { return CODEC; } public LightBlock(final BlockBehaviour.Properties properties) { super(properties); this.registerDefaultState(this.stateDefinition.any().setValue(LEVEL, 15).setValue(WATERLOGGED, false)); } @Override protected void createBlockStateDefinition(final Builder builder) { builder.add(LEVEL, WATERLOGGED); } @Override protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) { if (!level.isClientSide() && player.canUseGameMasterBlocks()) { level.setBlock(pos, state.cycle(LEVEL), 2); return InteractionResult.SUCCESS_SERVER; } else { return InteractionResult.CONSUME; } } @Override protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) { return context.isHoldingItem(Items.LIGHT) ? Shapes.block() : Shapes.empty(); } @Override protected boolean propagatesSkylightDown(final BlockState state) { return state.getFluidState().isEmpty(); } @Override protected RenderShape getRenderShape(final BlockState state) { return RenderShape.INVISIBLE; } @Override protected float getShadeBrightness(final BlockState state, final BlockGetter level, final BlockPos pos) { return 1.0F; } @Override protected BlockState updateShape( final BlockState state, final LevelReader level, final ScheduledTickAccess ticks, final BlockPos pos, final Direction direction, final BlockPos neighbourPos, final BlockState neighbour, final RandomSource random ) { if ((Boolean)state.getValue(WATERLOGGED)) { ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level)); } return super.updateShape(state, level, ticks, pos, direction, neighbourPos, neighbour, random); } @Override protected FluidState getFluidState(final BlockState state) { return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state); } @Override protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) { return setLightOnStack(super.getCloneItemStack(level, pos, state, includeData), (Integer)state.getValue(LEVEL)); } public static ItemStack setLightOnStack(final ItemStack result, final int lightLevel) { result.set(DataComponents.BLOCK_STATE, BlockItemStateProperties.EMPTY.with(LEVEL, lightLevel)); return result; } }