From 13670e3ed9200f775e63ddf873ff533e12f978d2 Mon Sep 17 00:00:00 2001 From: Navoei Date: Fri, 31 Mar 2023 15:42:15 -0500 Subject: [PATCH] Added download command class. The action bar now shows for all players in range of a jukebox. --- gradle.properties | 6 +- .../customdiscsplugin/HopperManager.java | 139 +++++++++++++----- .../customdiscsplugin/PlayerManager.java | 36 ++++- .../command/SubCommands/DownloadCommand.java | 5 + .../customdiscsplugin/event/JukeBox.java | 28 ++-- 5 files changed, 152 insertions(+), 62 deletions(-) create mode 100644 src/main/java/me/Navoei/customdiscsplugin/command/SubCommands/DownloadCommand.java diff --git a/gradle.properties b/gradle.properties index a5ccb70..69a607d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,13 +4,13 @@ java_version=17 mp3spi_version=1.9.5.4 -bukkit_api_version=1.19 -bukkit_version=1.19-R0.1-SNAPSHOT +bukkit_api_version=1.19.4 +bukkit_version=1.19.4-R0.1-SNAPSHOT mod_id=customdiscsplugin # Target an older API to make it compatible with older versions of Simple Voice Chat voicechat_api_version=2.3.3 -plugin_version=2.2.3 +plugin_version=2.3 maven_group=me.Navoei.customdiscsplugin archives_base_name=custom-discs \ No newline at end of file diff --git a/src/main/java/me/Navoei/customdiscsplugin/HopperManager.java b/src/main/java/me/Navoei/customdiscsplugin/HopperManager.java index a7a2f63..9dc5718 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/HopperManager.java +++ b/src/main/java/me/Navoei/customdiscsplugin/HopperManager.java @@ -1,5 +1,9 @@ package me.Navoei.customdiscsplugin; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.NamespacedKey; @@ -24,6 +28,10 @@ import java.util.Objects; public class HopperManager implements Listener { + CustomDiscs customDiscs = CustomDiscs.getInstance(); + + PlayerManager playerManager = PlayerManager.instance(); + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onHopperPickupFromOtherSource(InventoryMoveItemEvent event) { @@ -47,16 +55,24 @@ public class HopperManager implements Listener { jukebox.setRecord(event.getItem()); jukebox.update(); - Bukkit.getScheduler().runTaskLater(CustomDiscs.getInstance(), () -> { + Bukkit.getScheduler().runTaskLater(customDiscs, () -> { hopper.getInventory().removeItem(event.getItem()); }, 1L); - String soundFileName = event.getItem().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(CustomDiscs.getInstance(), "customdisc"), PersistentDataType.STRING); + String soundFileName = event.getItem().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(customDiscs, "customdisc"), PersistentDataType.STRING); - Path soundFilePath = Path.of(CustomDiscs.getInstance().getDataFolder().getPath(), "musicdata", soundFileName); + Path soundFilePath = Path.of(customDiscs.getDataFolder().getPath(), "musicdata", soundFileName); + + Component songNameComponent = Objects.requireNonNull(event.getItem().getItemMeta().lore()).get(0).asComponent(); + String songName = PlainTextComponentSerializer.plainText().serialize(songNameComponent); + + TextComponent customActionBarSongPlaying = Component.text() + .content("Now Playing: " + songName) + .color(NamedTextColor.GOLD) + .build(); assert VoicePlugin.voicechatServerApi != null; - PlayerManager.instance().playLocationalAudio(VoicePlugin.voicechatServerApi, soundFilePath, null, jukebox.getBlock()); + playerManager.playLocationalAudio(VoicePlugin.voicechatServerApi, soundFilePath, jukebox.getBlock(), customActionBarSongPlaying.asComponent()); } @@ -82,16 +98,24 @@ public class HopperManager implements Listener { jukebox.setRecord(event.getItem().getItemStack()); jukebox.update(); - Bukkit.getScheduler().runTaskLater(CustomDiscs.getInstance(), () -> { + Component songNameComponent = Objects.requireNonNull(event.getItem().getItemStack().getItemMeta().lore()).get(0).asComponent(); + String songName = PlainTextComponentSerializer.plainText().serialize(songNameComponent); + + TextComponent customActionBarSongPlaying = Component.text() + .content("Now Playing: " + songName) + .color(NamedTextColor.GOLD) + .build(); + + Bukkit.getScheduler().runTaskLater(customDiscs, () -> { hopper.getInventory().removeItem(event.getItem().getItemStack()); }, 1L); - String soundFileName = jukebox.getRecord().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(CustomDiscs.getInstance(), "customdisc"), PersistentDataType.STRING); + String soundFileName = jukebox.getRecord().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(customDiscs, "customdisc"), PersistentDataType.STRING); - Path soundFilePath = Path.of(CustomDiscs.getInstance().getDataFolder().getPath(), "musicdata", soundFileName); + Path soundFilePath = Path.of(customDiscs.getDataFolder().getPath(), "musicdata", soundFileName); assert VoicePlugin.voicechatServerApi != null; - PlayerManager.instance().playLocationalAudio(VoicePlugin.voicechatServerApi, soundFilePath, null, jukebox.getBlock()); + playerManager.playLocationalAudio(VoicePlugin.voicechatServerApi, soundFilePath, jukebox.getBlock(), customActionBarSongPlaying.asComponent()); } @@ -106,7 +130,7 @@ public class HopperManager implements Listener { if (!event.getClickedInventory().getLocation().getBlock().getType().equals(Material.HOPPER)) return; - Bukkit.getScheduler().runTaskLater(CustomDiscs.getInstance(), () -> { + Bukkit.getScheduler().runTaskLater(customDiscs, () -> { Hopper hopperData = (Hopper) event.getInventory().getLocation().getBlock().getBlockData(); Block jukeboxBlock = event.getInventory().getLocation().getBlock().getRelative(hopperData.getFacing()); @@ -123,7 +147,7 @@ public class HopperManager implements Listener { if (!event.getInventory().getType().equals(InventoryType.HOPPER)) return; - Bukkit.getScheduler().runTaskLater(CustomDiscs.getInstance(), () -> { + Bukkit.getScheduler().runTaskLater(customDiscs, () -> { Hopper hopperData = (Hopper) event.getInventory().getLocation().getBlock().getBlockData(); Block jukeboxBlock = event.getInventory().getLocation().getBlock().getRelative(hopperData.getFacing()); @@ -138,7 +162,7 @@ public class HopperManager implements Listener { if (!event.getClickedInventory().getLocation().getBlock().getType().equals(Material.HOPPER)) return; - Bukkit.getScheduler().runTaskLater(CustomDiscs.getInstance(), () -> { + Bukkit.getScheduler().runTaskLater(customDiscs, () -> { Hopper hopperData = (Hopper) event.getInventory().getLocation().getBlock().getBlockData(); Block jukeboxBlock = event.getInventory().getLocation().getBlock().getRelative(hopperData.getFacing()); @@ -153,7 +177,7 @@ public class HopperManager implements Listener { if (!event.getClickedInventory().getLocation().getBlock().getType().equals(Material.HOPPER)) return; - Bukkit.getScheduler().runTaskLater(CustomDiscs.getInstance(), () -> { + Bukkit.getScheduler().runTaskLater(customDiscs, () -> { Hopper hopperData = (Hopper) event.getInventory().getLocation().getBlock().getBlockData(); Block jukeboxBlock = event.getInventory().getLocation().getBlock().getRelative(hopperData.getFacing()); @@ -168,7 +192,7 @@ public class HopperManager implements Listener { if (!event.getInventory().getType().equals(InventoryType.HOPPER)) return; - Bukkit.getScheduler().runTaskLater(CustomDiscs.getInstance(), () -> { + Bukkit.getScheduler().runTaskLater(customDiscs, () -> { Hopper hopperData = (Hopper) event.getInventory().getLocation().getBlock().getBlockData(); Block jukeboxBlock = event.getInventory().getLocation().getBlock().getRelative(hopperData.getFacing()); @@ -191,7 +215,7 @@ public class HopperManager implements Listener { if (event.getInventory().getHolder() instanceof HopperMinecart) return; - Bukkit.getScheduler().runTaskLater(CustomDiscs.getInstance(), () -> { + Bukkit.getScheduler().runTaskLater(customDiscs, () -> { Hopper hopperData = (Hopper) event.getInventory().getLocation().getBlock().getBlockData(); Block jukeboxBlock = event.getInventory().getLocation().getBlock().getRelative(hopperData.getFacing()); @@ -206,8 +230,8 @@ public class HopperManager implements Listener { public void onChunkLoad(ChunkLoadEvent event) { for (BlockState blockState : event.getChunk().getTileEntities()) { if (blockState instanceof Jukebox) { - if (!PlayerManager.instance().isAudioPlayerPlaying(blockState.getLocation())) { - itemJukeboxToHopper(blockState.getBlock()); + if (!playerManager.isAudioPlayerPlaying(blockState.getLocation())) { + discToHopper(blockState.getBlock()); } } } @@ -223,17 +247,18 @@ public class HopperManager implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onHopperPlace(BlockPlaceEvent event) { - if (!event.getBlock().getType().equals(Material.HOPPER)) return; - Block hopperBlock = event.getBlock(); + Block block = event.getBlock(); - if (!PlayerManager.instance().isAudioPlayerPlaying(hopperBlock.getRelative(BlockFace.UP).getLocation())) { - itemJukeboxToHopper(hopperBlock.getRelative(BlockFace.UP)); + if (!block.getType().equals(Material.HOPPER)) return; + + if (!playerManager.isAudioPlayerPlaying(block.getRelative(BlockFace.UP).getLocation())) { + discToHopper(block.getRelative(BlockFace.UP)); } } - public void itemJukeboxToHopper (Block block) { + public void discToHopper(Block block) { if (block == null) return; if (!block.getLocation().getChunk().isLoaded()) return; @@ -272,12 +297,20 @@ public class HopperManager implements Listener { jukebox.setRecord(hopper.getInventory().getItem(i)); jukebox.update(); - String soundFileName = jukebox.getRecord().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(CustomDiscs.getInstance(), "customdisc"), PersistentDataType.STRING); + String soundFileName = jukebox.getRecord().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(customDiscs, "customdisc"), PersistentDataType.STRING); - Path soundFilePath = Path.of(CustomDiscs.getInstance().getDataFolder().getPath(), "musicdata", soundFileName); + Path soundFilePath = Path.of(customDiscs.getDataFolder().getPath(), "musicdata", soundFileName); + + Component songNameComponent = Objects.requireNonNull(jukebox.getRecord().getItemMeta().lore()).get(0).asComponent(); + String songName = PlainTextComponentSerializer.plainText().serialize(songNameComponent); + + TextComponent customActionBarSongPlaying = Component.text() + .content("Now Playing: " + songName) + .color(NamedTextColor.GOLD) + .build(); assert VoicePlugin.voicechatServerApi != null; - PlayerManager.instance().playLocationalAudio(VoicePlugin.voicechatServerApi, soundFilePath, null, jukebox.getBlock()); + playerManager.playLocationalAudio(VoicePlugin.voicechatServerApi, soundFilePath, jukebox.getBlock(), customActionBarSongPlaying.asComponent()); hopper.getInventory().setItem(i, new ItemStack(Material.AIR)); return; @@ -297,12 +330,20 @@ public class HopperManager implements Listener { jukebox.setRecord(hopper.getInventory().getItem(i)); jukebox.update(); - String soundFileName = jukebox.getRecord().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(CustomDiscs.getInstance(), "customdisc"), PersistentDataType.STRING); + String soundFileName = jukebox.getRecord().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(customDiscs, "customdisc"), PersistentDataType.STRING); - Path soundFilePath = Path.of(CustomDiscs.getInstance().getDataFolder().getPath(), "musicdata", soundFileName); + Path soundFilePath = Path.of(customDiscs.getDataFolder().getPath(), "musicdata", soundFileName); + + Component songNameComponent = Objects.requireNonNull(jukebox.getRecord().getItemMeta().lore()).get(0).asComponent(); + String songName = PlainTextComponentSerializer.plainText().serialize(songNameComponent); + + TextComponent customActionBarSongPlaying = Component.text() + .content("Now Playing: " + songName) + .color(NamedTextColor.GOLD) + .build(); assert VoicePlugin.voicechatServerApi != null; - PlayerManager.instance().playLocationalAudio(VoicePlugin.voicechatServerApi, soundFilePath, null, jukebox.getBlock()); + playerManager.playLocationalAudio(VoicePlugin.voicechatServerApi, soundFilePath, jukebox.getBlock(), customActionBarSongPlaying.asComponent()); hopper.getInventory().setItem(i, new ItemStack(Material.AIR)); return; @@ -322,12 +363,20 @@ public class HopperManager implements Listener { jukebox.setRecord(hopper.getInventory().getItem(i)); jukebox.update(); - String soundFileName = jukebox.getRecord().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(CustomDiscs.getInstance(), "customdisc"), PersistentDataType.STRING); + String soundFileName = jukebox.getRecord().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(customDiscs, "customdisc"), PersistentDataType.STRING); - Path soundFilePath = Path.of(CustomDiscs.getInstance().getDataFolder().getPath(), "musicdata", soundFileName); + Path soundFilePath = Path.of(customDiscs.getDataFolder().getPath(), "musicdata", soundFileName); + + Component songNameComponent = Objects.requireNonNull(jukebox.getRecord().getItemMeta().lore()).get(0).asComponent(); + String songName = PlainTextComponentSerializer.plainText().serialize(songNameComponent); + + TextComponent customActionBarSongPlaying = Component.text() + .content("Now Playing: " + songName) + .color(NamedTextColor.GOLD) + .build(); assert VoicePlugin.voicechatServerApi != null; - PlayerManager.instance().playLocationalAudio(VoicePlugin.voicechatServerApi, soundFilePath, null, jukebox.getBlock()); + playerManager.playLocationalAudio(VoicePlugin.voicechatServerApi, soundFilePath, jukebox.getBlock(), customActionBarSongPlaying.asComponent()); hopper.getInventory().setItem(i, new ItemStack(Material.AIR)); return; @@ -347,12 +396,20 @@ public class HopperManager implements Listener { jukebox.setRecord(hopper.getInventory().getItem(i)); jukebox.update(); - String soundFileName = jukebox.getRecord().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(CustomDiscs.getInstance(), "customdisc"), PersistentDataType.STRING); + String soundFileName = jukebox.getRecord().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(customDiscs, "customdisc"), PersistentDataType.STRING); - Path soundFilePath = Path.of(CustomDiscs.getInstance().getDataFolder().getPath(), "musicdata", soundFileName); + Path soundFilePath = Path.of(customDiscs.getDataFolder().getPath(), "musicdata", soundFileName); + + Component songNameComponent = Objects.requireNonNull(jukebox.getRecord().getItemMeta().lore()).get(0).asComponent(); + String songName = PlainTextComponentSerializer.plainText().serialize(songNameComponent); + + TextComponent customActionBarSongPlaying = Component.text() + .content("Now Playing: " + songName) + .color(NamedTextColor.GOLD) + .build(); assert VoicePlugin.voicechatServerApi != null; - PlayerManager.instance().playLocationalAudio(VoicePlugin.voicechatServerApi, soundFilePath, null, jukebox.getBlock()); + playerManager.playLocationalAudio(VoicePlugin.voicechatServerApi, soundFilePath, jukebox.getBlock(), customActionBarSongPlaying.asComponent()); hopper.getInventory().setItem(i, new ItemStack(Material.AIR)); return; @@ -372,12 +429,20 @@ public class HopperManager implements Listener { jukebox.setRecord(hopper.getInventory().getItem(i)); jukebox.update(); - String soundFileName = jukebox.getRecord().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(CustomDiscs.getInstance(), "customdisc"), PersistentDataType.STRING); + String soundFileName = jukebox.getRecord().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(customDiscs, "customdisc"), PersistentDataType.STRING); - Path soundFilePath = Path.of(CustomDiscs.getInstance().getDataFolder().getPath(), "musicdata", soundFileName); + Path soundFilePath = Path.of(customDiscs.getDataFolder().getPath(), "musicdata", soundFileName); + + Component songNameComponent = Objects.requireNonNull(jukebox.getRecord().getItemMeta().lore()).get(0).asComponent(); + String songName = PlainTextComponentSerializer.plainText().serialize(songNameComponent); + + TextComponent customActionBarSongPlaying = Component.text() + .content("Now Playing: " + songName) + .color(NamedTextColor.GOLD) + .build(); assert VoicePlugin.voicechatServerApi != null; - PlayerManager.instance().playLocationalAudio(VoicePlugin.voicechatServerApi, soundFilePath, null, jukebox.getBlock()); + playerManager.playLocationalAudio(VoicePlugin.voicechatServerApi, soundFilePath, jukebox.getBlock(), customActionBarSongPlaying.asComponent()); hopper.getInventory().setItem(i, new ItemStack(Material.AIR)); return; @@ -391,7 +456,7 @@ public class HopperManager implements Listener { private boolean isCustomMusicDisc (ItemStack item) { - return item.getItemMeta().getPersistentDataContainer().has(new NamespacedKey(CustomDiscs.getInstance(), "customdisc"), PersistentDataType.STRING) && ( + return item.getItemMeta().getPersistentDataContainer().has(new NamespacedKey(customDiscs, "customdisc"), PersistentDataType.STRING) && ( item.getType().equals(Material.MUSIC_DISC_13) || item.getType().equals(Material.MUSIC_DISC_CAT) || item.getType().equals(Material.MUSIC_DISC_BLOCKS) || diff --git a/src/main/java/me/Navoei/customdiscsplugin/PlayerManager.java b/src/main/java/me/Navoei/customdiscsplugin/PlayerManager.java index a341a51..d9f7108 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/PlayerManager.java +++ b/src/main/java/me/Navoei/customdiscsplugin/PlayerManager.java @@ -1,13 +1,17 @@ package me.Navoei.customdiscsplugin; +import de.maxhenkel.voicechat.api.ServerPlayer; 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 javazoom.spi.mpeg.sampled.convert.MpegFormatConversionProvider; import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.entity.Player; @@ -18,7 +22,9 @@ import javax.annotation.Nullable; import javax.sound.sampled.*; import java.io.IOException; import java.nio.file.Path; +import java.util.Collection; import java.util.Map; +import java.util.Objects; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; @@ -30,7 +36,7 @@ public class PlayerManager { private final Map playerMap; private final ExecutorService executorService; - private static AudioFormat FORMAT = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 48000F, 16, 1, 2, 48000F, false); + private static final AudioFormat FORMAT = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 48000F, 16, 1, 2, 48000F, false); public PlayerManager() { this.playerMap = new ConcurrentHashMap<>(); @@ -41,7 +47,7 @@ public class PlayerManager { }); } - public void playLocationalAudio(VoicechatServerApi api, Path soundFilePath, Player bukkitPlayer, Block block) { + public void playLocationalAudio(VoicechatServerApi api, Path soundFilePath, Block block, Component actionbarComponent) { UUID id = UUID.nameUUIDFromBytes(block.getLocation().toString().getBytes()); LocationalAudioChannel audioChannel = api.createLocationalAudioChannel(id, api.fromServerLevel(block.getWorld()), api.createPosition(block.getLocation().getX() + 0.5d, block.getLocation().getY() + 0.5d, block.getLocation().getZ() + 0.5d)); @@ -65,7 +71,15 @@ public class PlayerManager { }); executorService.execute(() -> { - de.maxhenkel.voicechat.api.audiochannel.AudioPlayer audioPlayer = playChannel(api, bukkitPlayer, audioChannel, block, soundFilePath); + Collection playersInRange = api.getPlayersInRange(api.fromServerLevel(block.getWorld()), api.createPosition(block.getLocation().getX() + 0.5d, block.getLocation().getY() + 0.5d, block.getLocation().getZ() + 0.5d), CustomDiscs.getInstance().musicDiscDistance); + + de.maxhenkel.voicechat.api.audiochannel.AudioPlayer audioPlayer = playChannel(api, audioChannel, block, soundFilePath, playersInRange); + + for (ServerPlayer serverPlayer : playersInRange) { + Player bukkitPlayer = (Player) serverPlayer.getPlayer(); + bukkitPlayer.sendActionBar(actionbarComponent); + } + if (audioPlayer == null) { playerMap.remove(id); return; @@ -73,7 +87,7 @@ public class PlayerManager { audioPlayer.setOnStopped(() -> { - Bukkit.getScheduler().runTask(CustomDiscs.getInstance(), () -> HopperManager.instance().itemJukeboxToHopper(block)); + Bukkit.getScheduler().runTask(CustomDiscs.getInstance(), () -> HopperManager.instance().discToHopper(block)); playerMap.remove(id); }); @@ -89,7 +103,7 @@ public class PlayerManager { } @Nullable - private de.maxhenkel.voicechat.api.audiochannel.AudioPlayer playChannel(VoicechatServerApi api, Player bukkitPlayer, AudioChannel audioChannel, Block block, Path soundFilePath) { + private de.maxhenkel.voicechat.api.audiochannel.AudioPlayer playChannel(VoicechatServerApi api, AudioChannel audioChannel, Block block, Path soundFilePath, Collection playersInRange) { try { short[] audio = readSoundFile(soundFilePath); AudioPlayer audioPlayer = api.createAudioPlayer(audioChannel, api.createEncoder(), audio); @@ -98,8 +112,14 @@ public class PlayerManager { } catch (Exception e) { e.printStackTrace(); Bukkit.getLogger().info("Error Occurred At: " + block.getLocation()); - if (bukkitPlayer != null) { - bukkitPlayer.sendMessage(ChatColor.RED + "An error occurred while trying to play the music!"); + for (ServerPlayer serverPlayer : playersInRange) { + Player bukkitPlayer = (Player) serverPlayer.getPlayer(); + + TextComponent actionBarComponent = Component.text() + .content("An error has occurred while trying to play the music disc.") + .color(NamedTextColor.RED) + .build(); + bukkitPlayer.sendActionBar(actionBarComponent.asComponent()); } return null; } diff --git a/src/main/java/me/Navoei/customdiscsplugin/command/SubCommands/DownloadCommand.java b/src/main/java/me/Navoei/customdiscsplugin/command/SubCommands/DownloadCommand.java new file mode 100644 index 0000000..de689e2 --- /dev/null +++ b/src/main/java/me/Navoei/customdiscsplugin/command/SubCommands/DownloadCommand.java @@ -0,0 +1,5 @@ +package me.Navoei.customdiscsplugin.command.SubCommands; + +public class DownloadCommand { + +} diff --git a/src/main/java/me/Navoei/customdiscsplugin/event/JukeBox.java b/src/main/java/me/Navoei/customdiscsplugin/event/JukeBox.java index a2d1ea5..839c253 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/event/JukeBox.java +++ b/src/main/java/me/Navoei/customdiscsplugin/event/JukeBox.java @@ -13,19 +13,16 @@ import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.block.Block; -import org.bukkit.block.BlockState; import org.bukkit.block.Jukebox; -import org.bukkit.block.data.BlockData; -import org.bukkit.block.data.Directional; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.block.BlockRedstoneEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; import org.bukkit.persistence.PersistentDataType; import java.io.FileNotFoundException; @@ -35,6 +32,10 @@ import java.util.Objects; public class JukeBox implements Listener{ + CustomDiscs customDiscs = CustomDiscs.getInstance(); + PlayerManager playerManager = PlayerManager.instance(); + HopperManager hopperManager = HopperManager.instance(); + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onInsert(PlayerInteractEvent event) throws IOException { @@ -46,15 +47,12 @@ public class JukeBox implements Listener{ if (isCustomMusicDisc(event) && !jukeboxContainsDisc(block)) { - String soundFileName = event.getItem().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(CustomDiscs.getInstance(), "customdisc"), PersistentDataType.STRING); + String soundFileName = event.getItem().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(customDiscs, "customdisc"), PersistentDataType.STRING); - Path soundFilePath = Path.of(CustomDiscs.getInstance().getDataFolder().getPath(), "musicdata", soundFileName); + Path soundFilePath = Path.of(customDiscs.getDataFolder().getPath(), "musicdata", soundFileName); if (soundFilePath.toFile().exists()) { - assert VoicePlugin.voicechatServerApi != null; - PlayerManager.instance().playLocationalAudio(VoicePlugin.voicechatServerApi, soundFilePath, player, block); - Component songNameComponent = Objects.requireNonNull(event.getItem().getItemMeta().lore()).get(0).asComponent(); String songName = PlainTextComponentSerializer.plainText().serialize(songNameComponent); @@ -62,7 +60,9 @@ public class JukeBox implements Listener{ .content("Now Playing: " + songName) .color(NamedTextColor.GOLD) .build(); - player.sendActionBar(customActionBarSongPlaying.asComponent()); + + assert VoicePlugin.voicechatServerApi != null; + playerManager.playLocationalAudio(VoicePlugin.voicechatServerApi, soundFilePath, block, customActionBarSongPlaying.asComponent()); } else { player.sendMessage(ChatColor.RED + "Sound file not found."); @@ -82,10 +82,10 @@ public class JukeBox implements Listener{ if (event.getClickedBlock().getType() != Material.JUKEBOX) return; if (jukeboxContainsDisc(block)) { + event.setUseInteractedBlock(event.useInteractedBlock()); stopDisc(block, player); - Bukkit.getScheduler().runTaskLater(CustomDiscs.getInstance(), () -> HopperManager.instance().getNextDiscFromHopperIntoJukebox(block), 1L); + Bukkit.getScheduler().runTaskLater(customDiscs, () -> hopperManager.getNextDiscFromHopperIntoJukebox(block), 1L); } - } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @@ -119,7 +119,7 @@ public class JukeBox implements Listener{ if (e.getItem()==null) return false; - return e.getItem().getItemMeta().getPersistentDataContainer().has(new NamespacedKey(CustomDiscs.getInstance(), "customdisc")) && + return e.getItem().getItemMeta().getPersistentDataContainer().has(new NamespacedKey(customDiscs, "customdisc")) && ( e.getItem().getType().equals(Material.MUSIC_DISC_13) || e.getItem().getType().equals(Material.MUSIC_DISC_CAT) || @@ -140,7 +140,7 @@ public class JukeBox implements Listener{ } private void stopDisc(Block block, Player player) { - PlayerManager.instance().stopLocationalAudio(block.getLocation()); + playerManager.stopLocationalAudio(block.getLocation()); if (player == null) return; if (jukeboxContainsDisc(block)) { player.swingMainHand();