minecraft-src/net/minecraft/world/level/block/LightBlock.java
2025-07-04 03:15:13 +03:00

115 lines
4.2 KiB
Java

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<LightBlock> 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<BlockState> LIGHT_EMISSION = blockState -> (Integer)blockState.getValue(LEVEL);
@Override
public MapCodec<LightBlock> codec() {
return CODEC;
}
public LightBlock(BlockBehaviour.Properties properties) {
super(properties);
this.registerDefaultState(this.stateDefinition.any().setValue(LEVEL, 15).setValue(WATERLOGGED, false));
}
@Override
protected void createBlockStateDefinition(Builder<Block, BlockState> builder) {
builder.add(LEVEL, WATERLOGGED);
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, 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(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return context.isHoldingItem(Items.LIGHT) ? Shapes.block() : Shapes.empty();
}
@Override
protected boolean propagatesSkylightDown(BlockState state) {
return state.getFluidState().isEmpty();
}
@Override
protected RenderShape getRenderShape(BlockState state) {
return RenderShape.INVISIBLE;
}
@Override
protected float getShadeBrightness(BlockState state, BlockGetter level, BlockPos pos) {
return 1.0F;
}
@Override
protected BlockState updateShape(
BlockState state,
LevelReader level,
ScheduledTickAccess scheduledTickAccess,
BlockPos pos,
Direction direction,
BlockPos neighborPos,
BlockState neighborState,
RandomSource random
) {
if ((Boolean)state.getValue(WATERLOGGED)) {
scheduledTickAccess.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
}
return super.updateShape(state, level, scheduledTickAccess, pos, direction, neighborPos, neighborState, random);
}
@Override
protected FluidState getFluidState(BlockState state) {
return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
}
@Override
protected ItemStack getCloneItemStack(LevelReader level, BlockPos pos, BlockState state, boolean includeData) {
return setLightOnStack(super.getCloneItemStack(level, pos, state, includeData), (Integer)state.getValue(LEVEL));
}
public static ItemStack setLightOnStack(ItemStack stack, int light) {
stack.set(DataComponents.BLOCK_STATE, BlockItemStateProperties.EMPTY.with(LEVEL, light));
return stack;
}
}