| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.particles.ParticleTypes;
|
| 6 | import net.minecraft.network.chat.Component;
|
| 7 | import net.minecraft.tags.BlockTags;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.world.InteractionResult;
|
| 10 | import net.minecraft.world.MenuProvider;
|
| 11 | import net.minecraft.world.SimpleMenuProvider;
|
| 12 | import net.minecraft.world.entity.player.Player;
|
| 13 | import net.minecraft.world.inventory.ContainerLevelAccess;
|
| 14 | import net.minecraft.world.inventory.EnchantmentMenu;
|
| 15 | import net.minecraft.world.level.BlockGetter;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 18 | import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
| 19 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 20 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 21 | import net.minecraft.world.level.block.entity.EnchantingTableBlockEntity;
|
| 22 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 23 | import net.minecraft.world.level.block.state.BlockState;
|
| 24 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 25 | import net.minecraft.world.phys.BlockHitResult;
|
| 26 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 27 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | /**
|
| 31 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 32 | */
|
| 33 | public class EnchantingTableBlock extends BaseEntityBlock {
|
| 34 | public static final List<BlockPos> BOOKSHELF_OFFSETS = BlockPos.betweenClosedStream(-2, 0, -2, 2, 1, 2)
|
| 35 | .filter(pos -> Math.abs(pos.getX()) == 2 || Math.abs(pos.getZ()) == 2)
|
| 36 | .map(BlockPos::immutable)
|
| 37 | .toList();
|
| 38 | private static final VoxelShape SHAPE = Block.column(16.0, 0.0, 12.0);
|
| 39 |
|
| 40 | /**
|
| 41 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 42 | */
|
| 43 | public EnchantingTableBlock(final BlockBehaviour.Properties properties) {
|
| 44 | super(properties);
|
| 45 | }
|
| 46 |
|
| 47 | public static boolean isValidBookShelf(final Level level, final BlockPos pos, final BlockPos offset) {
|
| 48 | return level.getBlockState(pos.offset(offset)).is(BlockTags.ENCHANTMENT_POWER_PROVIDER)
|
| 49 | && level.getBlockState(pos.offset(offset.getX() / 2, offset.getY(), offset.getZ() / 2)).is(BlockTags.ENCHANTMENT_POWER_TRANSMITTER);
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected boolean useShapeForLightOcclusion(final BlockState state) {
|
| 54 | return true;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 59 | return SHAPE;
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public void animateTick(final BlockState state, final Level level, final BlockPos pos, final RandomSource random) {
|
| 64 | super.animateTick(state, level, pos, random);
|
| 65 |
|
| 66 | for (BlockPos offset : BOOKSHELF_OFFSETS) {
|
| 67 | if (random.nextInt(16) == 0 && isValidBookShelf(level, pos, offset)) {
|
| 68 | level.addParticle(
|
| 69 | ParticleTypes.ENCHANT,
|
| 70 | pos.getX() + 0.5,
|
| 71 | pos.getY() + 2.0,
|
| 72 | pos.getZ() + 0.5,
|
| 73 | offset.getX() + random.nextFloat() - 0.5,
|
| 74 | offset.getY() - random.nextFloat() - 1.0F,
|
| 75 | offset.getZ() + random.nextFloat() - 0.5
|
| 76 | );
|
| 77 | }
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 83 | return new EnchantingTableBlockEntity(worldPosition, blockState);
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
|
| 88 | return level.isClientSide() ? createTickerHelper(type, BlockEntityTypes.ENCHANTING_TABLE, EnchantingTableBlockEntity::bookAnimationTick) : null;
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 93 | if (!level.isClientSide()) {
|
| 94 | player.openMenu(state.getMenuProvider(level, pos));
|
| 95 | }
|
| 96 |
|
| 97 | return InteractionResult.SUCCESS;
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | protected @Nullable MenuProvider getMenuProvider(final BlockState state, final Level level, final BlockPos pos) {
|
| 102 | if (level.getBlockEntity(pos) instanceof EnchantingTableBlockEntity enchantingTable) {
|
| 103 | Component title = enchantingTable.getDisplayName();
|
| 104 | return new SimpleMenuProvider(
|
| 105 | (containerId, inventory, player) -> new EnchantmentMenu(containerId, inventory, ContainerLevelAccess.create(level, pos)), title
|
| 106 | );
|
| 107 | } else {
|
| 108 | return null;
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 114 | return false;
|
| 115 | }
|
| 116 | }
|
| 117 |
|