Makes sure music is stopped immediately if an NPC is removed

This commit is contained in:
Kristian Knarvik 2022-10-30 21:18:27 +01:00
parent 9d7a9117be
commit 9db2f871c6
3 changed files with 47 additions and 27 deletions

View File

@ -3,7 +3,7 @@ package net.knarcraft.minstrel;
import net.citizensnpcs.api.CitizensAPI;
import net.knarcraft.minstrel.command.MinstrelCommand;
import net.knarcraft.minstrel.command.MinstrelTabCompleter;
import net.knarcraft.minstrel.listener.PlayerListener;
import net.knarcraft.minstrel.listener.MinstrelListener;
import net.knarcraft.minstrel.trait.MinstrelTrait;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
@ -32,7 +32,7 @@ public final class MinstrelPlugin extends JavaPlugin {
net.citizensnpcs.api.trait.TraitInfo.create(MinstrelTrait.class).withName("minstrel"));
PluginManager pluginManager = Bukkit.getPluginManager();
pluginManager.registerEvents(new PlayerListener(), this);
pluginManager.registerEvents(new MinstrelListener(), this);
PluginCommand minstrelCommand = this.getCommand("minstrel");
if (minstrelCommand != null) {

View File

@ -0,0 +1,45 @@
package net.knarcraft.minstrel.listener;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.NPCRemoveEvent;
import net.citizensnpcs.api.npc.NPC;
import net.knarcraft.minstrel.trait.MinstrelTrait;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
/**
* A listener for minstrel-relate events
*/
public class MinstrelListener implements Listener {
/**
* Listens for any players joining, and makes all minstrels play for them
*
* @param event <p>The triggered event</p>
*/
@EventHandler
public void playerJoinListener(PlayerJoinEvent event) {
for (NPC npc : CitizensAPI.getNPCRegistry()) {
if (npc.hasTrait(MinstrelTrait.class)) {
MinstrelTrait minstrelTrait = npc.getTraitNullable(MinstrelTrait.class);
minstrelTrait.getPlaylist().play(minstrelTrait, event.getPlayer());
}
}
}
/**
* Listens for the deletion of any NPCs, and stops their music
*
* @param event <p>The triggered event</p>
*/
@EventHandler
public void npcDeletionListener(NPCRemoveEvent event) {
NPC npc = event.getNPC();
if (npc.hasTrait(MinstrelTrait.class)) {
MinstrelTrait minstrelTrait = npc.getTraitNullable(MinstrelTrait.class);
minstrelTrait.getPlaylist().stop();
}
}
}

View File

@ -1,25 +0,0 @@
package net.knarcraft.minstrel.listener;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.knarcraft.minstrel.trait.MinstrelTrait;
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)) {
MinstrelTrait minstrelTrait = npc.getTraitNullable(MinstrelTrait.class);
minstrelTrait.getPlaylist().play(minstrelTrait, event.getPlayer());
}
}
}
}