diff --git a/api/src/main/kotlin/com/artillexstudios/axminions/api/events/MinionToolEvent.kt b/api/src/main/kotlin/com/artillexstudios/axminions/api/events/MinionToolEvent.kt new file mode 100644 index 0000000..0f7c368 --- /dev/null +++ b/api/src/main/kotlin/com/artillexstudios/axminions/api/events/MinionToolEvent.kt @@ -0,0 +1,39 @@ +package com.artillexstudios.axminions.api.events + +import com.artillexstudios.axminions.api.minions.Minion +import org.bukkit.entity.Player +import org.bukkit.event.Cancellable +import org.bukkit.event.Event +import org.bukkit.event.HandlerList +import org.bukkit.inventory.ItemStack + +class MinionToolEvent(minion: Minion, private val player: Player, private val newTool: ItemStack) : MinionEvent(minion), Cancellable { + companion object { + private val handlerList = HandlerList() + + @JvmStatic + fun getHandlerList(): HandlerList = handlerList + } + + private var isCancelled = false + + override fun getHandlers(): HandlerList { + return handlerList + } + + override fun isCancelled(): Boolean { + return isCancelled + } + + fun getPlayer(): Player { + return player + } + + fun getNewTool(): ItemStack { + return newTool + } + + override fun setCancelled(cancelled: Boolean) { + isCancelled = cancelled + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/listeners/MinionInventoryListener.kt b/common/src/main/kotlin/com/artillexstudios/axminions/listeners/MinionInventoryListener.kt index cca3191..4aae692 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/listeners/MinionInventoryListener.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/listeners/MinionInventoryListener.kt @@ -5,6 +5,7 @@ import com.artillexstudios.axminions.AxMinionsPlugin import com.artillexstudios.axminions.api.AxMinionsAPI import com.artillexstudios.axminions.api.config.Config import com.artillexstudios.axminions.api.config.Messages +import com.artillexstudios.axminions.api.events.MinionToolEvent import com.artillexstudios.axminions.api.minions.Direction import com.artillexstudios.axminions.api.minions.Minion import com.artillexstudios.axminions.api.minions.miniontype.MinionTypes @@ -14,6 +15,7 @@ import com.artillexstudios.axminions.api.utils.fastFor import java.util.Locale import net.md_5.bungee.api.ChatMessageType import net.md_5.bungee.api.chat.TextComponent +import org.bukkit.Bukkit import org.bukkit.Material import org.bukkit.Sound import org.bukkit.entity.Player @@ -65,6 +67,9 @@ class MinionInventoryListener : Listener { player.sendMessage(StringUtils.formatToString(Messages.PREFIX() + Messages.WRONG_TOOL())) return } + val minionToolEvent = MinionToolEvent(minion, player, event.currentItem!!) + Bukkit.getPluginManager().callEvent(minionToolEvent) + if (minionToolEvent.isCancelled) return if (minion.getTool()?.type != Material.AIR) { val current = event.currentItem!!.clone() @@ -88,6 +93,9 @@ class MinionInventoryListener : Listener { player.sendMessage(StringUtils.formatToString(Messages.PREFIX() + Messages.ERROR_INVENTORY_FULL())) return } + val minionToolEvent = MinionToolEvent(minion, player, ItemStack(Material.AIR)) + Bukkit.getPluginManager().callEvent(minionToolEvent) + if (minionToolEvent.isCancelled) return val tool = minion.getTool()?.clone() ?: return minion.setTool(ItemStack(Material.AIR))