| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Holder;
|
| 5 | import net.minecraft.core.particles.ParticleTypes;
|
| 6 | import net.minecraft.core.registries.Registries;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.world.level.LevelAccessor;
|
| 9 | import net.minecraft.world.level.block.state.BlockState;
|
| 10 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 11 | import net.minecraft.world.phys.Vec3;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | public class JukeboxSongPlayer {
|
| 15 | public static final int PLAY_EVENT_INTERVAL_TICKS = 20;
|
| 16 | private long ticksSinceSongStarted;
|
| 17 | private @Nullable Holder<JukeboxSong> song;
|
| 18 | private final BlockPos blockPos;
|
| 19 | private final JukeboxSongPlayer.OnSongChanged onSongChanged;
|
| 20 |
|
| 21 | public JukeboxSongPlayer(final JukeboxSongPlayer.OnSongChanged onSongChanged, final BlockPos blockPos) {
|
| 22 | this.onSongChanged = onSongChanged;
|
| 23 | this.blockPos = blockPos;
|
| 24 | }
|
| 25 |
|
| 26 | public boolean isPlaying() {
|
| 27 | return this.song != null;
|
| 28 | }
|
| 29 |
|
| 30 | public @Nullable JukeboxSong getSong() {
|
| 31 | return this.song == null ? null : this.song.value();
|
| 32 | }
|
| 33 |
|
| 34 | public long getTicksSinceSongStarted() {
|
| 35 | return this.ticksSinceSongStarted;
|
| 36 | }
|
| 37 |
|
| 38 | public void setSongWithoutPlaying(final Holder<JukeboxSong> song, final long ticksSinceSongStarted) {
|
| 39 | if (!song.value().hasFinished(ticksSinceSongStarted)) {
|
| 40 | this.song = song;
|
| 41 | this.ticksSinceSongStarted = ticksSinceSongStarted;
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | public void play(final LevelAccessor level, final Holder<JukeboxSong> song) {
|
| 46 | this.song = song;
|
| 47 | this.ticksSinceSongStarted = 0L;
|
| 48 | int songId = level.registryAccess().lookupOrThrow(Registries.JUKEBOX_SONG).getId(this.song.value());
|
| 49 | level.levelEvent(null, 1010, this.blockPos, songId);
|
| 50 | this.onSongChanged.notifyChange();
|
| 51 | }
|
| 52 |
|
| 53 | public void stop(final LevelAccessor level, final @Nullable BlockState blockState) {
|
| 54 | if (this.song != null) {
|
| 55 | this.song = null;
|
| 56 | this.ticksSinceSongStarted = 0L;
|
| 57 | level.gameEvent(GameEvent.JUKEBOX_STOP_PLAY, this.blockPos, GameEvent.Context.of(blockState));
|
| 58 | level.levelEvent(1011, this.blockPos, 0);
|
| 59 | this.onSongChanged.notifyChange();
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | public void tick(final LevelAccessor level, final @Nullable BlockState blockState) {
|
| 64 | if (this.song != null) {
|
| 65 | if (this.song.value().hasFinished(this.ticksSinceSongStarted)) {
|
| 66 | this.stop(level, blockState);
|
| 67 | } else {
|
| 68 | if (this.shouldEmitJukeboxPlayingEvent()) {
|
| 69 | level.gameEvent(GameEvent.JUKEBOX_PLAY, this.blockPos, GameEvent.Context.of(blockState));
|
| 70 | spawnMusicParticles(level, this.blockPos);
|
| 71 | }
|
| 72 |
|
| 73 | this.ticksSinceSongStarted++;
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | private boolean shouldEmitJukeboxPlayingEvent() {
|
| 79 | return this.ticksSinceSongStarted % 20L == 0L;
|
| 80 | }
|
| 81 |
|
| 82 | private static void spawnMusicParticles(final LevelAccessor level, final BlockPos blockPos) {
|
| 83 | if (level instanceof ServerLevel serverLevel) {
|
| 84 | Vec3 pos = Vec3.atBottomCenterOf(blockPos).add(0.0, 1.2F, 0.0);
|
| 85 | float randomColor = level.getRandom().nextInt(4) / 24.0F;
|
| 86 | serverLevel.sendParticles(ParticleTypes.NOTE, pos.x(), pos.y(), pos.z(), 0, randomColor, 0.0, 0.0, 1.0);
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | @FunctionalInterface
|
| 91 | public interface OnSongChanged {
|
| 92 | void notifyChange();
|
| 93 | }
|
| 94 | }
|
| 95 |
|