improve MinionPrePlaceEvent + add MinionChestLinkEvent

This commit is contained in:
rudde 2024-04-30 12:51:48 +03:00
parent 12773889cd
commit d478a3c4eb
4 changed files with 74 additions and 8 deletions

View File

@ -0,0 +1,39 @@
package com.artillexstudios.axminions.api.events
import com.artillexstudios.axminions.api.minions.Minion
import org.bukkit.block.Block
import org.bukkit.entity.Player
import org.bukkit.event.Cancellable
import org.bukkit.event.HandlerList
class MinionChestLinkEvent(minion: Minion, private val player: Player, private val block: Block) : MinionEvent(minion), Cancellable {
companion object {
private val handlerList = HandlerList()
@JvmStatic
fun getHandlerList(): HandlerList = handlerList
}
private var isCancelled = false
override fun getHandlers(): HandlerList {
return handlerList
}
fun getLinker(): Player {
return player
}
fun getBlock(): Block {
return block
}
override fun isCancelled(): Boolean {
return isCancelled
}
override fun setCancelled(cancelled: Boolean) {
isCancelled = cancelled
}
}

View File

@ -1,11 +1,12 @@
package com.artillexstudios.axminions.api.events
import org.bukkit.Location
import org.bukkit.entity.Player
import org.bukkit.event.Cancellable
import org.bukkit.event.Event
import org.bukkit.event.HandlerList
class MinionPrePlaceEvent(private val player: Player) : Cancellable, Event() {
class MinionPrePlaceEvent(private val player: Player, private val location: Location) : Cancellable, Event() {
companion object {
private val handlerList = HandlerList()
@ -14,7 +15,7 @@ class MinionPrePlaceEvent(private val player: Player) : Cancellable, Event() {
}
private var isCancelled = false
private var shouldOverridePlayerLimit = false
override fun getHandlers(): HandlerList {
return MinionPrePlaceEvent.handlerList
@ -28,6 +29,18 @@ class MinionPrePlaceEvent(private val player: Player) : Cancellable, Event() {
return player
}
fun getLocation(): Location {
return location
}
fun getShouldOverridePlayerLimit(): Boolean {
return shouldOverridePlayerLimit
}
fun setShouldOverridePlayerLimit(should: Boolean) {
shouldOverridePlayerLimit = should
}
override fun setCancelled(cancelled: Boolean) {
isCancelled = cancelled
}

View File

@ -4,9 +4,13 @@ import com.artillexstudios.axapi.utils.StringUtils
import com.artillexstudios.axminions.AxMinionsPlugin
import com.artillexstudios.axminions.api.config.Config
import com.artillexstudios.axminions.api.config.Messages
import com.artillexstudios.axminions.api.events.MinionChestLinkEvent
import com.artillexstudios.axminions.api.events.PreMinionDamageEntityEvent
import com.artillexstudios.axminions.api.minions.Minion
import org.bukkit.Bukkit
import java.util.WeakHashMap
import org.bukkit.Material
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
@ -25,6 +29,16 @@ class LinkingListener : Listener {
if (event.clickedBlock!!.type !in CONTAINERS) return
if (!AxMinionsPlugin.integrations.getProtectionIntegration().canBuildAt(event.player, event.clickedBlock!!.location)) return
val linkEvent = MinionChestLinkEvent(
linking.getValue(event.player),
event.player,
event.clickedBlock!!
)
Bukkit.getPluginManager().callEvent(linkEvent)
if (linkEvent.isCancelled) {
return
}
val minion = linking.remove(event.player) ?: return
event.isCancelled = true
if (minion.getLocation()

View File

@ -58,11 +58,6 @@ class MinionPlaceListener : Listener {
if (meta.persistentDataContainer.has(Keys.PLACED, PersistentDataType.BYTE)) return
val prePlaceEvent = MinionPrePlaceEvent(event.player)
Bukkit.getPluginManager().callEvent(prePlaceEvent)
if(prePlaceEvent.isCancelled) return
meta.persistentDataContainer.set(Keys.PLACED, PersistentDataType.BYTE, 0)
item.itemMeta = meta
@ -71,10 +66,15 @@ class MinionPlaceListener : Listener {
val maxMinions = AxMinionsAPI.INSTANCE.getMinionLimit(event.player)
val chunk = location.chunk
val prePlaceEvent = MinionPrePlaceEvent(event.player, event.clickedBlock!!.location)
Bukkit.getPluginManager().callEvent(prePlaceEvent)
if(prePlaceEvent.isCancelled) return
AxMinionsPlugin.dataQueue.submit {
val placed = AxMinionsPlugin.dataHandler.getMinionAmount(event.player.uniqueId)
if (placed >= maxMinions && !event.player.hasPermission("axminions.limit.*")) {
if (placed >= maxMinions && !prePlaceEvent.getShouldOverridePlayerLimit() && !event.player.hasPermission("axminions.limit.*")) {
event.player.sendMessage(
StringUtils.formatToString(
Messages.PREFIX() + Messages.PLACE_LIMIT_REACHED(),