diff --git a/gradle.properties b/gradle.properties index 9701039..9f0c068 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,6 +11,6 @@ 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.3 +plugin_version=2.3.1 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/CustomDiscs.java b/src/main/java/me/Navoei/customdiscsplugin/CustomDiscs.java index 1beaee9..243a9cf 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/CustomDiscs.java +++ b/src/main/java/me/Navoei/customdiscsplugin/CustomDiscs.java @@ -12,15 +12,26 @@ import me.Navoei.customdiscsplugin.event.JukeBox; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.NamespacedKey; +import org.bukkit.block.BlockFace; +import org.bukkit.block.Hopper; import org.bukkit.block.Jukebox; +import org.bukkit.inventory.ItemStack; import org.bukkit.persistence.PersistentDataType; import org.bukkit.plugin.java.JavaPlugin; import javax.annotation.Nullable; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; import java.io.File; +import java.io.IOException; +import java.nio.file.Path; import java.util.Objects; +import static me.Navoei.customdiscsplugin.PlayerManager.getLengthSeconds; + public final class CustomDiscs extends JavaPlugin { public static final String PLUGIN_ID = "CustomDiscs"; @@ -75,9 +86,12 @@ public void onPacketSending(PacketEvent event) { if (!jukebox.getRecord().hasItemMeta()) return; if (jukebox.getRecord().getItemMeta().getPersistentDataContainer().has(new NamespacedKey(CustomDiscs.getInstance(), "customdisc"), PersistentDataType.STRING)) { + jukebox.stopPlaying(); event.setCancelled(true); } + //Spawn particles if there isnt any music playing at this location. + ParticleManager.start(jukebox); } } }); @@ -95,4 +109,5 @@ public void onDisable() { public static CustomDiscs getInstance() { return instance; } + } diff --git a/src/main/java/me/Navoei/customdiscsplugin/HopperManager.java b/src/main/java/me/Navoei/customdiscsplugin/HopperManager.java index b3b9c75..ffe3991 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/HopperManager.java +++ b/src/main/java/me/Navoei/customdiscsplugin/HopperManager.java @@ -4,17 +4,24 @@ 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; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.BlockState; +import org.bukkit.block.Jukebox; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryMoveItemEvent; import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.world.ChunkLoadEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.persistence.PersistentDataType; import java.nio.file.Path; +import java.util.Arrays; import java.util.Objects; public class HopperManager implements Listener { @@ -51,9 +58,48 @@ public void onJukeboxEjectToHopper(InventoryMoveItemEvent event) { if (event.getSource().getLocation() == null) return; if (!event.getSource().getType().equals(InventoryType.JUKEBOX)) return; + if (event.getItem().getItemMeta() == null) return; if (!isCustomMusicDisc(event.getItem())) return; event.setCancelled(playerManager.isAudioPlayerPlaying(event.getSource().getLocation())); + + } + + public void discToHopper(Block block) { + + if (block == null) return; + if (!block.getLocation().getChunk().isLoaded()) return; + if (!block.getType().equals(Material.JUKEBOX)) return; + if (!block.getRelative(BlockFace.DOWN).getType().equals(Material.HOPPER)) return; + + Block hopperBlock = block.getRelative(BlockFace.DOWN); + org.bukkit.block.Hopper hopper = (org.bukkit.block.Hopper) hopperBlock.getState(); + + Jukebox jukebox = (Jukebox) block.getState(); + + InventoryMoveItemEvent event = new InventoryMoveItemEvent(jukebox.getInventory(), jukebox.getRecord(), hopper.getInventory(), false); + Bukkit.getPluginManager().callEvent(event); + + if (!event.isCancelled()) { + if (!Arrays.toString(hopper.getInventory().getContents()).contains("null")) return; + + hopper.getInventory().setItem(hopper.getInventory().firstEmpty(), jukebox.getRecord()); + + block.setType(Material.AIR); + block.setType(Material.JUKEBOX); + } + + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onChunkLoad(ChunkLoadEvent event) { + for (BlockState blockState : event.getChunk().getTileEntities()) { + if (blockState instanceof Jukebox jukebox) { + if (!PlayerManager.instance().isAudioPlayerPlaying(blockState.getLocation()) && !jukebox.isPlaying()) { + discToHopper(blockState.getBlock()); + } + } + } } private boolean isCustomMusicDisc (ItemStack item) { diff --git a/src/main/java/me/Navoei/customdiscsplugin/ParticleManager.java b/src/main/java/me/Navoei/customdiscsplugin/ParticleManager.java new file mode 100644 index 0000000..2e8e9e1 --- /dev/null +++ b/src/main/java/me/Navoei/customdiscsplugin/ParticleManager.java @@ -0,0 +1,41 @@ +package me.Navoei.customdiscsplugin; + +import org.bukkit.Location; +import org.bukkit.Particle; +import org.bukkit.block.Jukebox; +import org.bukkit.inventory.ItemStack; +import org.bukkit.scheduler.BukkitRunnable; + +import java.util.HashMap; + +public class ParticleManager extends BukkitRunnable { + + static PlayerManager playerManager = PlayerManager.instance(); + static HashMap locationParticleManager = new HashMap<>(); + + public static void start(Jukebox jukebox) { + ParticleManager particleManager = new ParticleManager(); + particleManager.jukebox = jukebox; + if (locationParticleManager.containsKey(jukebox.getLocation())) return; + locationParticleManager.put(jukebox.getLocation(), particleManager); + locationParticleManager.get(jukebox.getLocation()).runTaskTimer(CustomDiscs.getInstance(), 0, 20); + } + + //private float seconds; + private Jukebox jukebox; + + + @Override + public void run() { + + if (!playerManager.isAudioPlayerPlaying(jukebox.getLocation())) { + locationParticleManager.remove(jukebox.getLocation()); + cancel(); + } else { + //if (!jukebox.isPlaying()) { + jukebox.getLocation().getWorld().spawnParticle(Particle.NOTE, jukebox.getLocation().add(0.5, 1.1, 0.5), 1); + //} + } + } + +} diff --git a/src/main/java/me/Navoei/customdiscsplugin/PlayerManager.java b/src/main/java/me/Navoei/customdiscsplugin/PlayerManager.java index aaa96b7..28f830a 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/PlayerManager.java +++ b/src/main/java/me/Navoei/customdiscsplugin/PlayerManager.java @@ -83,6 +83,9 @@ public void playLocationalAudio(VoicechatServerApi api, Path soundFilePath, Bloc } audioPlayer.setOnStopped(() -> { + + Bukkit.getScheduler().runTask(CustomDiscs.getInstance(), () -> HopperManager.instance().discToHopper(block)); + playerMap.remove(id); }); diff --git a/src/main/java/me/Navoei/customdiscsplugin/event/JukeBox.java b/src/main/java/me/Navoei/customdiscsplugin/event/JukeBox.java index a67baf7..96fb40a 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/event/JukeBox.java +++ b/src/main/java/me/Navoei/customdiscsplugin/event/JukeBox.java @@ -1,6 +1,7 @@ package me.Navoei.customdiscsplugin.event; import me.Navoei.customdiscsplugin.CustomDiscs; +import me.Navoei.customdiscsplugin.ParticleManager; import me.Navoei.customdiscsplugin.PlayerManager; import me.Navoei.customdiscsplugin.VoicePlugin; import net.kyori.adventure.text.Component;