From d1a405430e8ea112a690c0855b782419026bf55b Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Thu, 19 Dec 2024 16:26:45 +0100 Subject: [PATCH] FIX --- .../eu/m724/musicPlugin/MusicCommands.java | 8 +- .../eu/m724/musicPlugin/file/AudioFile.java | 3 +- .../eu/m724/musicPlugin/item/ItemEvents.java | 18 ++-- .../eu/m724/musicPlugin/item/PlayedSong.java | 33 +++++++ .../musicPlugin/item/PortableMediaPlayer.java | 92 +++++++++++++++---- .../eu/m724/musicPlugin/item/Speakers.java | 2 - .../item/speaker/BlockSpeaker.java | 11 +-- .../musicPlugin/item/speaker/Speaker.java | 27 +++--- .../m724/musicPlugin/player/MusicPlayer.java | 34 +++---- 9 files changed, 156 insertions(+), 72 deletions(-) create mode 100644 src/main/java/eu/m724/musicPlugin/item/PlayedSong.java diff --git a/src/main/java/eu/m724/musicPlugin/MusicCommands.java b/src/main/java/eu/m724/musicPlugin/MusicCommands.java index 50df0a4..29e35c3 100644 --- a/src/main/java/eu/m724/musicPlugin/MusicCommands.java +++ b/src/main/java/eu/m724/musicPlugin/MusicCommands.java @@ -2,6 +2,7 @@ package eu.m724.musicPlugin; import eu.m724.musicPlugin.file.AudioFileStorage; import eu.m724.musicPlugin.item.PortableMediaPlayer; +import eu.m724.musicPlugin.item.PortableMediaPlayers; import eu.m724.musicPlugin.item.Speakers; import eu.m724.musicPlugin.player.MusicPlayer; import eu.m724.musicPlugin.file.AudioFile; @@ -88,7 +89,8 @@ public class MusicCommands implements CommandExecutor { try (FileInputStream fis = new FileInputStream(file)) { var song = new AudioFile(fis); song.load(); - player.play(song); + player.setAudio(song); + player.unpause(); } catch (IOException e) { throw new RuntimeException(e); } @@ -105,7 +107,7 @@ public class MusicCommands implements CommandExecutor { sender.sendMessage("unapuised"); } else if (command.getName().equals("pmp")) { var p = (Player) sender; - var pmp = PortableMediaPlayer.fromItemStack(p.getItemInHand()); + var pmp = PortableMediaPlayers.get(p.getItemInHand()); switch (args[0]) { case "info" -> { @@ -123,7 +125,7 @@ public class MusicCommands implements CommandExecutor { try (FileInputStream fis = new FileInputStream(file)) { var song = new AudioFile(fis); song.load(); - pmp.getMusicPlayer().play(song); + pmp.play(song); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/main/java/eu/m724/musicPlugin/file/AudioFile.java b/src/main/java/eu/m724/musicPlugin/file/AudioFile.java index 95380a1..63c1d48 100644 --- a/src/main/java/eu/m724/musicPlugin/file/AudioFile.java +++ b/src/main/java/eu/m724/musicPlugin/file/AudioFile.java @@ -40,7 +40,8 @@ public class AudioFile { frames += packet.getNumberOfFrames(); } - this.duration = frames / 50; + this.duration = frames / 50; // a frame is 20 ms long so 20 * 50 is 1000 + System.out.printf("audiop file has %s framrs\n", frames); this.title = opus.getTags().getTitle(); this.artist = opus.getTags().getArtist(); diff --git a/src/main/java/eu/m724/musicPlugin/item/ItemEvents.java b/src/main/java/eu/m724/musicPlugin/item/ItemEvents.java index b436101..160d037 100644 --- a/src/main/java/eu/m724/musicPlugin/item/ItemEvents.java +++ b/src/main/java/eu/m724/musicPlugin/item/ItemEvents.java @@ -25,7 +25,7 @@ public class ItemEvents implements Listener { if (event.getItem() == null) return; - var player = PortableMediaPlayer.fromItemStack(event.getItem()); + var player = PortableMediaPlayers.get(event.getItem()); if (player == null) return; System.out.println("A player used"); @@ -61,18 +61,18 @@ public class ItemEvents implements Listener { @EventHandler public void onDrop(PlayerDropItemEvent event) { - var item = PortableMediaPlayer.fromItemStack(event.getItemDrop().getItemStack()); - if (item == null) return; + var player = PortableMediaPlayers.get(event.getItemDrop().getItemStack()); + if (player == null) return; System.out.println("A playe rdropped"); - item.stop(); + player.stop(); } @EventHandler public void onPickup(EntityPickupItemEvent event) { - var item = PortableMediaPlayer.fromItemStack(event.getItem().getItemStack()); - if (item == null) return; + var player = PortableMediaPlayers.get(event.getItem().getItemStack()); + if (player == null) return; System.out.println("A player puckuperd"); @@ -81,12 +81,12 @@ public class ItemEvents implements Listener { public void onMove(InventoryMoveItemEvent event) { if (event.getDestination().getType() == InventoryType.PLAYER) { - var item = PortableMediaPlayer.fromItemStack(event.getItem()); - if (item == null) return; + var player = PortableMediaPlayers.get(event.getItem()); + if (player == null) return; System.out.println("A player storaged :(("); - item.stop(); + player.stop(); } } } diff --git a/src/main/java/eu/m724/musicPlugin/item/PlayedSong.java b/src/main/java/eu/m724/musicPlugin/item/PlayedSong.java new file mode 100644 index 0000000..2d59509 --- /dev/null +++ b/src/main/java/eu/m724/musicPlugin/item/PlayedSong.java @@ -0,0 +1,33 @@ +package eu.m724.musicPlugin.item; + +import eu.m724.musicPlugin.file.AudioFile; + +public class PlayedSong { + public final AudioFile file; + + private int progress = 0; + private long started = -1; + + public PlayedSong(AudioFile file) { + this.file = file; + } + + public int getProgress() { + if (started == -1) + return progress; + return (int) (System.currentTimeMillis() - started) + progress; + } + + public void pause() { + this.progress = getProgress(); + this.started = -1; + } + + public void unpause() { + this.started = System.currentTimeMillis(); + } + + public boolean paused() { + return started == -1; + } +} diff --git a/src/main/java/eu/m724/musicPlugin/item/PortableMediaPlayer.java b/src/main/java/eu/m724/musicPlugin/item/PortableMediaPlayer.java index e40e523..9f47e50 100644 --- a/src/main/java/eu/m724/musicPlugin/item/PortableMediaPlayer.java +++ b/src/main/java/eu/m724/musicPlugin/item/PortableMediaPlayer.java @@ -1,7 +1,8 @@ package eu.m724.musicPlugin.item; +import eu.m724.musicPlugin.file.AudioFile; import eu.m724.musicPlugin.item.speaker.Speaker; -import eu.m724.musicPlugin.player.MovingMusicPlayer; +import eu.m724.musicPlugin.player.MusicPlayer; import net.md_5.bungee.api.ChatColor; import org.bukkit.Material; import org.bukkit.NamespacedKey; @@ -28,6 +29,9 @@ public class PortableMediaPlayer { public final boolean premium; public final String engraving; + private PlayedSong played; + private Speaker speaker; + PortableMediaPlayer(int id, int storageSeconds, int audioBitrate, boolean premium, String engraving) { this.id = id; this.storageSeconds = storageSeconds; @@ -40,29 +44,81 @@ public class PortableMediaPlayer { return new PortableMediaPlayer(ThreadLocalRandom.current().nextInt(), 600, 32000, premium, engraving); } - public void stop() { - Speakers.speakers.remove(id).destroy(); - getMusicPlayer().stop(); - Speakers.players.remove(id); - } - public void setSpeaker(Speaker speaker) { - var linked = Speakers.speakers.get(id); - if (speaker.equals(linked)) + if (speaker.equals(this.speaker)) return; - if (linked != null) - linked.destroy(); + if (this.speaker != null) { + // no need to pause if no need to pause + this.speaker.setDestroyCallback(c -> {}); + this.speaker.destroy(); + } - var paused = getMusicPlayer().isPlaying(); - speaker.setMusicPlayer(getMusicPlayer()); - speaker.setDestroyCallback(c -> this.onSpeakerDestroyed()); - if (paused) getMusicPlayer().unpause(); - Speakers.speakers.put(id, speaker); + speaker.setDestroyCallback(c -> { + System.out.println("spekar rip"); + played.pause(); + }); + + this.speaker = speaker; + + if (played != null && !played.paused()) { + unpause(); + } } - public MovingMusicPlayer getMusicPlayer() { - return Speakers.players.computeIfAbsent(id, m -> new MovingMusicPlayer()); + public void play(AudioFile audioFile) { + if (speaker == null) return; + + played = new PlayedSong(audioFile); + + speaker.getMusicPlayer().setOnAction(action -> { + if (action == MusicPlayer.TrackAction.PAUSE) { + // track paused + System.out.println("Okay its paused"); + played.pause(); + } else if (action == MusicPlayer.TrackAction.UNPAUSE) { // track unpaused + System.out.println("Okay its unpaused"); + played.unpause(); + } else if (action == MusicPlayer.TrackAction.DURATION) { // track ended + System.out.println("Okay its ened"); + next(); + } + }); + + unpause(); + } + + void pause() { + System.out.println("pmp pase"); + if (played == null || speaker == null) return; + System.out.println("pmp pasedd"); + + speaker.getMusicPlayer().pause(); + } + + void unpause() { + System.out.println("pmp unpase"); + if (played == null || speaker == null) return; + System.out.println("pmp unpasedd"); + + speaker.getMusicPlayer().setAudio(played.file); + speaker.getMusicPlayer().seek(played.getProgress()); + speaker.getMusicPlayer().unpause(); + } + + void stop() { + System.out.println("pmp stop"); + + if (speaker != null) + speaker.destroy(); + + this.played = null; + } + + void next() { + System.out.println("pmp next"); + this.played = null; + // TODO } /* Item functions */ diff --git a/src/main/java/eu/m724/musicPlugin/item/Speakers.java b/src/main/java/eu/m724/musicPlugin/item/Speakers.java index e28e87d..e737fd0 100644 --- a/src/main/java/eu/m724/musicPlugin/item/Speakers.java +++ b/src/main/java/eu/m724/musicPlugin/item/Speakers.java @@ -1,12 +1,10 @@ package eu.m724.musicPlugin.item; import eu.m724.musicPlugin.item.speaker.Speaker; -import eu.m724.musicPlugin.player.MovingMusicPlayer; import java.util.HashMap; import java.util.Map; public class Speakers { static Map speakers = new HashMap<>(); - static Map players = new HashMap<>(); } diff --git a/src/main/java/eu/m724/musicPlugin/item/speaker/BlockSpeaker.java b/src/main/java/eu/m724/musicPlugin/item/speaker/BlockSpeaker.java index 02a929f..43389af 100644 --- a/src/main/java/eu/m724/musicPlugin/item/speaker/BlockSpeaker.java +++ b/src/main/java/eu/m724/musicPlugin/item/speaker/BlockSpeaker.java @@ -1,6 +1,8 @@ package eu.m724.musicPlugin.item.speaker; import eu.m724.musicPlugin.player.MovingMusicPlayer; +import eu.m724.musicPlugin.player.MusicPlayer; +import eu.m724.musicPlugin.player.StaticMusicPlayer; import org.bukkit.Location; import java.util.HashMap; @@ -14,7 +16,8 @@ public class BlockSpeaker extends Speaker { } public static BlockSpeaker create(Location location) { - if (speakers.containsKey(location)) return null; + if (speakers.containsKey(location)) + return null; return speakers.compute(location, (k, v) -> new BlockSpeaker(location)); } @@ -23,14 +26,10 @@ public class BlockSpeaker extends Speaker { private final Location location; public BlockSpeaker(Location location) { + super(new StaticMusicPlayer(location)); this.location = location; } - @Override - void onSetMusicPlayer(MovingMusicPlayer musicPlayer) { - musicPlayer.moveTo(location); - } - @Override void onDestroy() { speakers.remove(location); diff --git a/src/main/java/eu/m724/musicPlugin/item/speaker/Speaker.java b/src/main/java/eu/m724/musicPlugin/item/speaker/Speaker.java index 3ea4c3e..8a5c6fb 100644 --- a/src/main/java/eu/m724/musicPlugin/item/speaker/Speaker.java +++ b/src/main/java/eu/m724/musicPlugin/item/speaker/Speaker.java @@ -1,31 +1,23 @@ package eu.m724.musicPlugin.item.speaker; +import de.maxhenkel.voicechat.api.audiochannel.AudioChannel; +import eu.m724.musicPlugin.file.AudioFile; import eu.m724.musicPlugin.player.MovingMusicPlayer; +import eu.m724.musicPlugin.player.MusicPlayer; import java.util.function.Consumer; public abstract class Speaker { - private MovingMusicPlayer musicPlayer; + private final MusicPlayer musicPlayer; private Consumer destroyCallback; - public MovingMusicPlayer getMusicPlayer() { - return musicPlayer; - } - - public void setMusicPlayer(MovingMusicPlayer musicPlayer) { - if (musicPlayer.equals(this.musicPlayer)) return; - - if (this.musicPlayer != null) { - musicPlayer.pause(); - } - + protected Speaker(MusicPlayer musicPlayer) { this.musicPlayer = musicPlayer; - onSetMusicPlayer(musicPlayer); + musicPlayer.init(); } public void destroy() { - this.musicPlayer.pause(); - this.musicPlayer = null; + musicPlayer.stop(); onDestroy(); destroyCallback.accept(null); @@ -35,6 +27,9 @@ public abstract class Speaker { this.destroyCallback = consumer; } - abstract void onSetMusicPlayer(MovingMusicPlayer musicPlayer); + public MusicPlayer getMusicPlayer() { + return musicPlayer; + } + abstract void onDestroy(); } diff --git a/src/main/java/eu/m724/musicPlugin/player/MusicPlayer.java b/src/main/java/eu/m724/musicPlugin/player/MusicPlayer.java index 31173e7..e80c215 100644 --- a/src/main/java/eu/m724/musicPlugin/player/MusicPlayer.java +++ b/src/main/java/eu/m724/musicPlugin/player/MusicPlayer.java @@ -3,7 +3,6 @@ package eu.m724.musicPlugin.player; import de.maxhenkel.voicechat.api.VoicechatServerApi; import de.maxhenkel.voicechat.api.audiochannel.AudioChannel; import de.maxhenkel.voicechat.api.audiochannel.AudioPlayer; -import de.maxhenkel.voicechat.api.audiochannel.LocationalAudioChannel; import eu.m724.musicPlugin.Statics; import eu.m724.musicPlugin.file.AudioFile; @@ -45,13 +44,15 @@ public abstract class MusicPlayer { /* Playback control */ /** - * Starts playback of an audio file
- * If it's not loaded, it will be loaded synchronously. + * Sets audio file to play
+ * If it's not loaded, it will be loaded synchronously.
+ * The file is not rewinded. If you want to start from beginning, {@link MusicPlayer#seek(int)} + * After this, do {@link MusicPlayer#unpause()} * * @see MusicPlayer#pause() * @see MusicPlayer#stop() */ - public void play(AudioFile audioFile) { + public void setAudio(AudioFile audioFile) { if (audioFile != null) this.audioFile = audioFile; @@ -60,21 +61,23 @@ public abstract class MusicPlayer { if (!audioFile.isLoaded()) { try { + System.out.println("Audio not already loaded, so I load it"); audioFile.load(); } catch (IOException e) { throw new RuntimeException(e); } } + System.out.printf("Audio loaded it's %d seconds long, so I'm guessing %d frames\n", audioFile.getTrackDuration(), audioFile.getTrackDuration() * 50); + ready = true; - playing = true; unpause(); } public void unpause() { - if (!ready || !playing) return; - System.out.println("Unapised"); + if (!ready || playing) return; + System.out.println("playback unpaused"); var sa = new short[960]; var enc = audioFile.getEncoder(); @@ -84,11 +87,11 @@ public abstract class MusicPlayer { player.startPlaying(); // TODO this may take a long time and it might glitch if it does - onAction.accept(ready ? TrackAction.UNPAUSE : TrackAction.START); + onAction.accept(TrackAction.UNPAUSE); - playing = false; + playing = true; - System.out.println("unpaused"); + System.out.println("playback unpaused now"); } /** @@ -123,8 +126,8 @@ public abstract class MusicPlayer { private void stopPlayback(boolean pause) { - System.out.println("psued"); - playing = pause; + System.out.println("playback stopped"); + playing = false; ready = pause; player.stopPlaying(); @@ -134,9 +137,10 @@ public abstract class MusicPlayer { } private void onStop() { - if (playing) { // paused + if (ready && !playing) { // paused System.out.println("I detected pause"); onAction.accept(TrackAction.PAUSE); + playing = false; } else if (ready) { // not paused and still playing System.out.println("I detected end"); onAction.accept(TrackAction.DURATION); @@ -148,10 +152,6 @@ public abstract class MusicPlayer { } public enum TrackAction { - /** - * A new track started playing - */ - START, /** * Song stopped playing because it played */