diff --git a/src/main/java/eu/m724/musicPlugin/item/ItemEvents.java b/src/main/java/eu/m724/musicPlugin/item/ItemEvents.java index 9dbc809..66fcb6f 100644 --- a/src/main/java/eu/m724/musicPlugin/item/ItemEvents.java +++ b/src/main/java/eu/m724/musicPlugin/item/ItemEvents.java @@ -52,14 +52,20 @@ public class ItemEvents implements Listener { var block = event.getClickedBlock(); if (block != null) { if (block.getType() == Material.NOTE_BLOCK) { + if (event.getPlayer().getGameMode() == GameMode.CREATIVE) + event.setCancelled(true); // prevent accidental break + var location = block.getLocation().add(0.5, 0.5, 0.5); var speaker = BlockSpeaker.create(location); + if (speaker == null) { + event.getPlayer().sendMessage("This speaker is occupied"); + return; + } + player.setSpeaker(speaker); block.getWorld().spawnParticle(Particle.HAPPY_VILLAGER, block.getLocation(), 10); - if (event.getPlayer().getGameMode() == GameMode.CREATIVE) - event.setCancelled(true); // prevent accidental break } } } diff --git a/src/main/java/eu/m724/musicPlugin/item/PortableMediaPlayer.java b/src/main/java/eu/m724/musicPlugin/item/PortableMediaPlayer.java index 48851f2..2d01d84 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(); // this one stops the tracker not playback, which is stopped in Speaker + if (played != null) + played.pause(); // this one stops the tracker not playback, which is stopped in Speaker this.speaker = null; }); 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 6e38430..f167c96 100644 --- a/src/main/java/eu/m724/musicPlugin/item/speaker/BlockSpeaker.java +++ b/src/main/java/eu/m724/musicPlugin/item/speaker/BlockSpeaker.java @@ -19,9 +19,14 @@ public class BlockSpeaker extends Speaker { } public static BlockSpeaker create(Location location) { - if (speakers.containsKey(location)) + var below = location.clone().subtract(0, 1, 0); + + if (speakers.containsKey(location) || speakers.containsKey(below)) return null; - return speakers.compute(location, (k, v) -> new BlockSpeaker(location)); + + var isTopSpeaker = below.getBlock().getType() == Material.NOTE_BLOCK; + System.out.println("SPekaer is top: " + isTopSpeaker); + return speakers.compute(isTopSpeaker ? below : location, (k, v) -> new BlockSpeaker(k)); } /* */