Adds some events that trigger when NPC actions start or end, and when sounds are played

This commit is contained in:
2024-07-28 17:12:26 +02:00
parent 81dda85405
commit 904adf46f0
20 changed files with 571 additions and 10 deletions

View File

@@ -2,10 +2,14 @@ package net.knarcraft.blacksmith.trait;
import net.citizensnpcs.api.npc.NPC;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.event.BlacksmithReforgeStartEvent;
import net.knarcraft.blacksmith.event.NPCSoundEvent;
import net.knarcraft.blacksmith.event.ScrapperSalvageStartEvent;
import net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.Sound;
import org.bukkit.SoundCategory;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitScheduler;
@@ -95,6 +99,13 @@ public abstract class Session implements Runnable {
BukkitScheduler scheduler = BlacksmithPlugin.getInstance().getServer().getScheduler();
int actionDelay = getActionDelay();
this.finishTime = System.currentTimeMillis() + (actionDelay * 1000L);
if (this instanceof ReforgeSession) {
BlacksmithPlugin.getInstance().callEvent(new BlacksmithReforgeStartEvent(npc, player, this.finishTime));
} else if (this instanceof SalvageSession) {
BlacksmithPlugin.getInstance().callEvent(new ScrapperSalvageStartEvent(npc, player, this.finishTime));
}
taskId = scheduler.scheduleSyncDelayedTask(BlacksmithPlugin.getInstance(), this, actionDelay * 20L);
int playWorkSound = scheduler.scheduleSyncRepeatingTask(BlacksmithPlugin.getInstance(),
this::playWorkSound, 20, 20);
@@ -156,20 +167,37 @@ public abstract class Session implements Runnable {
* @param sound <p>The sound to play</p>
*/
protected void playSound(Sound sound) {
World world = this.npc.getEntity().getLocation().getWorld();
if (world != null) {
world.playSound(this.npc.getEntity(), sound, SoundCategory.AMBIENT, 0.5F, 1.0F);
}
playSound(this.npc.getEntity(), sound, 1.0F);
}
/**
* Plays the working NPC sound
*/
protected void playWorkSound() {
World world = this.npc.getEntity().getLocation().getWorld();
if (world != null) {
world.playSound(this.npc.getEntity(), Sound.BLOCK_PACKED_MUD_BREAK, SoundCategory.AMBIENT, 0.5F, (float) 0.5);
playSound(this.npc.getEntity(), Sound.ITEM_ARMOR_EQUIP_NETHERITE, 0.5f);
}
/**
* Plays a npc sound using a cancellable event
*
* @param entity <p>The entity that should play the sound</p>
* @param sound <p>The sound to play</p>
* @param pitch <p>The pitch to play the sound at</p>
*/
private void playSound(@NotNull Entity entity, @NotNull Sound sound, float pitch) {
World world = entity.getLocation().getWorld();
if (world == null) {
return;
}
NPCSoundEvent event = new NPCSoundEvent(this.npc, this.player, SoundCategory.AMBIENT, sound, 0.5f, pitch);
BlacksmithPlugin.getInstance().callEvent(event);
if (event.isCancelled()) {
return;
}
world.playSound(event.getNpc().getEntity(), event.getSound(), event.getSoundCategory(), event.getVolume(),
event.getPitch());
}
}