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 isPerNPCWhether this setting is per-NPC or global
* @param isMessageWhether 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 isPerNPCWhether this setting is per-NPC or global
* @param isMessageWhether 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 npcThe NPC involved in the event
+ * @param playerThe 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();.
+ * + * @returnThe 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 + * + * @returnThe NPC
+ */ + @NotNull + NPC getNpc(); + + /** + * Gets the player involved in the event + * + * @returnThe 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 npcThe NPC involved in the event
+ */ + public BlacksmithReforgeFailEvent(@NotNull NPC npc, @NotNull Player player) { + super(npc, player); + } + + /** + * Gets a handler-list containing all event handlers + * + * @returnA 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 npcThe NPC involved in the event
+ * @param playerThe player involved in the event
+ * @param reforgeEndTimeThe 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 + * + * @returnA 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 npcThe NPC involved in the event
+ * @param playerThe player involved in the event
+ */ + public BlacksmithReforgeSucceedEvent(@NotNull NPC npc, @NotNull Player player) { + super(npc, player); + } + + /** + * Gets a handler-list containing all event handlers + * + * @returnA 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 npcThe NPC playing the sound
+ * @param playerThe player whose interaction triggered the sound
+ * @param soundCategoryThe category the sound is to play in
+ * @param soundThe sound to play
+ * @param volumeThe volume of the played sound
+ * @param pitchThe 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 + * + * @returnThe pitch of the played sound
+ */ + public float getPitch() { + return this.pitch; + } + + /** + * Gets the volume of the played sound + * + * @returnThe volume of the played sound
+ */ + public float getVolume() { + return this.volume; + } + + /** + * Gets the category the sound is played in + * + * @returnThe sound category used
+ */ + public @NotNull SoundCategory getSoundCategory() { + return this.soundCategory; + } + + /** + * Gets the sound to play + * + * @returnThe sound to play
+ */ + public @NotNull Sound getSound() { + return this.sound; + } + + /** + * Sets the pitch of the played sound + * + * @param pitchThe new pitch
+ */ + public void setPitch(float pitch) { + this.pitch = pitch; + } + + /** + * Sets the volume of the played sound + * + * @param volumeThe new volume
+ */ + public void setVolume(float volume) { + this.volume = volume; + } + + /** + * Sets the sound to play + * + * @param soundThe new sound to play
+ */ + public void setSound(@NotNull Sound sound) { + this.sound = sound; + } + + /** + * Sets the category the sound is played in + * + * @param soundCategoryThe 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 + * + * @returnA 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 npcThe NPC involved in the event
+ * @param playerThe player involved in the event
+ */ + public ScrapperSalvageFailEvent(@NotNull NPC npc, @NotNull Player player) { + super(npc, player); + } + + /** + * Gets a handler-list containing all event handlers + * + * @returnA 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 npcThe NPC involved in the event
+ * @param playerThe player involved in the event
+ * @param salvageEndTimeThe 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 + * + * @returnA 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 npcThe NPC involved in the event
+ * @param playerThe player involved in the event
+ */ + public ScrapperSalvageSucceedEvent(@NotNull NPC npc, @NotNull Player player) { + super(npc, player); + } + + /** + * Gets a handler-list containing all event handlers + * + * @returnA 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 CustomTraitThe 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 entityThe entity that should play the sound
+ * @param soundThe sound to play
+ * @param pitchThe 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/"