| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.core.component.DataComponents;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.world.Containers;
|
| 8 | import net.minecraft.world.InteractionHand;
|
| 9 | import net.minecraft.world.InteractionResult;
|
| 10 | import net.minecraft.world.entity.LivingEntity;
|
| 11 | import net.minecraft.world.entity.player.Player;
|
| 12 | import net.minecraft.world.item.ItemStack;
|
| 13 | import net.minecraft.world.item.JukeboxPlayable;
|
| 14 | import net.minecraft.world.item.component.TypedEntityData;
|
| 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.JukeboxBlockEntity;
|
| 22 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 23 | import net.minecraft.world.level.block.state.BlockState;
|
| 24 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 25 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 26 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 27 | import net.minecraft.world.phys.BlockHitResult;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | /**
|
| 31 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 32 | */
|
| 33 | public class JukeboxBlock extends BaseEntityBlock {
|
| 34 | public static final BooleanProperty HAS_RECORD = BlockStateProperties.HAS_RECORD;
|
| 35 |
|
| 36 | /**
|
| 37 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 38 | */
|
| 39 | public JukeboxBlock(final BlockBehaviour.Properties properties) {
|
| 40 | super(properties);
|
| 41 | this.registerDefaultState(this.stateDefinition.any().setValue(HAS_RECORD, false));
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
|
| 46 | super.setPlacedBy(level, pos, state, by, itemStack);
|
| 47 | TypedEntityData<BlockEntityType<?>> blockEntityData = itemStack.get(DataComponents.BLOCK_ENTITY_DATA);
|
| 48 | if (blockEntityData != null && blockEntityData.contains("RecordItem")) {
|
| 49 | level.setBlock(pos, state.setValue(HAS_RECORD, true), 2);
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 55 | if (state.getValue(HAS_RECORD) && level.getBlockEntity(pos) instanceof JukeboxBlockEntity jukebox) {
|
| 56 | jukebox.popOutTheItem();
|
| 57 | return InteractionResult.SUCCESS;
|
| 58 | } else {
|
| 59 | return InteractionResult.PASS;
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | protected InteractionResult useItemOn(
|
| 65 | final ItemStack itemStack,
|
| 66 | final BlockState state,
|
| 67 | final Level level,
|
| 68 | final BlockPos pos,
|
| 69 | final Player player,
|
| 70 | final InteractionHand hand,
|
| 71 | final BlockHitResult hitResult
|
| 72 | ) {
|
| 73 | if (state.getValue(HAS_RECORD)) {
|
| 74 | return InteractionResult.TRY_WITH_EMPTY_HAND;
|
| 75 | }
|
| 76 |
|
| 77 | ItemStack toInsert = player.getItemInHand(hand);
|
| 78 | InteractionResult result = JukeboxPlayable.tryInsertIntoJukebox(level, pos, toInsert, player);
|
| 79 | return !result.consumesAction() ? InteractionResult.TRY_WITH_EMPTY_HAND : result;
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 84 | Containers.updateNeighboursAfterDestroy(state, level, pos);
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 89 | return new JukeboxBlockEntity(worldPosition, blockState);
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public boolean isSignalSource(final BlockState state) {
|
| 94 | return true;
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | protected int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 99 | return level.getBlockEntity(pos) instanceof JukeboxBlockEntity jukebox && jukebox.getSongPlayer().isPlaying() ? 15 : 0;
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | protected boolean hasAnalogOutputSignal(final BlockState state) {
|
| 104 | return true;
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
|
| 109 | return level.getBlockEntity(pos) instanceof JukeboxBlockEntity jukebox ? jukebox.getComparatorOutput() : 0;
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 114 | builder.add(HAS_RECORD);
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
|
| 119 | return blockState.getValue(HAS_RECORD) ? createTickerHelper(type, BlockEntityTypes.JUKEBOX, JukeboxBlockEntity::tick) : null;
|
| 120 | }
|
| 121 | }
|
| 122 |
|