140 lines
5.6 KiB
Java
140 lines
5.6 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* com.mojang.serialization.MapCodec
|
|
* org.jspecify.annotations.Nullable
|
|
*/
|
|
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.world.Containers;
|
|
import net.minecraft.world.InteractionHand;
|
|
import net.minecraft.world.InteractionResult;
|
|
import net.minecraft.world.entity.LivingEntity;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.item.JukeboxPlayable;
|
|
import net.minecraft.world.item.component.TypedEntityData;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.block.BaseEntityBlock;
|
|
import net.minecraft.world.level.block.Block;
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
|
import net.minecraft.world.level.block.entity.BlockEntityType;
|
|
import net.minecraft.world.level.block.entity.JukeboxBlockEntity;
|
|
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.phys.BlockHitResult;
|
|
import org.jspecify.annotations.Nullable;
|
|
|
|
public class JukeboxBlock
|
|
extends BaseEntityBlock {
|
|
public static final MapCodec<JukeboxBlock> CODEC = JukeboxBlock.simpleCodec(JukeboxBlock::new);
|
|
public static final BooleanProperty HAS_RECORD = BlockStateProperties.HAS_RECORD;
|
|
|
|
public MapCodec<JukeboxBlock> codec() {
|
|
return CODEC;
|
|
}
|
|
|
|
protected JukeboxBlock(BlockBehaviour.Properties properties) {
|
|
super(properties);
|
|
this.registerDefaultState((BlockState)((BlockState)this.stateDefinition.any()).setValue(HAS_RECORD, false));
|
|
}
|
|
|
|
@Override
|
|
public void setPlacedBy(Level level, BlockPos pos, BlockState state, @Nullable LivingEntity by, ItemStack itemStack) {
|
|
super.setPlacedBy(level, pos, state, by, itemStack);
|
|
TypedEntityData<BlockEntityType<?>> blockEntityData = itemStack.get(DataComponents.BLOCK_ENTITY_DATA);
|
|
if (blockEntityData != null && blockEntityData.contains("RecordItem")) {
|
|
level.setBlock(pos, (BlockState)state.setValue(HAS_RECORD, true), 2);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) {
|
|
BlockEntity blockEntity;
|
|
if (state.getValue(HAS_RECORD).booleanValue() && (blockEntity = level.getBlockEntity(pos)) instanceof JukeboxBlockEntity) {
|
|
JukeboxBlockEntity jukebox = (JukeboxBlockEntity)blockEntity;
|
|
jukebox.popOutTheItem();
|
|
return InteractionResult.SUCCESS;
|
|
}
|
|
return InteractionResult.PASS;
|
|
}
|
|
|
|
@Override
|
|
protected InteractionResult useItemOn(ItemStack itemStack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) {
|
|
if (state.getValue(HAS_RECORD).booleanValue()) {
|
|
return InteractionResult.TRY_WITH_EMPTY_HAND;
|
|
}
|
|
ItemStack toInsert = player.getItemInHand(hand);
|
|
InteractionResult result = JukeboxPlayable.tryInsertIntoJukebox(level, pos, toInsert, player);
|
|
if (!result.consumesAction()) {
|
|
return InteractionResult.TRY_WITH_EMPTY_HAND;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
protected void affectNeighborsAfterRemoval(BlockState state, ServerLevel level, BlockPos pos, boolean movedByPiston) {
|
|
Containers.updateNeighboursAfterDestroy(state, level, pos);
|
|
}
|
|
|
|
@Override
|
|
public BlockEntity newBlockEntity(BlockPos worldPosition, BlockState blockState) {
|
|
return new JukeboxBlockEntity(worldPosition, blockState);
|
|
}
|
|
|
|
@Override
|
|
public boolean isSignalSource(BlockState state) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public int getSignal(BlockState state, BlockGetter level, BlockPos pos, Direction direction) {
|
|
JukeboxBlockEntity jukebox;
|
|
BlockEntity blockEntity = level.getBlockEntity(pos);
|
|
if (blockEntity instanceof JukeboxBlockEntity && (jukebox = (JukeboxBlockEntity)blockEntity).getSongPlayer().isPlaying()) {
|
|
return 15;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
protected boolean hasAnalogOutputSignal(BlockState state) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
protected int getAnalogOutputSignal(BlockState state, Level level, BlockPos pos, Direction direction) {
|
|
BlockEntity blockEntity = level.getBlockEntity(pos);
|
|
if (blockEntity instanceof JukeboxBlockEntity) {
|
|
JukeboxBlockEntity jukebox = (JukeboxBlockEntity)blockEntity;
|
|
return jukebox.getComparatorOutput();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
|
|
builder.add(HAS_RECORD);
|
|
}
|
|
|
|
@Override
|
|
public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(Level level, BlockState blockState, BlockEntityType<T> type) {
|
|
if (blockState.getValue(HAS_RECORD).booleanValue()) {
|
|
return JukeboxBlock.createTickerHelper(type, BlockEntityType.JUKEBOX, JukeboxBlockEntity::tick);
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|