| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.Optional;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Holder;
|
| 7 | import net.minecraft.core.component.DataComponents;
|
| 8 | import net.minecraft.world.Container;
|
| 9 | import net.minecraft.world.entity.item.ItemEntity;
|
| 10 | import net.minecraft.world.item.ItemStack;
|
| 11 | import net.minecraft.world.item.JukeboxSong;
|
| 12 | import net.minecraft.world.item.JukeboxSongPlayer;
|
| 13 | import net.minecraft.world.level.Level;
|
| 14 | import net.minecraft.world.level.block.JukeboxBlock;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 17 | import net.minecraft.world.level.storage.ValueInput;
|
| 18 | import net.minecraft.world.level.storage.ValueOutput;
|
| 19 | import net.minecraft.world.phys.Vec3;
|
| 20 | import net.minecraft.world.ticks.ContainerSingleItem;
|
| 21 |
|
| 22 | public class JukeboxBlockEntity extends BlockEntity implements ContainerSingleItem.BlockContainerSingleItem {
|
| 23 | public static final String SONG_ITEM_TAG_ID = "RecordItem";
|
| 24 | public static final String TICKS_SINCE_SONG_STARTED_TAG_ID = "ticks_since_song_started";
|
| 25 | private ItemStack item = ItemStack.EMPTY;
|
| 26 | private final JukeboxSongPlayer jukeboxSongPlayer = new JukeboxSongPlayer(this::onSongChanged, this.getBlockPos());
|
| 27 |
|
| 28 | public JukeboxBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 29 | super(BlockEntityTypes.JUKEBOX, worldPosition, blockState);
|
| 30 | }
|
| 31 |
|
| 32 | public JukeboxSongPlayer getSongPlayer() {
|
| 33 | return this.jukeboxSongPlayer;
|
| 34 | }
|
| 35 |
|
| 36 | public void onSongChanged() {
|
| 37 | this.level.updateNeighborsAt(this.getBlockPos(), this.getBlockState().getBlock());
|
| 38 | this.setChanged();
|
| 39 | }
|
| 40 |
|
| 41 | private void notifyItemChangedInJukebox(final boolean wasInserted) {
|
| 42 | if (this.level != null && this.level.getBlockState(this.getBlockPos()) == this.getBlockState()) {
|
| 43 | this.level.setBlock(this.getBlockPos(), this.getBlockState().setValue(JukeboxBlock.HAS_RECORD, wasInserted), 2);
|
| 44 | this.level.gameEvent(GameEvent.BLOCK_CHANGE, this.getBlockPos(), GameEvent.Context.of(this.getBlockState()));
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | public void popOutTheItem() {
|
| 49 | if (this.level != null && !this.level.isClientSide()) {
|
| 50 | BlockPos pos = this.getBlockPos();
|
| 51 | ItemStack itemBeforePoppingOut = this.getTheItem();
|
| 52 | if (!itemBeforePoppingOut.isEmpty()) {
|
| 53 | this.removeTheItem();
|
| 54 | Vec3 itemPos = Vec3.atLowerCornerWithOffset(pos, 0.5, 1.01, 0.5).offsetRandomXZ(this.level.getRandom(), 0.7F);
|
| 55 | ItemStack itemStack = itemBeforePoppingOut.copy();
|
| 56 | ItemEntity entity = new ItemEntity(this.level, itemPos.x(), itemPos.y(), itemPos.z(), itemStack);
|
| 57 | entity.setDefaultPickUpDelay();
|
| 58 | this.level.addFreshEntity(entity);
|
| 59 | this.onSongChanged();
|
| 60 | }
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | public static void tick(final Level level, final BlockPos blockPos, final BlockState blockState, final JukeboxBlockEntity jukebox) {
|
| 65 | jukebox.jukeboxSongPlayer.tick(level, blockState);
|
| 66 | }
|
| 67 |
|
| 68 | public int getComparatorOutput() {
|
| 69 | return JukeboxSong.fromStack(this.item).map(Holder::value).map(JukeboxSong::comparatorOutput).orElse(0);
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | protected void loadAdditional(final ValueInput input) {
|
| 74 | super.loadAdditional(input);
|
| 75 | ItemStack newItem = input.read("RecordItem", ItemStack.CODEC).orElse(ItemStack.EMPTY);
|
| 76 | if (!this.item.isEmpty() && !ItemStack.isSameItemSameComponents(newItem, this.item)) {
|
| 77 | this.jukeboxSongPlayer.stop(this.level, this.getBlockState());
|
| 78 | }
|
| 79 |
|
| 80 | this.item = newItem;
|
| 81 | input.getLong("ticks_since_song_started")
|
| 82 | .ifPresent(
|
| 83 | ticksSinceSongStarted -> JukeboxSong.fromStack(this.item)
|
| 84 | .ifPresent(song -> this.jukeboxSongPlayer.setSongWithoutPlaying((Holder<JukeboxSong>)song, ticksSinceSongStarted))
|
| 85 | );
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | protected void saveAdditional(final ValueOutput output) {
|
| 90 | super.saveAdditional(output);
|
| 91 | if (!this.getTheItem().isEmpty()) {
|
| 92 | output.store("RecordItem", ItemStack.CODEC, this.getTheItem());
|
| 93 | }
|
| 94 |
|
| 95 | if (this.jukeboxSongPlayer.getSong() != null) {
|
| 96 | output.putLong("ticks_since_song_started", this.jukeboxSongPlayer.getTicksSinceSongStarted());
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public ItemStack getTheItem() {
|
| 102 | return this.item;
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public ItemStack splitTheItem(final int count) {
|
| 107 | ItemStack retrievedItem = this.item;
|
| 108 | this.setTheItem(ItemStack.EMPTY);
|
| 109 | return retrievedItem;
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public void setTheItem(final ItemStack itemStack) {
|
| 114 | this.item = itemStack;
|
| 115 | boolean itemWasInserted = !this.item.isEmpty();
|
| 116 | Optional<Holder<JukeboxSong>> maybeSong = JukeboxSong.fromStack(this.item);
|
| 117 | this.notifyItemChangedInJukebox(itemWasInserted);
|
| 118 | if (itemWasInserted && maybeSong.isPresent()) {
|
| 119 | this.jukeboxSongPlayer.play(this.level, maybeSong.get());
|
| 120 | } else {
|
| 121 | this.jukeboxSongPlayer.stop(this.level, this.getBlockState());
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public void setRemoved() {
|
| 127 | super.setRemoved();
|
| 128 | this.level.gameEvent(GameEvent.JUKEBOX_STOP_PLAY, this.getBlockPos(), GameEvent.Context.of(this.getBlockState()));
|
| 129 | this.level.levelEvent(1011, this.getBlockPos(), 0);
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public int getMaxStackSize() {
|
| 134 | return 1;
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | public BlockEntity getContainerBlockEntity() {
|
| 139 | return this;
|
| 140 | }
|
| 141 |
|
| 142 | @Override
|
| 143 | public boolean canPlaceItem(final int slot, final ItemStack itemStack) {
|
| 144 | return itemStack.has(DataComponents.JUKEBOX_PLAYABLE) && this.getItem(slot).isEmpty();
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | public boolean canTakeItem(final Container into, final int slot, final ItemStack itemStack) {
|
| 149 | return into.hasAnyMatching(ItemStack::isEmpty);
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | public void preRemoveSideEffects(final BlockPos pos, final BlockState state) {
|
| 154 | this.popOutTheItem();
|
| 155 | }
|
| 156 |
|
| 157 | @VisibleForTesting
|
| 158 | public void setSongItemWithoutPlaying(final ItemStack itemStack) {
|
| 159 | this.item = itemStack;
|
| 160 | JukeboxSong.fromStack(itemStack).ifPresent(song -> this.jukeboxSongPlayer.setSongWithoutPlaying((Holder<JukeboxSong>)song, 0L));
|
| 161 | this.level.updateNeighborsAt(this.getBlockPos(), this.getBlockState().getBlock());
|
| 162 | this.setChanged();
|
| 163 | }
|
| 164 |
|
| 165 | @VisibleForTesting
|
| 166 | public void tryForcePlaySong() {
|
| 167 | JukeboxSong.fromStack(this.getTheItem()).ifPresent(song -> this.jukeboxSongPlayer.play(this.level, (Holder<JukeboxSong>)song));
|
| 168 | }
|
| 169 | }
|
| 170 |
|