From 9410d7483272429c5c577bd955e59b2965a0d812 Mon Sep 17 00:00:00 2001 From: rudde Date: Tue, 30 Apr 2024 13:12:23 +0300 Subject: [PATCH] custom fail message for MinionChestLinkEvent --- .../api/events/MinionChestLinkEvent.kt | 10 ++++++++- .../axminions/listeners/LinkingListener.kt | 22 ++++++++++--------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/api/src/main/kotlin/com/artillexstudios/axminions/api/events/MinionChestLinkEvent.kt b/api/src/main/kotlin/com/artillexstudios/axminions/api/events/MinionChestLinkEvent.kt index c07ca27..48950b9 100644 --- a/api/src/main/kotlin/com/artillexstudios/axminions/api/events/MinionChestLinkEvent.kt +++ b/api/src/main/kotlin/com/artillexstudios/axminions/api/events/MinionChestLinkEvent.kt @@ -15,7 +15,7 @@ class MinionChestLinkEvent(minion: Minion, private val player: Player, private v } private var isCancelled = false - + private var failMessage: String? = null override fun getHandlers(): HandlerList { return handlerList @@ -25,6 +25,14 @@ class MinionChestLinkEvent(minion: Minion, private val player: Player, private v return player } + fun getFailMessage(): String? { + return failMessage + } + + fun setFailMessage(message: String) { + failMessage = message + } + fun getBlock(): Block { return block } diff --git a/common/src/main/kotlin/com/artillexstudios/axminions/listeners/LinkingListener.kt b/common/src/main/kotlin/com/artillexstudios/axminions/listeners/LinkingListener.kt index ea335d6..bba31a3 100644 --- a/common/src/main/kotlin/com/artillexstudios/axminions/listeners/LinkingListener.kt +++ b/common/src/main/kotlin/com/artillexstudios/axminions/listeners/LinkingListener.kt @@ -30,6 +30,18 @@ class LinkingListener : Listener { if (!AxMinionsPlugin.integrations.getProtectionIntegration().canBuildAt(event.player, event.clickedBlock!!.location)) return val minion = linking.remove(event.player) ?: return + + val linkEvent = MinionChestLinkEvent( + minion, + event.player, + event.clickedBlock!! + ) + Bukkit.getPluginManager().callEvent(linkEvent) + if (linkEvent.isCancelled) { + event.player.sendMessage(StringUtils.formatToString(linkEvent.getFailMessage() ?: (Messages.PREFIX() + Messages.LINK_FAIL()))) + return + } + event.isCancelled = true if (minion.getLocation() .distanceSquared(event.clickedBlock!!.location) > Config.MAX_LINKING_DISTANCE() * Config.MAX_LINKING_DISTANCE() @@ -38,16 +50,6 @@ class LinkingListener : Listener { return } - val linkEvent = MinionChestLinkEvent( - linking.getValue(event.player), - event.player, - event.clickedBlock!! - ) - Bukkit.getPluginManager().callEvent(linkEvent) - if (linkEvent.isCancelled) { - return - } - event.player.sendMessage(StringUtils.formatToString(Messages.PREFIX() + Messages.LINK_SUCCESS())) minion.setLinkedChest(event.clickedBlock!!.location) }