Adds sound effects to Blacksmiths and Scrappers

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

View File

@ -24,6 +24,7 @@ public interface Settings<K extends Setting> {
* @param setting <p>The setting to get</p>
* @return <p>The current raw setting value</p>
*/
@NotNull Object getRawValue(@NotNull K setting);
@NotNull
Object getRawValue(@NotNull K setting);
}

View File

@ -9,6 +9,7 @@ import net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Registry;
import org.bukkit.Sound;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
@ -30,7 +31,6 @@ import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.send
public class ReforgeSession extends Session implements Runnable {
private final BlacksmithTrait blacksmithTrait;
private final NPC npc;
private final ItemStack itemToReforge;
private final BlacksmithNPCSettings config;
private static List<String> enchantments = null;
@ -45,9 +45,8 @@ public class ReforgeSession extends Session implements Runnable {
*/
ReforgeSession(@NotNull BlacksmithTrait blacksmithTrait, @NotNull Player player, @NotNull NPC npc,
@NotNull BlacksmithNPCSettings config) {
super(player);
super(player, npc);
this.blacksmithTrait = blacksmithTrait;
this.npc = npc;
this.itemToReforge = player.getInventory().getItemInMainHand();
this.config = config;
@ -97,7 +96,9 @@ public class ReforgeSession extends Session implements Runnable {
*/
@Override
public void run() {
sendNPCMessage(this.npc, this.player, reforgeItem() ? this.config.getSuccessMessage() : this.config.getFailMessage());
boolean success = reforgeItem();
sendNPCMessage(this.npc, this.player, success ? this.config.getSuccessMessage() : this.config.getFailMessage());
playSound(success ? Sound.BLOCK_ANVIL_USE : Sound.ENTITY_VILLAGER_NO);
//Stop the reforged item from displaying in the blacksmith's hand
if (this.npc.getEntity() instanceof Player) {

View File

@ -6,6 +6,7 @@ import net.knarcraft.blacksmith.config.scrapper.ScrapperNPCSettings;
import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.util.ItemHelper;
import net.knarcraft.blacksmith.util.SalvageHelper;
import org.bukkit.Sound;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@ -25,7 +26,6 @@ import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.send
public class SalvageSession extends Session implements Runnable {
private final ScrapperTrait scrapperTrait;
private final NPC npc;
private final ItemStack itemToSalvage;
private final ScrapperNPCSettings config;
private final List<ItemStack> salvage;
@ -47,9 +47,8 @@ public class SalvageSession extends Session implements Runnable {
SalvageSession(@NotNull ScrapperTrait scrapperTrait, @NotNull Player player, @NotNull NPC npc,
@NotNull ScrapperNPCSettings config, @NotNull List<ItemStack> salvage,
@NotNull SalvageMethod salvageMethod, int itemsConsumed) {
super(player);
super(player, npc);
this.scrapperTrait = scrapperTrait;
this.npc = npc;
this.itemToSalvage = player.getInventory().getItemInMainHand().clone();
this.config = config;
this.salvage = salvage;
@ -113,8 +112,10 @@ public class SalvageSession extends Session implements Runnable {
*/
private void salvageItem() {
if (random.nextInt(100) < this.config.getFailChance()) {
playSound(Sound.ENTITY_VILLAGER_NO);
failSalvage();
} else {
playSound(Sound.BLOCK_ANVIL_USE);
giveSalvage();
sendNPCMessage(this.npc, this.player, this.config.getSuccessMessage());
}

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);
}
}
}

View File

@ -105,6 +105,9 @@ public final class ItemHelper {
public static @NotNull Set<Material> getAllReforgeAbleMaterials() {
Set<Material> reforgeAbleMaterials = new HashSet<>();
for (Material material : Material.values()) {
if (!material.isItem()) {
continue;
}
ItemStack item = new ItemStack(material);
if (isRepairable(item)) {
reforgeAbleMaterials.add(material);