| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import java.util.function.Consumer;
|
| 5 | import net.minecraft.ChatFormatting;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.component.DataComponentGetter;
|
| 9 | import net.minecraft.core.component.DataComponents;
|
| 10 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.network.chat.ComponentUtils;
|
| 13 | import net.minecraft.network.chat.Style;
|
| 14 | import net.minecraft.network.codec.StreamCodec;
|
| 15 | import net.minecraft.stats.Stats;
|
| 16 | import net.minecraft.world.InteractionResult;
|
| 17 | import net.minecraft.world.entity.player.Player;
|
| 18 | import net.minecraft.world.item.component.TooltipProvider;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 | import net.minecraft.world.level.block.Blocks;
|
| 21 | import net.minecraft.world.level.block.JukeboxBlock;
|
| 22 | import net.minecraft.world.level.block.entity.JukeboxBlockEntity;
|
| 23 | import net.minecraft.world.level.block.state.BlockState;
|
| 24 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 25 |
|
| 26 | public record JukeboxPlayable(Holder<JukeboxSong> song) implements TooltipProvider {
|
| 27 | public static final Codec<JukeboxPlayable> CODEC = JukeboxSong.CODEC.xmap(JukeboxPlayable::new, JukeboxPlayable::song);
|
| 28 | public static final StreamCodec<RegistryFriendlyByteBuf, JukeboxPlayable> STREAM_CODEC = StreamCodec.composite(
|
| 29 | JukeboxSong.STREAM_CODEC, JukeboxPlayable::song, JukeboxPlayable::new
|
| 30 | );
|
| 31 |
|
| 32 | @Override
|
| 33 | public void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components) {
|
| 34 | consumer.accept(ComponentUtils.mergeStyles(this.song.value().description(), Style.EMPTY.withColor(ChatFormatting.GRAY)));
|
| 35 | }
|
| 36 |
|
| 37 | public static InteractionResult tryInsertIntoJukebox(final Level level, final BlockPos pos, final ItemStack toInsert, final Player player) {
|
| 38 | JukeboxPlayable jukeboxPlayable = toInsert.get(DataComponents.JUKEBOX_PLAYABLE);
|
| 39 | if (jukeboxPlayable == null) {
|
| 40 | return InteractionResult.TRY_WITH_EMPTY_HAND;
|
| 41 | }
|
| 42 |
|
| 43 | BlockState state = level.getBlockState(pos);
|
| 44 | if (state.is(Blocks.JUKEBOX) && !state.getValue(JukeboxBlock.HAS_RECORD)) {
|
| 45 | if (!level.isClientSide()) {
|
| 46 | ItemStack inserted = toInsert.consumeAndReturn(1, player);
|
| 47 | if (level.getBlockEntity(pos) instanceof JukeboxBlockEntity jukebox) {
|
| 48 | jukebox.setTheItem(inserted);
|
| 49 | level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(player, state));
|
| 50 | }
|
| 51 |
|
| 52 | player.awardStat(Stats.PLAY_RECORD);
|
| 53 | }
|
| 54 |
|
| 55 | return InteractionResult.SUCCESS;
|
| 56 | } else {
|
| 57 | return InteractionResult.TRY_WITH_EMPTY_HAND;
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 |
|