Merge pull request #15 from rudde0/miniontoolevent

Add MinionToolEvent
This commit is contained in:
TomTom 2024-05-18 17:17:59 +02:00 committed by GitHub
commit fced0cbbd9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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

@ -6,6 +6,7 @@ import com.artillexstudios.axminions.AxMinionsPlugin
import com.artillexstudios.axminions.api.AxMinionsAPI import com.artillexstudios.axminions.api.AxMinionsAPI
import com.artillexstudios.axminions.api.config.Config import com.artillexstudios.axminions.api.config.Config
import com.artillexstudios.axminions.api.config.Messages 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.Direction
import com.artillexstudios.axminions.api.minions.Minion import com.artillexstudios.axminions.api.minions.Minion
import com.artillexstudios.axminions.api.minions.miniontype.MinionTypes import com.artillexstudios.axminions.api.minions.miniontype.MinionTypes
@ -16,6 +17,7 @@ import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver
import java.util.Locale import java.util.Locale
import net.md_5.bungee.api.ChatMessageType import net.md_5.bungee.api.ChatMessageType
import net.md_5.bungee.api.chat.TextComponent import net.md_5.bungee.api.chat.TextComponent
import org.bukkit.Bukkit
import org.bukkit.Material import org.bukkit.Material
import org.bukkit.Sound import org.bukkit.Sound
import org.bukkit.entity.Player import org.bukkit.entity.Player
@ -67,6 +69,9 @@ class MinionInventoryListener : Listener {
player.sendMessage(StringUtils.formatToString(Messages.PREFIX() + Messages.WRONG_TOOL())) player.sendMessage(StringUtils.formatToString(Messages.PREFIX() + Messages.WRONG_TOOL()))
return return
} }
val minionToolEvent = MinionToolEvent(minion, player, event.currentItem!!)
Bukkit.getPluginManager().callEvent(minionToolEvent)
if (minionToolEvent.isCancelled) return
if (minion.getTool()?.type != Material.AIR) { if (minion.getTool()?.type != Material.AIR) {
val current = event.currentItem!!.clone() val current = event.currentItem!!.clone()
@ -90,6 +95,9 @@ class MinionInventoryListener : Listener {
player.sendMessage(StringUtils.formatToString(Messages.PREFIX() + Messages.ERROR_INVENTORY_FULL())) player.sendMessage(StringUtils.formatToString(Messages.PREFIX() + Messages.ERROR_INVENTORY_FULL()))
return return
} }
val minionToolEvent = MinionToolEvent(minion, player, ItemStack(Material.AIR))
Bukkit.getPluginManager().callEvent(minionToolEvent)
if (minionToolEvent.isCancelled) return
val tool = minion.getTool()?.clone() ?: return val tool = minion.getTool()?.clone() ?: return
minion.setTool(ItemStack(Material.AIR)) minion.setTool(ItemStack(Material.AIR))