Fix binding speaker and other errors

This commit is contained in:
Minecon724 2024-12-20 11:30:19 +01:00
commit 26963e32ca
No known key found for this signature in database
GPG key ID: 3CCC4D267742C8E8
3 changed files with 17 additions and 5 deletions

View file

@ -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
}
}
}

View file

@ -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;
});

View file

@ -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));
}
/* */