minecraft-src/net/minecraft/world/level/block/LecternBlock.java
2025-07-04 01:41:11 +03:00

314 lines
11 KiB
Java

package net.minecraft.world.level.block;
import com.mojang.serialization.MapCodec;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.component.DataComponents;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.stats.Stats;
import net.minecraft.tags.ItemTags;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.CustomData;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.LecternBlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
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.DirectionProperty;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.level.pathfinder.PathComputationType;
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;
import org.jetbrains.annotations.Nullable;
public class LecternBlock extends BaseEntityBlock {
public static final MapCodec<LecternBlock> CODEC = simpleCodec(LecternBlock::new);
public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;
public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
public static final BooleanProperty HAS_BOOK = BlockStateProperties.HAS_BOOK;
public static final VoxelShape SHAPE_BASE = Block.box(0.0, 0.0, 0.0, 16.0, 2.0, 16.0);
public static final VoxelShape SHAPE_POST = Block.box(4.0, 2.0, 4.0, 12.0, 14.0, 12.0);
public static final VoxelShape SHAPE_COMMON = Shapes.or(SHAPE_BASE, SHAPE_POST);
public static final VoxelShape SHAPE_TOP_PLATE = Block.box(0.0, 15.0, 0.0, 16.0, 15.0, 16.0);
public static final VoxelShape SHAPE_COLLISION = Shapes.or(SHAPE_COMMON, SHAPE_TOP_PLATE);
public static final VoxelShape SHAPE_WEST = Shapes.or(
Block.box(1.0, 10.0, 0.0, 5.333333, 14.0, 16.0),
Block.box(5.333333, 12.0, 0.0, 9.666667, 16.0, 16.0),
Block.box(9.666667, 14.0, 0.0, 14.0, 18.0, 16.0),
SHAPE_COMMON
);
public static final VoxelShape SHAPE_NORTH = Shapes.or(
Block.box(0.0, 10.0, 1.0, 16.0, 14.0, 5.333333),
Block.box(0.0, 12.0, 5.333333, 16.0, 16.0, 9.666667),
Block.box(0.0, 14.0, 9.666667, 16.0, 18.0, 14.0),
SHAPE_COMMON
);
public static final VoxelShape SHAPE_EAST = Shapes.or(
Block.box(10.666667, 10.0, 0.0, 15.0, 14.0, 16.0),
Block.box(6.333333, 12.0, 0.0, 10.666667, 16.0, 16.0),
Block.box(2.0, 14.0, 0.0, 6.333333, 18.0, 16.0),
SHAPE_COMMON
);
public static final VoxelShape SHAPE_SOUTH = Shapes.or(
Block.box(0.0, 10.0, 10.666667, 16.0, 14.0, 15.0),
Block.box(0.0, 12.0, 6.333333, 16.0, 16.0, 10.666667),
Block.box(0.0, 14.0, 2.0, 16.0, 18.0, 6.333333),
SHAPE_COMMON
);
private static final int PAGE_CHANGE_IMPULSE_TICKS = 2;
@Override
public MapCodec<LecternBlock> codec() {
return CODEC;
}
protected LecternBlock(BlockBehaviour.Properties properties) {
super(properties);
this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(POWERED, false).setValue(HAS_BOOK, false));
}
@Override
protected RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
protected VoxelShape getOcclusionShape(BlockState state, BlockGetter level, BlockPos pos) {
return SHAPE_COMMON;
}
@Override
protected boolean useShapeForLightOcclusion(BlockState state) {
return true;
}
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
Level level = context.getLevel();
ItemStack itemStack = context.getItemInHand();
Player player = context.getPlayer();
boolean bl = false;
if (!level.isClientSide && player != null && player.canUseGameMasterBlocks()) {
CustomData customData = itemStack.getOrDefault(DataComponents.BLOCK_ENTITY_DATA, CustomData.EMPTY);
if (customData.contains("Book")) {
bl = true;
}
}
return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite()).setValue(HAS_BOOK, bl);
}
@Override
protected VoxelShape getCollisionShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return SHAPE_COLLISION;
}
@Override
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
switch ((Direction)state.getValue(FACING)) {
case NORTH:
return SHAPE_NORTH;
case SOUTH:
return SHAPE_SOUTH;
case EAST:
return SHAPE_EAST;
case WEST:
return SHAPE_WEST;
default:
return SHAPE_COMMON;
}
}
@Override
protected BlockState rotate(BlockState state, Rotation rotation) {
return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
}
@Override
protected BlockState mirror(BlockState state, Mirror mirror) {
return state.rotate(mirror.getRotation(state.getValue(FACING)));
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(FACING, POWERED, HAS_BOOK);
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new LecternBlockEntity(pos, state);
}
public static boolean tryPlaceBook(@Nullable LivingEntity entity, Level level, BlockPos pos, BlockState state, ItemStack stack) {
if (!(Boolean)state.getValue(HAS_BOOK)) {
if (!level.isClientSide) {
placeBook(entity, level, pos, state, stack);
}
return true;
} else {
return false;
}
}
private static void placeBook(@Nullable LivingEntity entity, Level level, BlockPos pos, BlockState state, ItemStack stack) {
if (level.getBlockEntity(pos) instanceof LecternBlockEntity lecternBlockEntity) {
lecternBlockEntity.setBook(stack.consumeAndReturn(1, entity));
resetBookState(entity, level, pos, state, true);
level.playSound(null, pos, SoundEvents.BOOK_PUT, SoundSource.BLOCKS, 1.0F, 1.0F);
}
}
public static void resetBookState(@Nullable Entity entity, Level level, BlockPos pos, BlockState state, boolean hasBook) {
BlockState blockState = state.setValue(POWERED, false).setValue(HAS_BOOK, hasBook);
level.setBlock(pos, blockState, 3);
level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(entity, blockState));
updateBelow(level, pos, state);
}
public static void signalPageChange(Level level, BlockPos pos, BlockState state) {
changePowered(level, pos, state, true);
level.scheduleTick(pos, state.getBlock(), 2);
level.levelEvent(1043, pos, 0);
}
private static void changePowered(Level level, BlockPos pos, BlockState state, boolean powered) {
level.setBlock(pos, state.setValue(POWERED, powered), 3);
updateBelow(level, pos, state);
}
private static void updateBelow(Level level, BlockPos pos, BlockState state) {
level.updateNeighborsAt(pos.below(), state.getBlock());
}
@Override
protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
changePowered(level, pos, state, false);
}
@Override
protected void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean movedByPiston) {
if (!state.is(newState.getBlock())) {
if ((Boolean)state.getValue(HAS_BOOK)) {
this.popBook(state, level, pos);
}
super.onRemove(state, level, pos, newState, movedByPiston);
if ((Boolean)state.getValue(POWERED)) {
level.updateNeighborsAt(pos.below(), this);
}
}
}
private void popBook(BlockState state, Level level, BlockPos pos) {
if (level.getBlockEntity(pos) instanceof LecternBlockEntity lecternBlockEntity) {
Direction direction = state.getValue(FACING);
ItemStack itemStack = lecternBlockEntity.getBook().copy();
float f = 0.25F * direction.getStepX();
float g = 0.25F * direction.getStepZ();
ItemEntity itemEntity = new ItemEntity(level, pos.getX() + 0.5 + f, pos.getY() + 1, pos.getZ() + 0.5 + g, itemStack);
itemEntity.setDefaultPickUpDelay();
level.addFreshEntity(itemEntity);
lecternBlockEntity.clearContent();
}
}
@Override
protected boolean isSignalSource(BlockState state) {
return true;
}
@Override
protected int getSignal(BlockState state, BlockGetter level, BlockPos pos, Direction direction) {
return state.getValue(POWERED) ? 15 : 0;
}
@Override
protected int getDirectSignal(BlockState state, BlockGetter level, BlockPos pos, Direction direction) {
return direction == Direction.UP && state.getValue(POWERED) ? 15 : 0;
}
@Override
protected boolean hasAnalogOutputSignal(BlockState state) {
return true;
}
@Override
protected int getAnalogOutputSignal(BlockState state, Level level, BlockPos pos) {
if ((Boolean)state.getValue(HAS_BOOK)) {
BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity instanceof LecternBlockEntity) {
return ((LecternBlockEntity)blockEntity).getRedstoneSignal();
}
}
return 0;
}
@Override
protected ItemInteractionResult useItemOn(
ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult
) {
if ((Boolean)state.getValue(HAS_BOOK)) {
return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
} else if (stack.is(ItemTags.LECTERN_BOOKS)) {
return tryPlaceBook(player, level, pos, state, stack)
? ItemInteractionResult.sidedSuccess(level.isClientSide)
: ItemInteractionResult.SKIP_DEFAULT_BLOCK_INTERACTION;
} else {
return stack.isEmpty() && hand == InteractionHand.MAIN_HAND
? ItemInteractionResult.SKIP_DEFAULT_BLOCK_INTERACTION
: ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
}
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) {
if ((Boolean)state.getValue(HAS_BOOK)) {
if (!level.isClientSide) {
this.openScreen(level, pos, player);
}
return InteractionResult.sidedSuccess(level.isClientSide);
} else {
return InteractionResult.CONSUME;
}
}
@Nullable
@Override
protected MenuProvider getMenuProvider(BlockState state, Level level, BlockPos pos) {
return !state.getValue(HAS_BOOK) ? null : super.getMenuProvider(state, level, pos);
}
private void openScreen(Level level, BlockPos pos, Player player) {
BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity instanceof LecternBlockEntity) {
player.openMenu((LecternBlockEntity)blockEntity);
player.awardStat(Stats.INTERACT_WITH_LECTERN);
}
}
@Override
protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) {
return false;
}
}