From a7cade9bf6362f3a1bd3429aa47a971ee211c00d Mon Sep 17 00:00:00 2001 From: Auxilor Date: Tue, 24 Aug 2021 14:59:28 +0100 Subject: [PATCH] Added check for block being player placed --- .../ecoskills/data/PlayerBlockListener.kt | 39 +++++++++++++++++++ .../effects/effects/EffectSpelunking.kt | 8 ++-- 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/PlayerBlockListener.kt diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/PlayerBlockListener.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/PlayerBlockListener.kt new file mode 100644 index 0000000..b026ae2 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/data/PlayerBlockListener.kt @@ -0,0 +1,39 @@ +package com.willfp.ecoskills.data + +import com.willfp.eco.util.NamespacedKeyUtils +import com.willfp.ecoskills.EcoSkillsPlugin +import org.bukkit.block.Block +import org.bukkit.event.EventHandler +import org.bukkit.event.EventPriority +import org.bukkit.event.Listener +import org.bukkit.event.block.BlockMultiPlaceEvent +import org.bukkit.event.block.BlockPlaceEvent +import org.bukkit.persistence.PersistentDataType + +class PlayerBlockListener( + private val plugin: EcoSkillsPlugin +) : Listener { + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + fun onPlace(event: BlockPlaceEvent) { + val block = event.blockPlaced + + writeKey(block) + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + fun onPlace(event: BlockMultiPlaceEvent) { + val block = event.blockPlaced + + writeKey(block) + } + + private fun writeKey(block: Block) { + val loc = block.location.hashCode().toString(16) + block.chunk.persistentDataContainer.set(NamespacedKeyUtils.create("ecoskills", loc.lowercase()), PersistentDataType.INTEGER, 1) + } +} + +fun Block.isPlayerPlaced(): Boolean { + val chunk = this.chunk + return chunk.persistentDataContainer.has(NamespacedKeyUtils.create("ecoskills", this.location.hashCode().toString(16)), PersistentDataType.INTEGER) +} \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectSpelunking.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectSpelunking.kt index 0f763e8..dc38037 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectSpelunking.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoskills/effects/effects/EffectSpelunking.kt @@ -2,13 +2,11 @@ package com.willfp.ecoskills.effects.effects import com.willfp.eco.core.drops.DropQueue import com.willfp.eco.util.NumberUtils +import com.willfp.ecoskills.data.isPlayerPlaced import com.willfp.ecoskills.effects.Effect import com.willfp.ecoskills.getEffectLevel -import org.bukkit.Bukkit import org.bukkit.Location import org.bukkit.Material -import org.bukkit.block.data.Ageable -import org.bukkit.enchantments.Enchantment import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.block.BlockBreakEvent @@ -39,7 +37,9 @@ class EffectSpelunking: Effect( val player = event.player - if (player.inventory.itemInMainHand.containsEnchantment(Enchantment.SILK_TOUCH)) { + val block = event.block + + if (block.isPlayerPlaced()) { return }