| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.Map;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.core.component.DataComponents;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvents;
|
| 9 | import net.minecraft.sounds.SoundSource;
|
| 10 | import net.minecraft.stats.Stats;
|
| 11 | import net.minecraft.tags.ItemTags;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.world.InteractionHand;
|
| 14 | import net.minecraft.world.InteractionResult;
|
| 15 | import net.minecraft.world.MenuProvider;
|
| 16 | import net.minecraft.world.entity.Entity;
|
| 17 | import net.minecraft.world.entity.LivingEntity;
|
| 18 | import net.minecraft.world.entity.player.Player;
|
| 19 | import net.minecraft.world.item.ItemStack;
|
| 20 | import net.minecraft.world.item.component.TypedEntityData;
|
| 21 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 22 | import net.minecraft.world.level.BlockGetter;
|
| 23 | import net.minecraft.world.level.Level;
|
| 24 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 25 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 26 | import net.minecraft.world.level.block.entity.LecternBlockEntity;
|
| 27 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 28 | import net.minecraft.world.level.block.state.BlockState;
|
| 29 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 30 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 31 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 32 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 33 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 34 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 35 | import net.minecraft.world.level.redstone.ExperimentalRedstoneUtils;
|
| 36 | import net.minecraft.world.level.redstone.Orientation;
|
| 37 | import net.minecraft.world.phys.BlockHitResult;
|
| 38 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 39 | import net.minecraft.world.phys.shapes.Shapes;
|
| 40 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 41 | import org.jspecify.annotations.Nullable;
|
| 42 |
|
| 43 | /**
|
| 44 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 45 | */
|
| 46 | public class LecternBlock extends BaseEntityBlock {
|
| 47 | public static final EnumProperty<Direction> FACING = HorizontalDirectionalBlock.FACING;
|
| 48 | public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
|
| 49 | public static final BooleanProperty HAS_BOOK = BlockStateProperties.HAS_BOOK;
|
| 50 | private static final VoxelShape SHAPE_COLLISION = Shapes.or(Block.column(16.0, 0.0, 2.0), Block.column(8.0, 2.0, 14.0));
|
| 51 | private static final Map<Direction, VoxelShape> SHAPES = Shapes.rotateHorizontal(
|
| 52 | Shapes.or(
|
| 53 | Block.boxZ(16.0, 10.0, 14.0, 1.0, 5.333333), Block.boxZ(16.0, 12.0, 16.0, 5.333333, 9.666667), Block.boxZ(16.0, 14.0, 18.0, 9.666667, 14.0), SHAPE_COLLISION
|
| 54 | )
|
| 55 | );
|
| 56 | private static final int PAGE_CHANGE_IMPULSE_TICKS = 2;
|
| 57 |
|
| 58 | /**
|
| 59 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 60 | */
|
| 61 | public LecternBlock(final BlockBehaviour.Properties properties) {
|
| 62 | super(properties);
|
| 63 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(POWERED, false).setValue(HAS_BOOK, false));
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected VoxelShape getOcclusionShape(final BlockState state) {
|
| 68 | return SHAPE_COLLISION;
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | protected boolean useShapeForLightOcclusion(final BlockState state) {
|
| 73 | return true;
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 78 | Level level = context.getLevel();
|
| 79 | ItemStack itemStack = context.getItemInHand();
|
| 80 | Player player = context.getPlayer();
|
| 81 | boolean hasBook = false;
|
| 82 | if (!level.isClientSide() && player != null && player.canUseGameMasterBlocks()) {
|
| 83 | TypedEntityData<BlockEntityType<?>> blockEntityData = itemStack.get(DataComponents.BLOCK_ENTITY_DATA);
|
| 84 | if (blockEntityData != null && blockEntityData.contains("Book")) {
|
| 85 | hasBook = true;
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite()).setValue(HAS_BOOK, hasBook);
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | protected VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 94 | return SHAPE_COLLISION;
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 99 | return SHAPES.get(state.getValue(FACING));
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 104 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 109 | return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 114 | builder.add(FACING, POWERED, HAS_BOOK);
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 119 | return new LecternBlockEntity(worldPosition, blockState);
|
| 120 | }
|
| 121 |
|
| 122 | public static boolean tryPlaceBook(
|
| 123 | final @Nullable LivingEntity sourceEntity, final Level level, final BlockPos pos, final BlockState state, final ItemStack item
|
| 124 | ) {
|
| 125 | if (!state.getValue(HAS_BOOK)) {
|
| 126 | if (!level.isClientSide()) {
|
| 127 | placeBook(sourceEntity, level, pos, state, item);
|
| 128 | }
|
| 129 |
|
| 130 | return true;
|
| 131 | } else {
|
| 132 | return false;
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | private static void placeBook(final @Nullable LivingEntity sourceEntity, final Level level, final BlockPos pos, final BlockState state, final ItemStack book) {
|
| 137 | if (level.getBlockEntity(pos) instanceof LecternBlockEntity lectern) {
|
| 138 | lectern.setBook(book.consumeAndReturn(1, sourceEntity));
|
| 139 | resetBookState(sourceEntity, level, pos, state, true);
|
| 140 | level.playSound(null, pos, SoundEvents.BOOK_PUT, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | public static void resetBookState(final @Nullable Entity sourceEntity, final Level level, final BlockPos pos, final BlockState state, final boolean hasBook) {
|
| 145 | BlockState newState = state.setValue(POWERED, false).setValue(HAS_BOOK, hasBook);
|
| 146 | level.setBlockAndUpdate(pos, newState);
|
| 147 | level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(sourceEntity, newState));
|
| 148 | updateBelow(level, pos, state);
|
| 149 | }
|
| 150 |
|
| 151 | public static void signalPageChange(final Level level, final BlockPos pos, final BlockState state) {
|
| 152 | changePowered(level, pos, state, true);
|
| 153 | level.scheduleTick(pos, state.getBlock(), 2);
|
| 154 | level.levelEvent(1043, pos, 0);
|
| 155 | }
|
| 156 |
|
| 157 | private static void changePowered(final Level level, final BlockPos pos, final BlockState state, final boolean isPowered) {
|
| 158 | level.setBlockAndUpdate(pos, state.setValue(POWERED, isPowered));
|
| 159 | updateBelow(level, pos, state);
|
| 160 | }
|
| 161 |
|
| 162 | private static void updateBelow(final Level level, final BlockPos pos, final BlockState state) {
|
| 163 | Orientation orientation = ExperimentalRedstoneUtils.initialOrientation(level, state.getValue(FACING).getOpposite(), Direction.UP);
|
| 164 | level.updateNeighborsAt(pos.below(), state.getBlock(), orientation);
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 169 | changePowered(level, pos, state, false);
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 174 | if (state.getValue(POWERED)) {
|
| 175 | updateBelow(level, pos, state);
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | @Override
|
| 180 | protected boolean isSignalSource(final BlockState state) {
|
| 181 | return true;
|
| 182 | }
|
| 183 |
|
| 184 | @Override
|
| 185 | protected int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 186 | return state.getValue(POWERED) ? 15 : 0;
|
| 187 | }
|
| 188 |
|
| 189 | @Override
|
| 190 | protected int getDirectSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) {
|
| 191 | return direction == Direction.UP && state.getValue(POWERED) ? 15 : 0;
|
| 192 | }
|
| 193 |
|
| 194 | @Override
|
| 195 | protected boolean hasAnalogOutputSignal(final BlockState state) {
|
| 196 | return true;
|
| 197 | }
|
| 198 |
|
| 199 | @Override
|
| 200 | protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
|
| 201 | return state.getValue(HAS_BOOK) && level.getBlockEntity(pos) instanceof LecternBlockEntity lecternBlockEntity ? lecternBlockEntity.getRedstoneSignal() : 0;
|
| 202 | }
|
| 203 |
|
| 204 | @Override
|
| 205 | protected InteractionResult useItemOn(
|
| 206 | final ItemStack itemStack,
|
| 207 | final BlockState state,
|
| 208 | final Level level,
|
| 209 | final BlockPos pos,
|
| 210 | final Player player,
|
| 211 | final InteractionHand hand,
|
| 212 | final BlockHitResult hitResult
|
| 213 | ) {
|
| 214 | if (state.getValue(HAS_BOOK)) {
|
| 215 | return InteractionResult.TRY_WITH_EMPTY_HAND;
|
| 216 | } else if (itemStack.is(ItemTags.LECTERN_BOOKS)) {
|
| 217 | return tryPlaceBook(player, level, pos, state, itemStack) ? InteractionResult.SUCCESS : InteractionResult.PASS;
|
| 218 | } else {
|
| 219 | return itemStack.isEmpty() && hand == InteractionHand.MAIN_HAND ? InteractionResult.PASS : InteractionResult.TRY_WITH_EMPTY_HAND;
|
| 220 | }
|
| 221 | }
|
| 222 |
|
| 223 | @Override
|
| 224 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 225 | if (state.getValue(HAS_BOOK)) {
|
| 226 | if (!level.isClientSide()) {
|
| 227 | this.openScreen(level, pos, player);
|
| 228 | }
|
| 229 |
|
| 230 | return InteractionResult.SUCCESS;
|
| 231 | } else {
|
| 232 | return InteractionResult.CONSUME;
|
| 233 | }
|
| 234 | }
|
| 235 |
|
| 236 | @Override
|
| 237 | protected @Nullable MenuProvider getMenuProvider(final BlockState state, final Level level, final BlockPos pos) {
|
| 238 | return !state.getValue(HAS_BOOK) ? null : super.getMenuProvider(state, level, pos);
|
| 239 | }
|
| 240 |
|
| 241 | private void openScreen(final Level level, final BlockPos pos, final Player player) {
|
| 242 | if (level.getBlockEntity(pos) instanceof LecternBlockEntity lecternBlockEntity) {
|
| 243 | player.openMenu(lecternBlockEntity);
|
| 244 | player.awardStat(Stats.INTERACT_WITH_LECTERN);
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | @Override
|
| 249 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 250 | return false;
|
| 251 | }
|
| 252 | }
|
| 253 |
|