package net.knarcraft.minstrel.trait; import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.util.DataKey; import net.knarcraft.minstrel.MinstrelPlugin; import net.knarcraft.minstrel.music.Playlist; import net.knarcraft.minstrel.music.Song; import org.bukkit.Location; import org.bukkit.SoundCategory; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; /** * The minstrel trait itself, which contains all NPC data for this trait */ public class MinstrelTrait extends Trait { private final Playlist playlist = new Playlist(new ArrayList<>()); private float volume = 1F; private float pitch = 1F; /** * Instantiates a new minstrel trait */ public MinstrelTrait() { super("minstrel"); } /** * Gets the location of this minstrel * * @return

The location of this minstrel

*/ @NotNull public Location getLocation() { return this.getNPC().getStoredLocation(); } /** * Loads all config values stored in citizens' config file for this NPC * * @param key

The data key used for the config root

*/ @Override public void load(@NotNull DataKey key) { this.playlist.clear(); for (DataKey songKey : key.getRelative("playlist").getSubKeys()) { String category = songKey.getString("category"); SoundCategory soundCategory; if (category.equalsIgnoreCase("null")) { soundCategory = null; } else { soundCategory = SoundCategory.valueOf(category); } String soundIdentifier = songKey.getString("identifier"); int songDuration = songKey.getInt("duration"); Song song = new Song(soundCategory, soundIdentifier, songDuration); this.playlist.addSong(song); } this.volume = (float) key.getDouble("volume", 1D); this.pitch = (float) key.getDouble("pitch", 1D); //Register the minstrel to allow stopping the playback later MinstrelPlugin.getInstance().addMinstrel(this); } /** * Saves all of this NPC's config values to citizens' config file * * @param key

The data key used for the config root

*/ @Override public void save(@NotNull DataKey key) { //Saves the songs in the playlist, the volume and the pitch key.setRaw("playlist", null); List songs = this.playlist.getSongs(); for (int i = 0; i < songs.size(); i++) { Song song = songs.get(i); String songKey = "playlist." + i; if (song.getCategory() == null) { key.setString(songKey + ".category", "null"); } else { key.setString(songKey + ".category", song.getCategory().name()); } key.setString(songKey + ".identifier", song.getSound()); key.setInt(songKey + ".duration", song.getDuration()); } key.setRaw("volume", this.getVolume()); key.setRaw("pitch", this.getPitch()); } /** * Gets the volume to use for this minstrel * * @return

The volume of this minstrel

*/ public float getVolume() { return this.volume; } /** * Gets the pitch to use for this minstrel * * @return

The pitch of this minstrel

*/ public float getPitch() { return this.pitch; } /** * Gets this minstrel's playlist * * @return

This minstrel's playlist

*/ @NotNull public Playlist getPlaylist() { return this.playlist; } /** * Sets the volume for this minstrel * * @param newVolume

The volume for this minstrel

*/ public void setVolume(float newVolume) { if (newVolume > 0) { this.volume = newVolume; } else { throw new IllegalArgumentException("Volume cannot be negative!"); } } /** * Sets the pitch for this minstrel * * @param newPitch

The new pitch for this minstrel

*/ public void setPitch(float newPitch) { if (newPitch > 0) { this.pitch = newPitch; } else { throw new IllegalArgumentException("Pitch cannot be negative!"); } } @Override public boolean equals(@Nullable Object other) { if (!(other instanceof MinstrelTrait minstrelTrait)) { return false; } return npc.equals(minstrelTrait.npc); } }