package net.knarcraft.minstrel; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.SoundCategory; import org.bukkit.entity.Player; /** * A representation of a song playable by a minstrel * *

Note: Any custom song has to be mono to fade out like a normal jukebox. If it's stereo, you'll end up with a * global song instead.

*/ public class Song { private final int durationSeconds; private final SoundCategory category; private final String sound; private boolean isPlaying = false; /** * Instantiates a new song * * @param category

The category the song belongs to

* @param sound

The song to use

* @param durationSeconds

The duration of the song, in seconds

*/ public Song(SoundCategory category, String sound, int durationSeconds) { this.category = category; this.sound = sound; this.durationSeconds = durationSeconds; } /** * Plays this song at the given minstrel's location, but only for the given player * *

Volume of 1 means the max volume, which can be heard up to 1 chunk away. Setting it to 2 means it can be * heard 2 chunks away.

* * @param trait

The minstrel to play this song

* @param player

The player to play this song for

* @param volume

The volume to play this song at

* @param pitch

The pitch to play this song at

*/ public void play(MinstrelTrait trait, Player player, float volume, float pitch) { play(player, trait.getLocation(), volume, pitch); } /** * Plays this song at the given minstrel's location * *

Volume of 1 means the max volume, which can be heard up to 1 chunk away. Setting it to 2 means it can be heard 2 chunks away.

* * @param trait

The minstrel to play this song

* @param volume

The volume to play this song at

* @param pitch

The pitch to play this song at

*/ public void play(MinstrelTrait trait, float volume, float pitch) { for (Player player : Bukkit.getOnlinePlayers()) { play(player, trait.getLocation(), volume, pitch); } this.isPlaying = true; //Mark this song as ended, once Bukkit.getScheduler().runTaskLater(MinstrelPlugin.getInstance(), () -> this.isPlaying = false, durationSeconds * 20L); } /** * Gets whether this song is currently playing * * @return

True if this song is currently playing

*/ public boolean isPlaying() { return this.isPlaying; } /** * Gets the duration of this song, in seconds * * @return

The duration of this song

*/ public int getDuration() { return durationSeconds; } /** * Stops this song for the given player * * @param player

The player to stop this song for

*/ public void stop(Player player) { if (this.category == null) { player.stopSound(this.sound); } else { player.stopSound(this.sound, this.category); } } /** * Plays this song for the given player, at the given location * * @param player

The player to play this song for

* @param location

The location to play the song at

* @param volume

The volume to play at

* @param pitch

The pitch to play at

*/ private void play(Player player, Location location, float volume, float pitch) { if (this.category == null) { player.playSound(location, this.sound, volume, pitch); } else { player.playSound(location, this.sound, this.category, volume, pitch); } } }