mirror of
https://github.com/Artillex-Studios/AxMinions.git
synced 2024-11-25 12:05:56 +01:00
improve MinionPrePlaceEvent + add MinionChestLinkEvent
This commit is contained in:
parent
12773889cd
commit
d478a3c4eb
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,12 @@
|
|||||||
package com.artillexstudios.axminions.api.events
|
package com.artillexstudios.axminions.api.events
|
||||||
|
|
||||||
|
import org.bukkit.Location
|
||||||
import org.bukkit.entity.Player
|
import org.bukkit.entity.Player
|
||||||
import org.bukkit.event.Cancellable
|
import org.bukkit.event.Cancellable
|
||||||
import org.bukkit.event.Event
|
import org.bukkit.event.Event
|
||||||
import org.bukkit.event.HandlerList
|
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 {
|
companion object {
|
||||||
private val handlerList = HandlerList()
|
private val handlerList = HandlerList()
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ class MinionPrePlaceEvent(private val player: Player) : Cancellable, Event() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private var isCancelled = false
|
private var isCancelled = false
|
||||||
|
private var shouldOverridePlayerLimit = false
|
||||||
|
|
||||||
override fun getHandlers(): HandlerList {
|
override fun getHandlers(): HandlerList {
|
||||||
return MinionPrePlaceEvent.handlerList
|
return MinionPrePlaceEvent.handlerList
|
||||||
@ -28,6 +29,18 @@ class MinionPrePlaceEvent(private val player: Player) : Cancellable, Event() {
|
|||||||
return player
|
return player
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getLocation(): Location {
|
||||||
|
return location
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getShouldOverridePlayerLimit(): Boolean {
|
||||||
|
return shouldOverridePlayerLimit
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setShouldOverridePlayerLimit(should: Boolean) {
|
||||||
|
shouldOverridePlayerLimit = should
|
||||||
|
}
|
||||||
|
|
||||||
override fun setCancelled(cancelled: Boolean) {
|
override fun setCancelled(cancelled: Boolean) {
|
||||||
isCancelled = cancelled
|
isCancelled = cancelled
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,13 @@ import com.artillexstudios.axapi.utils.StringUtils
|
|||||||
import com.artillexstudios.axminions.AxMinionsPlugin
|
import com.artillexstudios.axminions.AxMinionsPlugin
|
||||||
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.MinionChestLinkEvent
|
||||||
|
import com.artillexstudios.axminions.api.events.PreMinionDamageEntityEvent
|
||||||
import com.artillexstudios.axminions.api.minions.Minion
|
import com.artillexstudios.axminions.api.minions.Minion
|
||||||
|
import org.bukkit.Bukkit
|
||||||
import java.util.WeakHashMap
|
import java.util.WeakHashMap
|
||||||
import org.bukkit.Material
|
import org.bukkit.Material
|
||||||
|
import org.bukkit.entity.LivingEntity
|
||||||
import org.bukkit.entity.Player
|
import org.bukkit.entity.Player
|
||||||
import org.bukkit.event.EventHandler
|
import org.bukkit.event.EventHandler
|
||||||
import org.bukkit.event.Listener
|
import org.bukkit.event.Listener
|
||||||
@ -25,6 +29,16 @@ class LinkingListener : Listener {
|
|||||||
if (event.clickedBlock!!.type !in CONTAINERS) return
|
if (event.clickedBlock!!.type !in CONTAINERS) return
|
||||||
if (!AxMinionsPlugin.integrations.getProtectionIntegration().canBuildAt(event.player, event.clickedBlock!!.location)) 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
|
val minion = linking.remove(event.player) ?: return
|
||||||
event.isCancelled = true
|
event.isCancelled = true
|
||||||
if (minion.getLocation()
|
if (minion.getLocation()
|
||||||
|
@ -58,11 +58,6 @@ class MinionPlaceListener : Listener {
|
|||||||
|
|
||||||
if (meta.persistentDataContainer.has(Keys.PLACED, PersistentDataType.BYTE)) return
|
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)
|
meta.persistentDataContainer.set(Keys.PLACED, PersistentDataType.BYTE, 0)
|
||||||
item.itemMeta = meta
|
item.itemMeta = meta
|
||||||
|
|
||||||
@ -71,10 +66,15 @@ class MinionPlaceListener : Listener {
|
|||||||
val maxMinions = AxMinionsAPI.INSTANCE.getMinionLimit(event.player)
|
val maxMinions = AxMinionsAPI.INSTANCE.getMinionLimit(event.player)
|
||||||
|
|
||||||
val chunk = location.chunk
|
val chunk = location.chunk
|
||||||
|
|
||||||
|
val prePlaceEvent = MinionPrePlaceEvent(event.player, event.clickedBlock!!.location)
|
||||||
|
Bukkit.getPluginManager().callEvent(prePlaceEvent)
|
||||||
|
if(prePlaceEvent.isCancelled) return
|
||||||
|
|
||||||
AxMinionsPlugin.dataQueue.submit {
|
AxMinionsPlugin.dataQueue.submit {
|
||||||
val placed = AxMinionsPlugin.dataHandler.getMinionAmount(event.player.uniqueId)
|
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(
|
event.player.sendMessage(
|
||||||
StringUtils.formatToString(
|
StringUtils.formatToString(
|
||||||
Messages.PREFIX() + Messages.PLACE_LIMIT_REACHED(),
|
Messages.PREFIX() + Messages.PLACE_LIMIT_REACHED(),
|
||||||
|
Loading…
Reference in New Issue
Block a user