diff --git a/src/main/java/eu/m724/musicPlugin/MusicCommands.java b/src/main/java/eu/m724/musicPlugin/MusicCommands.java index 29e35c3..b14ba85 100644 --- a/src/main/java/eu/m724/musicPlugin/MusicCommands.java +++ b/src/main/java/eu/m724/musicPlugin/MusicCommands.java @@ -3,7 +3,6 @@ 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; import eu.m724.musicPlugin.player.StaticMusicPlayer; diff --git a/src/main/java/eu/m724/musicPlugin/MusicPlugin.java b/src/main/java/eu/m724/musicPlugin/MusicPlugin.java index 237f7e7..2309119 100644 --- a/src/main/java/eu/m724/musicPlugin/MusicPlugin.java +++ b/src/main/java/eu/m724/musicPlugin/MusicPlugin.java @@ -5,11 +5,14 @@ import de.maxhenkel.voicechat.api.VoicechatConnection; import de.maxhenkel.voicechat.api.VoicechatServerApi; import eu.m724.musicPlugin.file.AudioFileStorage; import eu.m724.musicPlugin.item.ItemEvents; +import eu.m724.musicPlugin.item.speaker.BlockChecker; import org.bukkit.plugin.java.JavaPlugin; public final class MusicPlugin extends JavaPlugin { @Override public void onEnable() { + Statics.plugin = this; + BukkitVoicechatService service = getServer().getServicesManager().load(BukkitVoicechatService.class); if (service != null) { @@ -33,6 +36,8 @@ public final class MusicPlugin extends JavaPlugin { getServer().getPluginManager().registerEvents(new ItemEvents(), this); + // TODO do this better, maybe along events + new BlockChecker().runTaskTimerAsynchronously(this, 0, 20); } private void onApiStarted(VoicechatServerApi api) { diff --git a/src/main/java/eu/m724/musicPlugin/Statics.java b/src/main/java/eu/m724/musicPlugin/Statics.java index f948bb3..55bda0c 100644 --- a/src/main/java/eu/m724/musicPlugin/Statics.java +++ b/src/main/java/eu/m724/musicPlugin/Statics.java @@ -4,5 +4,6 @@ import de.maxhenkel.voicechat.api.VoicechatServerApi; // TODO find a better way public class Statics { + public static MusicPlugin plugin; public static VoicechatServerApi api; } diff --git a/src/main/java/eu/m724/musicPlugin/file/NotEncoder.java b/src/main/java/eu/m724/musicPlugin/file/NotEncoder.java index 79a3527..9aaf626 100644 --- a/src/main/java/eu/m724/musicPlugin/file/NotEncoder.java +++ b/src/main/java/eu/m724/musicPlugin/file/NotEncoder.java @@ -18,6 +18,8 @@ public class NotEncoder implements OpusEncoder { i = Math.clamp(frame, 0, size); } + public int getFrame() { return i; } + public boolean hasRemaining() { return i < size; } diff --git a/src/main/java/eu/m724/musicPlugin/item/ItemEvents.java b/src/main/java/eu/m724/musicPlugin/item/ItemEvents.java index 160d037..9dbc809 100644 --- a/src/main/java/eu/m724/musicPlugin/item/ItemEvents.java +++ b/src/main/java/eu/m724/musicPlugin/item/ItemEvents.java @@ -3,12 +3,16 @@ package eu.m724.musicPlugin.item; import eu.m724.musicPlugin.item.speaker.BlockSpeaker; import eu.m724.musicPlugin.item.speaker.Speaker; import eu.m724.musicPlugin.player.MovingMusicPlayer; +import org.bukkit.GameMode; import org.bukkit.Material; import org.bukkit.Particle; +import org.bukkit.block.Block; import org.bukkit.entity.Item; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockDamageEvent; import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.event.entity.EntityDropItemEvent; import org.bukkit.event.entity.EntityPickupItemEvent; @@ -32,15 +36,18 @@ public class ItemEvents implements Listener { if (!event.getPlayer().isSneaking()) { if (right) { - System.out.println("nos higft + rmb next song"); - // TODO forward song + System.out.println("nos higft + rmb: next song"); + player.next(); } else { - System.out.println("no shift + lmb rpevous song"); - // TODO back song + System.out.println("no shift + lmb: rpevous song"); + player.prev(); } } else { if (right) { - System.out.println("shift + rmb bind spekar"); + System.out.println("shift + rmb: open inventory"); + // TODO do that + } else { + System.out.println("shift + lmb: bind spekar"); var block = event.getClickedBlock(); if (block != null) { @@ -51,11 +58,11 @@ public class ItemEvents implements Listener { player.setSpeaker(speaker); block.getWorld().spawnParticle(Particle.HAPPY_VILLAGER, block.getLocation(), 10); - event.setCancelled(true); // prevent break + if (event.getPlayer().getGameMode() == GameMode.CREATIVE) + event.setCancelled(true); // prevent accidental break } } } - // originally inventory was supposed to be here } } @@ -79,6 +86,7 @@ public class ItemEvents implements Listener { // TODO do somehting? } + @EventHandler public void onMove(InventoryMoveItemEvent event) { if (event.getDestination().getType() == InventoryType.PLAYER) { var player = PortableMediaPlayers.get(event.getItem()); diff --git a/src/main/java/eu/m724/musicPlugin/item/PlayedSong.java b/src/main/java/eu/m724/musicPlugin/item/PlayedSong.java index 2d59509..07af38e 100644 --- a/src/main/java/eu/m724/musicPlugin/item/PlayedSong.java +++ b/src/main/java/eu/m724/musicPlugin/item/PlayedSong.java @@ -18,6 +18,10 @@ public class PlayedSong { return (int) (System.currentTimeMillis() - started) + progress; } + public void hint(int frame) { + this.progress = frame * 20; + } + public void pause() { this.progress = getProgress(); this.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 9f47e50..48851f2 100644 --- a/src/main/java/eu/m724/musicPlugin/item/PortableMediaPlayer.java +++ b/src/main/java/eu/m724/musicPlugin/item/PortableMediaPlayer.java @@ -56,7 +56,8 @@ public class PortableMediaPlayer { speaker.setDestroyCallback(c -> { System.out.println("spekar rip"); - played.pause(); + played.pause(); // this one stops the tracker not playback, which is stopped in Speaker + this.speaker = null; }); this.speaker = speaker; @@ -76,6 +77,7 @@ public class PortableMediaPlayer { // track paused System.out.println("Okay its paused"); played.pause(); + played.hint(played.file.getEncoder().getFrame()); } else if (action == MusicPlayer.TrackAction.UNPAUSE) { // track unpaused System.out.println("Okay its unpaused"); played.unpause(); @@ -115,6 +117,12 @@ public class PortableMediaPlayer { this.played = null; } + void prev() { + System.out.println("pmp prev"); + this.played = new PlayedSong(played.file); + unpause(); + } + void next() { System.out.println("pmp next"); this.played = null; diff --git a/src/main/java/eu/m724/musicPlugin/item/Speakers.java b/src/main/java/eu/m724/musicPlugin/item/Speakers.java deleted file mode 100644 index e737fd0..0000000 --- a/src/main/java/eu/m724/musicPlugin/item/Speakers.java +++ /dev/null @@ -1,10 +0,0 @@ -package eu.m724.musicPlugin.item; - -import eu.m724.musicPlugin.item.speaker.Speaker; - -import java.util.HashMap; -import java.util.Map; - -public class Speakers { - static Map speakers = new HashMap<>(); -} diff --git a/src/main/java/eu/m724/musicPlugin/item/speaker/BlockChecker.java b/src/main/java/eu/m724/musicPlugin/item/speaker/BlockChecker.java new file mode 100644 index 0000000..79d189b --- /dev/null +++ b/src/main/java/eu/m724/musicPlugin/item/speaker/BlockChecker.java @@ -0,0 +1,12 @@ +package eu.m724.musicPlugin.item.speaker; + +import org.bukkit.scheduler.BukkitRunnable; + +import java.util.HashSet; + +public class BlockChecker extends BukkitRunnable { + @Override + public void run() { + new HashSet<>(BlockSpeaker.speakers.values()).forEach(BlockSpeaker::check); + } +} 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 43389af..dde461c 100644 --- a/src/main/java/eu/m724/musicPlugin/item/speaker/BlockSpeaker.java +++ b/src/main/java/eu/m724/musicPlugin/item/speaker/BlockSpeaker.java @@ -1,15 +1,22 @@ package eu.m724.musicPlugin.item.speaker; +import eu.m724.musicPlugin.Statics; import eu.m724.musicPlugin.player.MovingMusicPlayer; import eu.m724.musicPlugin.player.MusicPlayer; import eu.m724.musicPlugin.player.StaticMusicPlayer; import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.metadata.MetadataValue; +import org.bukkit.metadata.MetadataValueAdapter; +import org.checkerframework.checker.units.qual.N; import java.util.HashMap; import java.util.Map; public class BlockSpeaker extends Speaker { - private static final Map speakers = new HashMap<>(); + static final Map speakers = new HashMap<>(); public static BlockSpeaker get(Location location) { return speakers.get(location); @@ -28,10 +35,21 @@ public class BlockSpeaker extends Speaker { public BlockSpeaker(Location location) { super(new StaticMusicPlayer(location)); this.location = location; + this.location.getBlock().setMetadata("t_speaker", new FixedMetadataValue(Statics.plugin, true)); } @Override void onDestroy() { speakers.remove(location); } + + public boolean check() { + if (location.getBlock().getType() == Material.NOTE_BLOCK && location.getBlock().hasMetadata("t_speaker")) { + return true; + } else { + System.out.println("Speaker disaper"); + destroy(); + return false; + } + } }