add MinionToolEvent

This commit is contained in:
rudde 2024-05-17 00:58:28 +03:00
parent c35bae34da
commit 3284681ef9
2 changed files with 47 additions and 0 deletions

View File

@ -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
}
}

View File

@ -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))