From 143cfd39987745ba78a93ffd632bd06d94f0d341 Mon Sep 17 00:00:00 2001 From: Sven Date: Wed, 11 Jun 2025 22:23:39 +0200 Subject: [PATCH] Improve economy-related item usage prevention --- .../net/trueog/diamondbankog/AutoCompress.kt | 14 ++-- .../kotlin/net/trueog/diamondbankog/Events.kt | 64 +++++++++++++++++-- .../trueog/diamondbankog/commands/Compress.kt | 18 +++--- 3 files changed, 75 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/net/trueog/diamondbankog/AutoCompress.kt b/src/main/kotlin/net/trueog/diamondbankog/AutoCompress.kt index 9d7ff44..513df7d 100644 --- a/src/main/kotlin/net/trueog/diamondbankog/AutoCompress.kt +++ b/src/main/kotlin/net/trueog/diamondbankog/AutoCompress.kt @@ -56,7 +56,7 @@ internal object AutoCompress { .sumOf { 64 - it.amount } if (changeInDiamonds > emptySlots + leftOverSpaceDiamonds) { - player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: You do not have enough space in your inventory to compress all the Diamond-related items (+$changeInDiamonds Diamonds).")) + player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: You do not have enough space in your inventory to compress all the Diamond currency items (+$changeInDiamonds Diamonds).")) return@runOnMainThread } } @@ -69,7 +69,7 @@ internal object AutoCompress { .sumOf { 64 - it.amount } if (changeInDiamondBlocks > emptySlots + leftOverSpaceDiamondBlocks) { - player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: You do not have enough space in your inventory to compress all the Diamond-related items (+$changeInDiamondBlocks Diamond Blocks).")) + player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: You do not have enough space in your inventory to compress all the Diamond currency items (+$changeInDiamondBlocks Diamond Blocks).")) return@runOnMainThread } } @@ -77,7 +77,7 @@ internal object AutoCompress { if (changeInShards < 0) { val removeMap = player.inventory.removeItem(Shard.createItemStack(abs(changeInShards))) if (removeMap.isNotEmpty()) { - player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond-related items in your inventory.")) + player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond currency items in your inventory.")) return@runOnMainThread } } @@ -85,13 +85,13 @@ internal object AutoCompress { if (changeInDiamonds > 0) { val addMap = player.inventory.addItem(ItemStack(Material.DIAMOND, changeInDiamonds)) if (addMap.isNotEmpty()) { - player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond-related items in your inventory.")) + player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond currency items in your inventory.")) return@runOnMainThread } } else if (changeInDiamonds < 0) { val removeMap = player.inventory.removeItem(ItemStack(Material.DIAMOND, abs(changeInDiamonds))) if (removeMap.isNotEmpty()) { - player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond-related items in your inventory.")) + player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond currency items in your inventory.")) return@runOnMainThread } } @@ -99,14 +99,14 @@ internal object AutoCompress { if (changeInDiamondBlocks > 0) { val addMap = player.inventory.addItem(ItemStack(Material.DIAMOND_BLOCK, changeInDiamondBlocks)) if (addMap.isNotEmpty()) { - player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond-related items in your inventory.")) + player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond currency items in your inventory.")) return@runOnMainThread } } else if (changeInDiamondBlocks < 0) { val removeMap = player.inventory.removeItem(ItemStack(Material.DIAMOND_BLOCK, abs(changeInDiamondBlocks))) if (removeMap.isNotEmpty()) { - player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond-related items in your inventory.")) + player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond currency items in your inventory.")) return@runOnMainThread } } diff --git a/src/main/kotlin/net/trueog/diamondbankog/Events.kt b/src/main/kotlin/net/trueog/diamondbankog/Events.kt index e3296fc..e803e0c 100644 --- a/src/main/kotlin/net/trueog/diamondbankog/Events.kt +++ b/src/main/kotlin/net/trueog/diamondbankog/Events.kt @@ -13,13 +13,14 @@ import org.bukkit.entity.Player import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.Listener -import org.bukkit.event.block.BlockPlaceEvent import org.bukkit.event.entity.EntityPickupItemEvent import org.bukkit.event.inventory.InventoryClickEvent import org.bukkit.event.inventory.InventoryCloseEvent import org.bukkit.event.inventory.InventoryType import org.bukkit.event.inventory.PrepareItemCraftEvent import org.bukkit.event.player.PlayerDropItemEvent +import org.bukkit.event.player.PlayerInteractEntityEvent +import org.bukkit.event.player.PlayerInteractEvent import org.bukkit.event.player.PlayerJoinEvent import org.bukkit.inventory.ItemStack import org.bukkit.scheduler.BukkitRunnable @@ -215,15 +216,68 @@ internal class Events : Listener { } @EventHandler(priority = EventPriority.HIGHEST) - fun onBlockPlace(event: BlockPlaceEvent) { - val worldName = event.player.world.name + fun onPlayerInteract(event: PlayerInteractEvent) { + val player = event.player + val worldName = player.world.name if (worldName != "world" && worldName != "world_nether" && worldName != "world_the_end") return - if (event.block.type != Material.DIAMOND_BLOCK && event.block.type != Material.SHULKER_BOX) return + val clickedBlock = event.clickedBlock + if (clickedBlock == null) return + if (clickedBlock.type == Material.AIR) return + + if (event.action.isLeftClick) { + if (clickedBlock.type != Material.DIAMOND_BLOCK && clickedBlock.type != Material.SHULKER_BOX) { + return + } + } else { + val itemStack = event.item + if (itemStack == null) return + val itemType = itemStack.type + + if (itemType != Material.DIAMOND && itemType != Material.DIAMOND_BLOCK && itemType != Material.SHULKER_BOX && !(itemType == Material.PRISMARINE_SHARD && itemStack.persistentDataContainer.has( + Shard.namespacedKey + )) + ) { + return + } + } if (DiamondBankOG.economyDisabled) { event.isCancelled = true - event.player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: You cannot place any economy-related items while the economy is disabled.")) + event.player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: You cannot interact with any economy-related items while the economy is disabled.")) + return + } + + if (DiamondBankOG.transactionLock.isLocked(event.player.uniqueId)) { + event.isCancelled = true + return + } + } + + @EventHandler(priority = EventPriority.HIGHEST) + fun onPlayerInteractEntity(event: PlayerInteractEntityEvent) { + val player = event.player + val worldName = player.world.name + if (worldName != "world" && worldName != "world_nether" && worldName != "world_the_end") return + + val itemStackInMainHand = player.inventory.itemInMainHand + val itemTypeInMainHand = itemStackInMainHand.type + + val itemStackInOffHand = player.inventory.itemInOffHand + val itemTypeInOffHand = itemStackInOffHand.type + + if ((itemTypeInMainHand != Material.DIAMOND && itemTypeInMainHand != Material.DIAMOND_BLOCK && itemTypeInMainHand != Material.SHULKER_BOX && !(itemTypeInMainHand == Material.PRISMARINE_SHARD && itemStackInMainHand.persistentDataContainer.has( + Shard.namespacedKey + )) && (itemTypeInOffHand != Material.DIAMOND && itemTypeInOffHand != Material.DIAMOND_BLOCK && itemTypeInOffHand != Material.SHULKER_BOX && !(itemTypeInOffHand == Material.PRISMARINE_SHARD && itemStackInOffHand.persistentDataContainer.has( + Shard.namespacedKey + )))) + ) { + return + } + + if (DiamondBankOG.economyDisabled) { + event.isCancelled = true + event.player.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: You cannot interact with any economy-related items while the economy is disabled.")) return } diff --git a/src/main/kotlin/net/trueog/diamondbankog/commands/Compress.kt b/src/main/kotlin/net/trueog/diamondbankog/commands/Compress.kt index b44c882..2c22f27 100644 --- a/src/main/kotlin/net/trueog/diamondbankog/commands/Compress.kt +++ b/src/main/kotlin/net/trueog/diamondbankog/commands/Compress.kt @@ -83,7 +83,7 @@ internal class Compress : CommandExecutor { } if (sender.inventory.itemInMainHand.type == Material.SHULKER_BOX && !isShulkerBox) { - sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: <#FFA500>Are you sure you want to compress the Diamond-related items in the shulker box you're holding? If so, run \"/compress yes\"")) + sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: <#FFA500>Are you sure you want to compress the Diamond currency items in the shulker box you're holding? If so, run \"/compress yes\"")) return@runOnMainThread RunOnMainThreadResult(true, null, null, null) } @@ -118,7 +118,7 @@ internal class Compress : CommandExecutor { .sumOf { 64 - it.amount } if (changeInDiamonds > emptySlots + leftOverSpaceDiamonds) { - sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: You do not have enough space in your ${if (isShulkerBox) "shulker box" else "inventory"} to compress all the Diamond-related items (+$changeInDiamonds Diamonds).")) + sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: You do not have enough space in your ${if (isShulkerBox) "shulker box" else "inventory"} to compress all the Diamond currency items (+$changeInDiamonds Diamonds).")) return@runOnMainThread RunOnMainThreadResult(true, null, null, null) } } @@ -130,7 +130,7 @@ internal class Compress : CommandExecutor { .sumOf { 64 - it.amount } if (changeInDiamondBlocks > emptySlots + leftOverSpaceDiamondBlocks) { - sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: You do not have enough space in your ${if (isShulkerBox) "shulker box" else "inventory"} to compress all the Diamond-related items (+$changeInDiamondBlocks Diamond Blocks).")) + sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: You do not have enough space in your ${if (isShulkerBox) "shulker box" else "inventory"} to compress all the Diamond currency items (+$changeInDiamondBlocks Diamond Blocks).")) return@runOnMainThread RunOnMainThreadResult(true, null, null, null) } } @@ -138,7 +138,7 @@ internal class Compress : CommandExecutor { if (changeInShards < 0) { val removeMap = inventory.removeItem(Shard.createItemStack(abs(changeInShards))) if (removeMap.isNotEmpty()) { - sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond-related items in your ${if (isShulkerBox) "shulker box" else "inventory"}.")) + sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond currency items in your ${if (isShulkerBox) "shulker box" else "inventory"}.")) return@runOnMainThread RunOnMainThreadResult(true, null, null, null) } } @@ -146,13 +146,13 @@ internal class Compress : CommandExecutor { if (changeInDiamonds > 0) { val addMap = inventory.addItem(ItemStack(Material.DIAMOND, changeInDiamonds)) if (addMap.isNotEmpty()) { - sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond-related items in your ${if (isShulkerBox) "shulker box" else "inventory"}.")) + sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond currency items in your ${if (isShulkerBox) "shulker box" else "inventory"}.")) return@runOnMainThread RunOnMainThreadResult(true, null, null, null) } } else if (changeInDiamonds < 0) { val removeMap = inventory.removeItem(ItemStack(Material.DIAMOND, abs(changeInDiamonds))) if (removeMap.isNotEmpty()) { - sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond-related items in your ${if (isShulkerBox) "shulker box" else "inventory"}.")) + sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond currency items in your ${if (isShulkerBox) "shulker box" else "inventory"}.")) return@runOnMainThread RunOnMainThreadResult(true, null, null, null) } } @@ -160,14 +160,14 @@ internal class Compress : CommandExecutor { if (changeInDiamondBlocks > 0) { val addMap = inventory.addItem(ItemStack(Material.DIAMOND_BLOCK, changeInDiamondBlocks)) if (addMap.isNotEmpty()) { - sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond-related items in your ${if (isShulkerBox) "shulker box" else "inventory"}.")) + sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond currency items in your ${if (isShulkerBox) "shulker box" else "inventory"}.")) return@runOnMainThread RunOnMainThreadResult(true, null, null, null) } } else if (changeInDiamondBlocks < 0) { val removeMap = inventory.removeItem(ItemStack(Material.DIAMOND_BLOCK, abs(changeInDiamondBlocks))) if (removeMap.isNotEmpty()) { - sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond-related items in your ${if (isShulkerBox) "shulker box" else "inventory"}.")) + sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Something went wrong while trying to compress the Diamond currency items in your ${if (isShulkerBox) "shulker box" else "inventory"}.")) return@runOnMainThread RunOnMainThreadResult(true, null, null, null) } } @@ -199,7 +199,7 @@ internal class Compress : CommandExecutor { return@launch } - sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Successfully compressed all the Diamond-related items in your ${if (isShulkerBox) "shulker box" else "inventory"}!")) + sender.sendMessage(DiamondBankOG.mm.deserialize("${Config.prefix}: Successfully compressed all the Diamond currency items in your ${if (isShulkerBox) "shulker box" else "inventory"}!")) val shardsLine = if (changeInShards < 0) "|$changeInShards Diamond Shard${if (abs(changeInShards) != 1) "s" else ""}\n" else "" -- 2.47.3