From 904adf46f08fbbbbe586f53087b6006d851b922a Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sun, 28 Jul 2024 17:12:26 +0200 Subject: [PATCH] Adds some events that trigger when NPC actions start or end, and when sounds are played --- .../blacksmith/BlacksmithPlugin.java | 10 ++ .../config/blacksmith/BlacksmithSetting.java | 5 +- .../config/scrapper/ScrapperSetting.java | 2 +- .../event/AbstractBlacksmithPluginEvent.java | 40 +++++ .../blacksmith/event/ActionStartEvent.java | 18 +++ .../event/BlacksmithPluginEvent.java | 29 ++++ .../event/BlacksmithReforgeFailEvent.java | 41 +++++ .../event/BlacksmithReforgeStartEvent.java | 50 ++++++ .../event/BlacksmithReforgeSucceedEvent.java | 42 ++++++ .../blacksmith/event/NPCSoundEvent.java | 142 ++++++++++++++++++ .../blacksmith/event/ReforgeEndEvent.java | 7 + .../blacksmith/event/SalvageEndEvent.java | 7 + .../event/ScrapperSalvageFailEvent.java | 42 ++++++ .../event/ScrapperSalvageStartEvent.java | 51 +++++++ .../event/ScrapperSalvageSucceedEvent.java | 42 ++++++ .../blacksmith/trait/BlacksmithTrait.java | 1 + .../blacksmith/trait/ReforgeSession.java | 4 + .../blacksmith/trait/SalvageSession.java | 5 + .../knarcraft/blacksmith/trait/Session.java | 42 +++++- src/main/resources/plugin.yml | 1 + 20 files changed, 571 insertions(+), 10 deletions(-) create mode 100644 src/main/java/net/knarcraft/blacksmith/event/AbstractBlacksmithPluginEvent.java create mode 100644 src/main/java/net/knarcraft/blacksmith/event/ActionStartEvent.java create mode 100644 src/main/java/net/knarcraft/blacksmith/event/BlacksmithPluginEvent.java create mode 100644 src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeFailEvent.java create mode 100644 src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeStartEvent.java create mode 100644 src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeSucceedEvent.java create mode 100644 src/main/java/net/knarcraft/blacksmith/event/NPCSoundEvent.java create mode 100644 src/main/java/net/knarcraft/blacksmith/event/ReforgeEndEvent.java create mode 100644 src/main/java/net/knarcraft/blacksmith/event/SalvageEndEvent.java create mode 100644 src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageFailEvent.java create mode 100644 src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageStartEvent.java create mode 100644 src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageSucceedEvent.java diff --git a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java index df48a67..05ff3ff 100644 --- a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java +++ b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java @@ -32,6 +32,7 @@ import org.bukkit.command.PluginCommand; import org.bukkit.command.TabCompleter; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.event.Event; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -206,6 +207,15 @@ public class BlacksmithPlugin extends JavaPlugin { } } + /** + * Calls an event + * + * @param event

The event to call

+ */ + public void callEvent(@NotNull Event event) { + this.getServer().getPluginManager().callEvent(event); + } + /** * Initializes custom configuration and translation * diff --git a/src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithSetting.java b/src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithSetting.java index 5f35405..085e82b 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithSetting.java +++ b/src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithSetting.java @@ -241,7 +241,8 @@ public enum BlacksmithSetting implements Setting { * The cost for repairing a damaged anvil */ ANVIL_DAMAGED_COST("damagedAnvilReforgingCost", SettingValueType.POSITIVE_DOUBLE, 20.0, - "The cost of fully repairing a damaged anvil", false, false); + "The cost of fully repairing a damaged anvil", false, false), + ; private final String path; private final String childPath; @@ -262,7 +263,7 @@ public enum BlacksmithSetting implements Setting { * @param isPerNPC

Whether this setting is per-NPC or global

* @param isMessage

Whether this option is for an NPC message

*/ - BlacksmithSetting(@NotNull String key, @NotNull SettingValueType valueType, @NotNull Object value, + BlacksmithSetting(@NotNull String key, @NotNull SettingValueType valueType, @Nullable Object value, @NotNull String description, boolean isPerNPC, boolean isMessage) { if (isPerNPC) { if (isMessage) { diff --git a/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperSetting.java b/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperSetting.java index a315d6b..bfd6c4a 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperSetting.java +++ b/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperSetting.java @@ -302,7 +302,7 @@ public enum ScrapperSetting implements Setting { * @param isPerNPC

Whether this setting is per-NPC or global

* @param isMessage

Whether this option is for an NPC message

*/ - ScrapperSetting(@NotNull String key, @NotNull SettingValueType valueType, @NotNull Object value, + ScrapperSetting(@NotNull String key, @NotNull SettingValueType valueType, @Nullable Object value, @NotNull String description, boolean isPerNPC, boolean isMessage) { if (isPerNPC) { if (isMessage) { diff --git a/src/main/java/net/knarcraft/blacksmith/event/AbstractBlacksmithPluginEvent.java b/src/main/java/net/knarcraft/blacksmith/event/AbstractBlacksmithPluginEvent.java new file mode 100644 index 0000000..3087ae2 --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/event/AbstractBlacksmithPluginEvent.java @@ -0,0 +1,40 @@ +package net.knarcraft.blacksmith.event; + +import net.citizensnpcs.api.npc.NPC; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.jetbrains.annotations.NotNull; + +/** + * The base class for all blacksmith plugin events + */ +@SuppressWarnings("unused") +public abstract class AbstractBlacksmithPluginEvent extends Event implements BlacksmithPluginEvent { + + protected final NPC npc; + protected final Player player; + + /** + * Instantiates a new blacksmith plugin event + * + * @param npc

The NPC involved in the event

+ * @param player

The player involved in the event

+ */ + public AbstractBlacksmithPluginEvent(@NotNull NPC npc, @NotNull Player player) { + this.npc = npc; + this.player = player; + } + + @Override + @NotNull + public NPC getNpc() { + return this.npc; + } + + @Override + @NotNull + public Player getPlayer() { + return this.player; + } + +} diff --git a/src/main/java/net/knarcraft/blacksmith/event/ActionStartEvent.java b/src/main/java/net/knarcraft/blacksmith/event/ActionStartEvent.java new file mode 100644 index 0000000..b0c6b42 --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/event/ActionStartEvent.java @@ -0,0 +1,18 @@ +package net.knarcraft.blacksmith.event; + +/** + * An event triggered when a blacksmith or scrapper starts reforging or salvaging an item + */ +@SuppressWarnings("unused") +public interface ActionStartEvent extends BlacksmithPluginEvent { + + /** + * Gets the end time for this action + * + *

The end time is a millisecond time-stamp, basically the same format used by currentTimeMillis();.

+ * + * @return

The end time

+ */ + long getActionEndTime(); + +} diff --git a/src/main/java/net/knarcraft/blacksmith/event/BlacksmithPluginEvent.java b/src/main/java/net/knarcraft/blacksmith/event/BlacksmithPluginEvent.java new file mode 100644 index 0000000..a5ae014 --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/event/BlacksmithPluginEvent.java @@ -0,0 +1,29 @@ +package net.knarcraft.blacksmith.event; + +import net.citizensnpcs.api.npc.NPC; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +/** + * An event triggered by the Blacksmith plugin + */ +@SuppressWarnings("unused") +public interface BlacksmithPluginEvent { + + /** + * Gets the NPC involved in the event + * + * @return

The NPC

+ */ + @NotNull + NPC getNpc(); + + /** + * Gets the player involved in the event + * + * @return

The player

+ */ + @NotNull + Player getPlayer(); + +} diff --git a/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeFailEvent.java b/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeFailEvent.java new file mode 100644 index 0000000..d69877b --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeFailEvent.java @@ -0,0 +1,41 @@ +package net.knarcraft.blacksmith.event; + +import net.citizensnpcs.api.npc.NPC; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +/** + * The event triggered when a blacksmith reforging fails + */ +public class BlacksmithReforgeFailEvent extends AbstractBlacksmithPluginEvent implements ReforgeEndEvent { + + private static final HandlerList handlers = new HandlerList(); + + /** + * Instantiates a new blacksmith reforge fail event + * + * @param npc

The NPC involved in the event

+ */ + public BlacksmithReforgeFailEvent(@NotNull NPC npc, @NotNull Player player) { + super(npc, player); + } + + /** + * Gets a handler-list containing all event handlers + * + * @return

A handler-list with all event handlers

+ */ + @NotNull + @SuppressWarnings("unused") + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + @NotNull + public HandlerList getHandlers() { + return handlers; + } + +} diff --git a/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeStartEvent.java b/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeStartEvent.java new file mode 100644 index 0000000..410278c --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeStartEvent.java @@ -0,0 +1,50 @@ +package net.knarcraft.blacksmith.event; + +import net.citizensnpcs.api.npc.NPC; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +/** + * The event triggered when a blacksmith reforging starts + */ +public class BlacksmithReforgeStartEvent extends AbstractBlacksmithPluginEvent implements ActionStartEvent { + + private static final HandlerList handlers = new HandlerList(); + private final Long reforgeEndTime; + + /** + * Instantiates a new blacksmith reforge start event + * + * @param npc

The NPC involved in the event

+ * @param player

The player involved in the event

+ * @param reforgeEndTime

The time at which this reforging ends

+ */ + public BlacksmithReforgeStartEvent(@NotNull NPC npc, @NotNull Player player, long reforgeEndTime) { + super(npc, player); + this.reforgeEndTime = reforgeEndTime; + } + + @Override + public long getActionEndTime() { + return reforgeEndTime; + } + + /** + * Gets a handler-list containing all event handlers + * + * @return

A handler-list with all event handlers

+ */ + @NotNull + @SuppressWarnings("unused") + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + @NotNull + public HandlerList getHandlers() { + return handlers; + } + +} diff --git a/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeSucceedEvent.java b/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeSucceedEvent.java new file mode 100644 index 0000000..269a40c --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/event/BlacksmithReforgeSucceedEvent.java @@ -0,0 +1,42 @@ +package net.knarcraft.blacksmith.event; + +import net.citizensnpcs.api.npc.NPC; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +/** + * The event triggered when a blacksmith reforging succeeds + */ +public class BlacksmithReforgeSucceedEvent extends AbstractBlacksmithPluginEvent implements ReforgeEndEvent { + + private static final HandlerList handlers = new HandlerList(); + + /** + * Instantiates a new blacksmith reforge succeed event + * + * @param npc

The NPC involved in the event

+ * @param player

The player involved in the event

+ */ + public BlacksmithReforgeSucceedEvent(@NotNull NPC npc, @NotNull Player player) { + super(npc, player); + } + + /** + * Gets a handler-list containing all event handlers + * + * @return

A handler-list with all event handlers

+ */ + @SuppressWarnings("unused") + @NotNull + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + @NotNull + public HandlerList getHandlers() { + return handlers; + } + +} diff --git a/src/main/java/net/knarcraft/blacksmith/event/NPCSoundEvent.java b/src/main/java/net/knarcraft/blacksmith/event/NPCSoundEvent.java new file mode 100644 index 0000000..01ef234 --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/event/NPCSoundEvent.java @@ -0,0 +1,142 @@ +package net.knarcraft.blacksmith.event; + +import net.citizensnpcs.api.npc.NPC; +import org.bukkit.Sound; +import org.bukkit.SoundCategory; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +/** + * An event triggered when an NPC plays a sound indicating an action + */ +@SuppressWarnings("unused") +public class NPCSoundEvent extends AbstractBlacksmithPluginEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + private float pitch; + private float volume; + private SoundCategory soundCategory; + private Sound sound; + + /** + * Instantiates a new NPC sound event + * + * @param npc

The NPC playing the sound

+ * @param player

The player whose interaction triggered the sound

+ * @param soundCategory

The category the sound is to play in

+ * @param sound

The sound to play

+ * @param volume

The volume of the played sound

+ * @param pitch

The pitch of the played sound

+ */ + public NPCSoundEvent(@NotNull NPC npc, @NotNull Player player, @NotNull SoundCategory soundCategory, + @NotNull Sound sound, float volume, float pitch) { + super(npc, player); + this.soundCategory = soundCategory; + this.sound = sound; + this.volume = volume; + this.pitch = pitch; + } + + /** + * Gets the pitch of the played sound + * + * @return

The pitch of the played sound

+ */ + public float getPitch() { + return this.pitch; + } + + /** + * Gets the volume of the played sound + * + * @return

The volume of the played sound

+ */ + public float getVolume() { + return this.volume; + } + + /** + * Gets the category the sound is played in + * + * @return

The sound category used

+ */ + public @NotNull SoundCategory getSoundCategory() { + return this.soundCategory; + } + + /** + * Gets the sound to play + * + * @return

The sound to play

+ */ + public @NotNull Sound getSound() { + return this.sound; + } + + /** + * Sets the pitch of the played sound + * + * @param pitch

The new pitch

+ */ + public void setPitch(float pitch) { + this.pitch = pitch; + } + + /** + * Sets the volume of the played sound + * + * @param volume

The new volume

+ */ + public void setVolume(float volume) { + this.volume = volume; + } + + /** + * Sets the sound to play + * + * @param sound

The new sound to play

+ */ + public void setSound(@NotNull Sound sound) { + this.sound = sound; + } + + /** + * Sets the category the sound is played in + * + * @param soundCategory

The new sound category

+ */ + public void setSoundCategory(@NotNull SoundCategory soundCategory) { + this.soundCategory = soundCategory; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + /** + * Gets a handler-list containing all event handlers + * + * @return

A handler-list with all event handlers

+ */ + @NotNull + @SuppressWarnings("unused") + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + @NotNull + public HandlerList getHandlers() { + return handlers; + } + +} diff --git a/src/main/java/net/knarcraft/blacksmith/event/ReforgeEndEvent.java b/src/main/java/net/knarcraft/blacksmith/event/ReforgeEndEvent.java new file mode 100644 index 0000000..74c82e6 --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/event/ReforgeEndEvent.java @@ -0,0 +1,7 @@ +package net.knarcraft.blacksmith.event; + +/** + * An event triggered when a reforge finishes + */ +public interface ReforgeEndEvent extends BlacksmithPluginEvent { +} diff --git a/src/main/java/net/knarcraft/blacksmith/event/SalvageEndEvent.java b/src/main/java/net/knarcraft/blacksmith/event/SalvageEndEvent.java new file mode 100644 index 0000000..bd13efe --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/event/SalvageEndEvent.java @@ -0,0 +1,7 @@ +package net.knarcraft.blacksmith.event; + +/** + * An event triggered when a salvaging finishes + */ +public interface SalvageEndEvent extends BlacksmithPluginEvent { +} diff --git a/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageFailEvent.java b/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageFailEvent.java new file mode 100644 index 0000000..1441efa --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageFailEvent.java @@ -0,0 +1,42 @@ +package net.knarcraft.blacksmith.event; + +import net.citizensnpcs.api.npc.NPC; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +/** + * The event triggered when a scrapper salvaging fails + */ +public class ScrapperSalvageFailEvent extends AbstractBlacksmithPluginEvent implements SalvageEndEvent { + + private static final HandlerList handlers = new HandlerList(); + + /** + * Instantiates a new scrapper salvage fail event + * + * @param npc

The NPC involved in the event

+ * @param player

The player involved in the event

+ */ + public ScrapperSalvageFailEvent(@NotNull NPC npc, @NotNull Player player) { + super(npc, player); + } + + /** + * Gets a handler-list containing all event handlers + * + * @return

A handler-list with all event handlers

+ */ + @SuppressWarnings("unused") + @NotNull + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + @NotNull + public HandlerList getHandlers() { + return handlers; + } + +} diff --git a/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageStartEvent.java b/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageStartEvent.java new file mode 100644 index 0000000..07df374 --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageStartEvent.java @@ -0,0 +1,51 @@ +package net.knarcraft.blacksmith.event; + +import net.citizensnpcs.api.npc.NPC; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +/** + * The event triggered when a scrapper salvaging starts + */ +@SuppressWarnings("unused") +public class ScrapperSalvageStartEvent extends AbstractBlacksmithPluginEvent implements ActionStartEvent { + + private static final HandlerList handlers = new HandlerList(); + private final long salvageEndTime; + + /** + * Instantiates a new scrapper salvage start event + * + * @param npc

The NPC involved in the event

+ * @param player

The player involved in the event

+ * @param salvageEndTime

The time at which this salvaging ends

+ */ + public ScrapperSalvageStartEvent(@NotNull NPC npc, @NotNull Player player, long salvageEndTime) { + super(npc, player); + this.salvageEndTime = salvageEndTime; + } + + @Override + public long getActionEndTime() { + return this.salvageEndTime; + } + + /** + * Gets a handler-list containing all event handlers + * + * @return

A handler-list with all event handlers

+ */ + @SuppressWarnings("unused") + @NotNull + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + @NotNull + public HandlerList getHandlers() { + return handlers; + } + +} diff --git a/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageSucceedEvent.java b/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageSucceedEvent.java new file mode 100644 index 0000000..4eb67a4 --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/event/ScrapperSalvageSucceedEvent.java @@ -0,0 +1,42 @@ +package net.knarcraft.blacksmith.event; + +import net.citizensnpcs.api.npc.NPC; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +/** + * The event triggered when a scrapper salvaging succeeds + */ +public class ScrapperSalvageSucceedEvent extends AbstractBlacksmithPluginEvent implements SalvageEndEvent { + + private static final HandlerList handlers = new HandlerList(); + + /** + * Instantiates a new scrapper salvage succeed event + * + * @param npc

The NPC involved in the event

+ * @param player

The player involved in the event

+ */ + public ScrapperSalvageSucceedEvent(@NotNull NPC npc, @NotNull Player player) { + super(npc, player); + } + + /** + * Gets a handler-list containing all event handlers + * + * @return

A handler-list with all event handlers

+ */ + @SuppressWarnings("unused") + @NotNull + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + @NotNull + public HandlerList getHandlers() { + return handlers; + } + +} diff --git a/src/main/java/net/knarcraft/blacksmith/trait/BlacksmithTrait.java b/src/main/java/net/knarcraft/blacksmith/trait/BlacksmithTrait.java index bc241b2..8bd9a83 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/BlacksmithTrait.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/BlacksmithTrait.java @@ -21,6 +21,7 @@ import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.send * The class representing the Blacksmith NPC trait */ public class BlacksmithTrait extends CustomTrait { + private final BlacksmithNPCSettings config; /** diff --git a/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java b/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java index ce2647f..b0814f0 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java @@ -3,6 +3,8 @@ package net.knarcraft.blacksmith.trait; import net.citizensnpcs.api.npc.NPC; import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSettings; +import net.knarcraft.blacksmith.event.BlacksmithReforgeFailEvent; +import net.knarcraft.blacksmith.event.BlacksmithReforgeSucceedEvent; import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.util.InputParsingHelper; import net.knarcraft.blacksmith.util.ItemHelper; @@ -135,9 +137,11 @@ public class ReforgeSession extends Session implements Runnable { private boolean reforgeItem() { if (random.nextInt(100) < this.config.getFailChance()) { failReforge(); + BlacksmithPlugin.getInstance().callEvent(new BlacksmithReforgeFailEvent(this.npc, this.player)); return false; } else { succeedReforge(); + BlacksmithPlugin.getInstance().callEvent(new BlacksmithReforgeSucceedEvent(this.npc, this.player)); return true; } } diff --git a/src/main/java/net/knarcraft/blacksmith/trait/SalvageSession.java b/src/main/java/net/knarcraft/blacksmith/trait/SalvageSession.java index f914cc1..84c6c31 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/SalvageSession.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/SalvageSession.java @@ -1,8 +1,11 @@ package net.knarcraft.blacksmith.trait; import net.citizensnpcs.api.npc.NPC; +import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.SalvageMethod; import net.knarcraft.blacksmith.config.scrapper.ScrapperNPCSettings; +import net.knarcraft.blacksmith.event.ScrapperSalvageFailEvent; +import net.knarcraft.blacksmith.event.ScrapperSalvageSucceedEvent; import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.util.ItemHelper; import net.knarcraft.blacksmith.util.SalvageHelper; @@ -114,9 +117,11 @@ public class SalvageSession extends Session implements Runnable { if (random.nextInt(100) < this.config.getFailChance()) { playSound(Sound.ENTITY_VILLAGER_NO); failSalvage(); + BlacksmithPlugin.getInstance().callEvent(new ScrapperSalvageFailEvent(this.npc, this.player)); } else { playSound(Sound.BLOCK_ANVIL_USE); giveSalvage(); + BlacksmithPlugin.getInstance().callEvent(new ScrapperSalvageSucceedEvent(this.npc, this.player)); sendNPCMessage(this.npc, this.player, this.config.getSuccessMessage()); } } diff --git a/src/main/java/net/knarcraft/blacksmith/trait/Session.java b/src/main/java/net/knarcraft/blacksmith/trait/Session.java index a0b740b..f9f7ce3 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/Session.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/Session.java @@ -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

The sound to play

*/ 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

The entity that should play the sound

+ * @param sound

The sound to play

+ * @param pitch

The pitch to play the sound at

+ */ + 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()); } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index ebcc906..4350236 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,6 +3,7 @@ authors: [ EpicKnarvik97, aPunch, jrbudda, HurricanKai ] version: '${project.version}' main: net.knarcraft.blacksmith.BlacksmithPlugin depend: [ Citizens, Vault ] +softdepend: [ ProtocolLib ] prefix: "Blacksmith" description: "A plugin that adds Blacksmith and Scrapper traits compatible with Citizens, allowing players to repair and salvage items" website: "https://www.spigotmc.org/resources/blacksmith.105938/"