EpicKnarvik97 353366559f
All checks were successful
KnarCraft/Minstrel/pipeline/head This commit looks good
Updates dependencies and adds annotations
2024-05-11 16:53:52 +02:00

159 lines
4.5 KiB
Java

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 <p>The location of this minstrel</p>
*/
@NotNull
public Location getLocation() {
return this.getNPC().getStoredLocation();
}
/**
* Loads all config values stored in citizens' config file for this NPC
*
* @param key <p>The data key used for the config root</p>
*/
@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 <p>The data key used for the config root</p>
*/
@Override
public void save(@NotNull DataKey key) {
//Saves the songs in the playlist, the volume and the pitch
key.setRaw("playlist", null);
List<Song> 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 <p>The volume of this minstrel</p>
*/
public float getVolume() {
return this.volume;
}
/**
* Gets the pitch to use for this minstrel
*
* @return <p>The pitch of this minstrel</p>
*/
public float getPitch() {
return this.pitch;
}
/**
* Gets this minstrel's playlist
*
* @return <p>This minstrel's playlist</p>
*/
@NotNull
public Playlist getPlaylist() {
return this.playlist;
}
/**
* Sets the volume for this minstrel
*
* @param newVolume <p>The volume for this minstrel</p>
*/
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 <p>The new pitch for this minstrel</p>
*/
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);
}
}