Adds sound effects to Blacksmiths and Scrappers

This commit is contained in:
2024-07-28 12:48:45 +02:00
parent 92c1b96fba
commit 81dda85405
5 changed files with 47 additions and 9 deletions

View File

@@ -3,6 +3,9 @@ package net.knarcraft.blacksmith.trait;
import net.citizensnpcs.api.npc.NPC;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.Sound;
import org.bukkit.SoundCategory;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitScheduler;
@@ -18,6 +21,7 @@ public abstract class Session implements Runnable {
protected static final Random random = new Random();
protected final Player player;
protected final NPC npc;
protected long finishTime;
protected int taskId;
@@ -25,9 +29,11 @@ public abstract class Session implements Runnable {
* Instantiates a new session
*
* @param player <p>The player the session belongs to</p>
* @param npc <p>The NPC involved in the session</p>
*/
public Session(@NotNull Player player) {
public Session(@NotNull Player player, @NotNull NPC npc) {
this.player = player;
this.npc = npc;
}
/**
@@ -90,6 +96,10 @@ public abstract class Session implements Runnable {
int actionDelay = getActionDelay();
this.finishTime = System.currentTimeMillis() + (actionDelay * 1000L);
taskId = scheduler.scheduleSyncDelayedTask(BlacksmithPlugin.getInstance(), this, actionDelay * 20L);
int playWorkSound = scheduler.scheduleSyncRepeatingTask(BlacksmithPlugin.getInstance(),
this::playWorkSound, 20, 20);
scheduler.scheduleSyncDelayedTask(BlacksmithPlugin.getInstance(), () -> scheduler.cancelTask(playWorkSound),
(actionDelay * 20L) - 20);
}
/**
@@ -140,4 +150,26 @@ public abstract class Session implements Runnable {
*/
protected abstract int getActionDelay();
/**
* Plays an NPC sound
*
* @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);
}
}
/**
* 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);
}
}
}