Initial commit
This commit is contained in:
62
src/main/java/net/knarcraft/minstrel/MinstrelPlugin.java
Normal file
62
src/main/java/net/knarcraft/minstrel/MinstrelPlugin.java
Normal file
@ -0,0 +1,62 @@
|
||||
package net.knarcraft.minstrel;
|
||||
|
||||
import net.citizensnpcs.api.CitizensAPI;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.SoundCategory;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The main class of this plugin
|
||||
*/
|
||||
public final class MinstrelPlugin extends JavaPlugin {
|
||||
|
||||
private static MinstrelPlugin instance;
|
||||
private static Playlist testPlaylist;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Plugin startup logic
|
||||
instance = this;
|
||||
|
||||
//Register the blacksmith trait with Citizens
|
||||
CitizensAPI.getTraitFactory().registerTrait(
|
||||
net.citizensnpcs.api.trait.TraitInfo.create(MinstrelTrait.class).withName("minstrel"));
|
||||
|
||||
Song testSong = new Song(SoundCategory.RECORDS, "minecraft:records.custom.medieval_3_g_mixolydian", 114);
|
||||
List<Song> songs = new ArrayList<>();
|
||||
songs.add(testSong);
|
||||
testPlaylist = new Playlist(songs, true);
|
||||
|
||||
PluginManager pluginManager = Bukkit.getPluginManager();
|
||||
pluginManager.registerEvents(new PlayerListener(), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
// Plugin shutdown logic
|
||||
//TODO: Stop all songs in all playlists
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the playlist which is currently hard-coded for testing
|
||||
*
|
||||
* @return <p>The test playlist</p>
|
||||
*/
|
||||
public static Playlist getTestPlaylist() {
|
||||
return testPlaylist;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an instance of this plugin
|
||||
*
|
||||
* @return <p>An instance of this plugin</p>
|
||||
*/
|
||||
public static MinstrelPlugin getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
}
|
44
src/main/java/net/knarcraft/minstrel/MinstrelTrait.java
Normal file
44
src/main/java/net/knarcraft/minstrel/MinstrelTrait.java
Normal file
@ -0,0 +1,44 @@
|
||||
package net.knarcraft.minstrel;
|
||||
|
||||
import net.citizensnpcs.api.trait.Trait;
|
||||
import net.citizensnpcs.api.util.DataKey;
|
||||
import org.bukkit.Location;
|
||||
|
||||
/**
|
||||
* The minstrel trait itself, which contains all NPC data for this trait
|
||||
*/
|
||||
public class MinstrelTrait extends Trait {
|
||||
|
||||
public MinstrelTrait() {
|
||||
super("minstrel");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the location of this minstrel
|
||||
*
|
||||
* @return <p>The location of this minstrel</p>
|
||||
*/
|
||||
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(DataKey key) {
|
||||
//TODO: Actually load the playlist set to this minstrel
|
||||
MinstrelPlugin.getTestPlaylist().play(this);
|
||||
}
|
||||
|
||||
public float getVolume() {
|
||||
return 1F;
|
||||
}
|
||||
|
||||
public float getPitch() {
|
||||
return 1F;
|
||||
}
|
||||
|
||||
}
|
23
src/main/java/net/knarcraft/minstrel/PlayerListener.java
Normal file
23
src/main/java/net/knarcraft/minstrel/PlayerListener.java
Normal file
@ -0,0 +1,23 @@
|
||||
package net.knarcraft.minstrel;
|
||||
|
||||
import net.citizensnpcs.api.CitizensAPI;
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
/**
|
||||
* A listener for events where a player isn't hearing music when they should
|
||||
*/
|
||||
public class PlayerListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void playerJoinListener(PlayerJoinEvent event) {
|
||||
for (NPC npc : CitizensAPI.getNPCRegistry()) {
|
||||
if (npc.hasTrait(MinstrelTrait.class)) {
|
||||
MinstrelPlugin.getTestPlaylist().play(npc.getTraitNullable(MinstrelTrait.class), event.getPlayer());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
116
src/main/java/net/knarcraft/minstrel/Playlist.java
Normal file
116
src/main/java/net/knarcraft/minstrel/Playlist.java
Normal file
@ -0,0 +1,116 @@
|
||||
package net.knarcraft.minstrel;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A representation of a playlist containing songs
|
||||
*/
|
||||
public class Playlist {
|
||||
|
||||
private final List<Song> songs;
|
||||
private final boolean loop;
|
||||
private int currentlyPlaying = 0;
|
||||
private int schedulerId = 0;
|
||||
|
||||
/**
|
||||
* Instantiates a new playlist
|
||||
*
|
||||
* @param songs <p>The songs contained in this playlist</p>
|
||||
* @param loop <p>Whether to loop around when this playlist finishes</p>
|
||||
*/
|
||||
public Playlist(List<Song> songs, boolean loop) {
|
||||
this.songs = new ArrayList<>(songs);
|
||||
this.loop = loop;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a copy of the songs in this playlist
|
||||
*
|
||||
* @return <p>The songs in this playlist</p>
|
||||
*/
|
||||
public List<Song> getSongs() {
|
||||
return new ArrayList<>(this.songs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new song to this playlist
|
||||
*
|
||||
* @param song <p>The song to add</p>
|
||||
*/
|
||||
public void addSong(Song song) {
|
||||
this.songs.add(song);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a song from this playlist
|
||||
*
|
||||
* @param index <p>The index of the song to remove</p>
|
||||
*/
|
||||
public void removeSong(int index) {
|
||||
this.songs.remove(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays the current song for the given player
|
||||
*
|
||||
* <p>This should only be used if a player recently joined, or for some other reason cannot currently hear the
|
||||
* music.</p>
|
||||
*
|
||||
* @param trait <p>The the minstrel to play this playlist for</p>
|
||||
* @param player <p>The player to play to</p>
|
||||
*/
|
||||
public void play(MinstrelTrait trait, Player player) {
|
||||
//If this playlist is empty, do nothing
|
||||
if (this.songs.size() < 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Song song : this.songs) {
|
||||
song.stop(player);
|
||||
}
|
||||
|
||||
Song currentSong = this.songs.get(this.currentlyPlaying - 1);
|
||||
currentSong.play(trait, player, trait.getVolume(), trait.getPitch());
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays the next song in this playlist
|
||||
*
|
||||
* @param trait <p>The minstrel to play this playlist for</p>
|
||||
*/
|
||||
public void play(MinstrelTrait trait) {
|
||||
//If this playlist is empty, do nothing
|
||||
if (this.songs.size() < 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.currentlyPlaying >= songs.size()) {
|
||||
if (this.loop) {
|
||||
this.currentlyPlaying = 0;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
for (Song song : this.songs) {
|
||||
song.stop(player);
|
||||
}
|
||||
}
|
||||
|
||||
Song currentSong = this.songs.get(this.currentlyPlaying);
|
||||
currentSong.play(trait, trait.getVolume(), trait.getPitch());
|
||||
currentlyPlaying++;
|
||||
schedulerId = Bukkit.getScheduler().scheduleSyncRepeatingTask(MinstrelPlugin.getInstance(), () -> {
|
||||
if (!currentSong.isPlaying()) {
|
||||
Bukkit.getScheduler().cancelTask(schedulerId);
|
||||
play(trait);
|
||||
}
|
||||
}, currentSong.getDuration(), 20);
|
||||
}
|
||||
|
||||
}
|
115
src/main/java/net/knarcraft/minstrel/Song.java
Normal file
115
src/main/java/net/knarcraft/minstrel/Song.java
Normal file
@ -0,0 +1,115 @@
|
||||
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
|
||||
*
|
||||
* <p>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.</p>
|
||||
*/
|
||||
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 <p>The category the song belongs to</p>
|
||||
* @param sound <p>The song to use</p>
|
||||
* @param durationSeconds <p>The duration of the song, in seconds</p>
|
||||
*/
|
||||
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
|
||||
*
|
||||
* <p>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.</p>
|
||||
*
|
||||
* @param trait <p>The minstrel to play this song</p>
|
||||
* @param player <p>The player to play this song for</p>
|
||||
* @param volume <p>The volume to play this song at</p>
|
||||
* @param pitch <p>The pitch to play this song at</p>
|
||||
*/
|
||||
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
|
||||
*
|
||||
* <p>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.</p>
|
||||
*
|
||||
* @param trait <p>The minstrel to play this song</p>
|
||||
* @param volume <p>The volume to play this song at</p>
|
||||
* @param pitch <p>The pitch to play this song at</p>
|
||||
*/
|
||||
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 <p>True if this song is currently playing</p>
|
||||
*/
|
||||
public boolean isPlaying() {
|
||||
return this.isPlaying;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the duration of this song, in seconds
|
||||
*
|
||||
* @return <p>The duration of this song</p>
|
||||
*/
|
||||
public int getDuration() {
|
||||
return durationSeconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops this song for the given player
|
||||
*
|
||||
* @param player <p>The player to stop this song for</p>
|
||||
*/
|
||||
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 <p>The player to play this song for</p>
|
||||
* @param location <p>The location to play the song at</p>
|
||||
* @param volume <p>The volume to play at</p>
|
||||
* @param pitch <p>The pitch to play at</p>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user