Jukebox updates and Presistent Data Containers

The jukebox now stops the vanilla sound!
The plugin now uses a presistent data container to store the music disc.
This commit is contained in:
Navoei
2022-07-11 20:12:28 -05:00
parent 0c0307607c
commit 53f1396738
2 changed files with 77 additions and 31 deletions

View File

@@ -17,7 +17,6 @@ processResources {
} }
dependencies { dependencies {
compileOnly group: "com.comphenix.protocol", name: "ProtocolLib", version: "5.0.0-SNAPSHOT"
implementation 'com.google.code.findbugs:jsr305:3.0.2' implementation 'com.google.code.findbugs:jsr305:3.0.2'

View File

@@ -7,22 +7,23 @@ import me.Navoei.customdiscsplugin.CustomDiscs;
import me.Navoei.customdiscsplugin.VoicePlugin; import me.Navoei.customdiscsplugin.VoicePlugin;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.ChatColor; import org.bukkit.*;
import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.block.Jukebox; import org.bukkit.block.Jukebox;
import org.bukkit.block.TileState;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioInputStream;
@@ -39,6 +40,7 @@ public class JukeBox implements Listener {
private final Map<UUID, AudioPlayer> playerMap = new ConcurrentHashMap<>(); private final Map<UUID, AudioPlayer> playerMap = new ConcurrentHashMap<>();
public static AudioFormat FORMAT = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 48000.0F, 16, 1, 2, 48000.0F, false); public static AudioFormat FORMAT = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 48000.0F, 16, 1, 2, 48000.0F, false);
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInsert(PlayerInteractEvent event) throws IOException { public void onInsert(PlayerInteractEvent event) throws IOException {
@@ -48,7 +50,16 @@ public class JukeBox implements Listener {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getClickedBlock() == null || event.getItem() == null || event.getItem().getItemMeta() == null || block == null) return; if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getClickedBlock() == null || event.getItem() == null || event.getItem().getItemMeta() == null || block == null) return;
if (event.getClickedBlock().getType() != Material.JUKEBOX) return; if (event.getClickedBlock().getType() != Material.JUKEBOX) return;
if (isCustomMusicDisc(event) && !jukeboxContainsDisc(block)) { if (isCustomMusicDisc(event) && !jukeboxContainsPersistentData(block)) {
TileState tileState = (TileState) block.getState();
PersistentDataContainer container = tileState.getPersistentDataContainer();
NamespacedKey key = new NamespacedKey(CustomDiscs.getInstance(),
"CustomDisc");
if (container.has(key, PersistentDataType.BYTE_ARRAY)) return;
event.setCancelled(true);
container.set(key, PersistentDataType.BYTE_ARRAY, event.getItem().serializeAsBytes());
tileState.update();
Component soundFileNameComponent = Objects.requireNonNull(event.getItem().getItemMeta().lore()).get(1).asComponent(); Component soundFileNameComponent = Objects.requireNonNull(event.getItem().getItemMeta().lore()).get(1).asComponent();
String soundFileName = PlainTextComponentSerializer.plainText().serialize(soundFileNameComponent); String soundFileName = PlainTextComponentSerializer.plainText().serialize(soundFileNameComponent);
@@ -59,16 +70,16 @@ public class JukeBox implements Listener {
if (soundFilePath.toFile().exists()) { if (soundFilePath.toFile().exists()) {
Component songNameComponent = Objects.requireNonNull(event.getItem().getItemMeta().lore()).get(0).asComponent(); //Component songNameComponent = Objects.requireNonNull(event.getItem().getItemMeta().lore()).get(0).asComponent();
String songName = PlainTextComponentSerializer.plainText().serialize(songNameComponent); //String songName = PlainTextComponentSerializer.plainText().serialize(songNameComponent);
LocationalAudioChannel audioChannel = VoicePlugin.voicechatServerApi.createLocationalAudioChannel(id, VoicePlugin.voicechatApi.fromServerLevel(block.getLocation().getWorld()), VoicePlugin.voicechatApi.createPosition(block.getLocation().getX() + 0.5d, block.getLocation().getY() + 0.5d, block.getLocation().getZ() + 0.5d)); LocationalAudioChannel audioChannel = VoicePlugin.voicechatServerApi.createLocationalAudioChannel(id, VoicePlugin.voicechatApi.fromServerLevel(block.getLocation().getWorld()), VoicePlugin.voicechatApi.createPosition(block.getLocation().getX() + 0.5d, block.getLocation().getY() + 0.5d, block.getLocation().getZ() + 0.5d));
try { try {
AudioPlayer audioPlayer = VoicePlugin.voicechatServerApi.createAudioPlayer((AudioChannel) audioChannel, VoicePlugin.voicechatApi.createEncoder(), readSoundFile(soundFilePath)); AudioPlayer audioPlayer = VoicePlugin.voicechatServerApi.createAudioPlayer(audioChannel, VoicePlugin.voicechatApi.createEncoder(), readSoundFile(soundFilePath));
playerMap.put(id, audioPlayer); playerMap.put(id, audioPlayer);
audioPlayer.startPlaying(); audioPlayer.startPlaying();
player.sendMessage(ChatColor.GOLD + "Now playing: " + songName); player.getInventory().setItem(Objects.requireNonNull(event.getHand()), null);
} catch (Exception e) { } catch (Exception e) {
player.sendMessage(ChatColor.RED + "An error occurred while trying to play the music!"); player.sendMessage(ChatColor.RED + "An error occurred while trying to play the music!");
} }
@@ -78,22 +89,36 @@ public class JukeBox implements Listener {
throw new FileNotFoundException("Sound file is missing!"); throw new FileNotFoundException("Sound file is missing!");
} }
} }
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEject(PlayerInteractEvent event) { public void onEject(PlayerInteractEvent event) {
Player player = event.getPlayer();
Block block = event.getClickedBlock(); Block block = event.getClickedBlock();
if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getClickedBlock() == null || block == null) return; if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getClickedBlock() == null || block == null) return;
if (event.getClickedBlock().getType() != Material.JUKEBOX) return; if (event.getClickedBlock().getType() != Material.JUKEBOX) return;
if (jukeboxContainsCustomDisc(block)) { UUID id = UUID.nameUUIDFromBytes(block.getLocation().toString().getBytes());
UUID id = UUID.nameUUIDFromBytes(block.getLocation().toString().getBytes()); //Spawn in item at the block position
TileState tileState = (TileState) block.getState();
PersistentDataContainer container = tileState.getPersistentDataContainer();
NamespacedKey key = new NamespacedKey(CustomDiscs.getInstance(),
"CustomDisc");
if (!container.has(key, PersistentDataType.BYTE_ARRAY)) return;
container.get(key, PersistentDataType.BYTE_ARRAY);
block.getWorld().dropItemNaturally(block.getLocation(), ItemStack.deserializeBytes(container.get(key, PersistentDataType.BYTE_ARRAY)));
container.remove(key);
tileState.update();
if (isAudioPlayerPlaying(playerMap, id)) {
player.swingMainHand();
stopAudioPlayer(playerMap, id); stopAudioPlayer(playerMap, id);
} }
} }
@@ -105,12 +130,23 @@ public class JukeBox implements Listener {
if (block.getType() != Material.JUKEBOX) return; if (block.getType() != Material.JUKEBOX) return;
if (jukeboxContainsCustomDisc(block)) { UUID id = UUID.nameUUIDFromBytes(block.getLocation().toString().getBytes());
UUID id = UUID.nameUUIDFromBytes(block.getLocation().toString().getBytes()); //Spawn in item at the block position
TileState tileState = (TileState) block.getState();
PersistentDataContainer container = tileState.getPersistentDataContainer();
NamespacedKey key = new NamespacedKey(CustomDiscs.getInstance(),
"CustomDisc");
if (!container.has(key, PersistentDataType.BYTE_ARRAY)) return;
container.get(key, PersistentDataType.BYTE_ARRAY);
stopAudioPlayer(playerMap, id); block.getWorld().dropItemNaturally(block.getLocation(), ItemStack.deserializeBytes(container.get(key, PersistentDataType.BYTE_ARRAY)));
container.remove(key);
tileState.update();
if (isAudioPlayerPlaying(playerMap, id)) {
stopAudioPlayer(playerMap, id);
} }
} }
@@ -120,8 +156,23 @@ public class JukeBox implements Listener {
for (Block explodedBlock : event.blockList()) { for (Block explodedBlock : event.blockList()) {
if (explodedBlock.getType() == Material.JUKEBOX) { if (explodedBlock.getType() == Material.JUKEBOX) {
if (jukeboxContainsCustomDisc(explodedBlock)) {
UUID id = UUID.nameUUIDFromBytes(explodedBlock.getLocation().toString().getBytes()); UUID id = UUID.nameUUIDFromBytes(explodedBlock.getLocation().toString().getBytes());
//Spawn in item at the block position
TileState tileState = (TileState) explodedBlock.getState();
PersistentDataContainer container = tileState.getPersistentDataContainer();
NamespacedKey key = new NamespacedKey(CustomDiscs.getInstance(),
"CustomDisc");
if (!container.has(key, PersistentDataType.BYTE_ARRAY)) return;
container.get(key, PersistentDataType.BYTE_ARRAY);
explodedBlock.getWorld().dropItemNaturally(explodedBlock.getLocation(), ItemStack.deserializeBytes(container.get(key, PersistentDataType.BYTE_ARRAY)));
container.remove(key);
tileState.update();
if (isAudioPlayerPlaying(playerMap, id)) {
stopAudioPlayer(playerMap, id); stopAudioPlayer(playerMap, id);
} }
} }
@@ -129,22 +180,18 @@ public class JukeBox implements Listener {
} }
private boolean jukeboxContainsDisc(Block b) { public boolean isAudioPlayerPlaying(Map<UUID, AudioPlayer> playerMap, UUID id) {
BlockState blockState = b.getState(); AudioPlayer audioPlayer = playerMap.get(id);
Jukebox jukebox = (Jukebox) blockState; if (audioPlayer == null) return false;
return audioPlayer.isPlaying();
return jukebox.getRecord().getType() != Material.AIR;
} }
private boolean jukeboxContainsCustomDisc(Block b) { private boolean jukeboxContainsPersistentData(Block b) {
BlockState blockState = b.getState(); TileState tileState = (TileState) b.getState();
Jukebox jukebox = (Jukebox) blockState; PersistentDataContainer container = tileState.getPersistentDataContainer();
NamespacedKey key = new NamespacedKey(CustomDiscs.getInstance(),
if (jukebox.getRecord().hasItemMeta()) { "CustomDisc");
return jukebox.getRecord().getItemMeta().hasItemFlag(ItemFlag.HIDE_ENCHANTS); return container.has(key, PersistentDataType.BYTE_ARRAY);
} else {
return false;
}
} }
public static short[] readSoundFile(Path file) throws UnsupportedAudioFileException, IOException { public static short[] readSoundFile(Path file) throws UnsupportedAudioFileException, IOException {