Added piston moving / retracting support to BlockUtils#isPlayerPlaced

This commit is contained in:
Auxilor
2021-12-14 08:30:34 +00:00
parent 507fad186a
commit 68e1f4afac

View File

@@ -1,12 +1,16 @@
package com.willfp.eco.internal.spigot.data package com.willfp.eco.internal.spigot.data
import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.util.BlockUtils
import org.bukkit.Location
import org.bukkit.block.Block import org.bukkit.block.Block
import org.bukkit.event.EventHandler import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority import org.bukkit.event.EventPriority
import org.bukkit.event.Listener import org.bukkit.event.Listener
import org.bukkit.event.block.BlockBreakEvent import org.bukkit.event.block.BlockBreakEvent
import org.bukkit.event.block.BlockMultiPlaceEvent import org.bukkit.event.block.BlockMultiPlaceEvent
import org.bukkit.event.block.BlockPistonExtendEvent
import org.bukkit.event.block.BlockPistonRetractEvent
import org.bukkit.event.block.BlockPlaceEvent import org.bukkit.event.block.BlockPlaceEvent
import org.bukkit.persistence.PersistentDataType import org.bukkit.persistence.PersistentDataType
@@ -36,9 +40,59 @@ class PlayerBlockListener(
} }
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun onExtend(event: BlockPistonExtendEvent) {
val locs = mutableListOf<Location>()
val toRemove = mutableListOf<Location>()
for (block in event.blocks) {
if (BlockUtils.isPlayerPlaced(block)) {
locs.add(block.getRelative(event.direction).location)
toRemove.add(block.location)
}
}
this.plugin.scheduler.run {
for (loc in toRemove) {
removeKey(loc)
}
for (loc in locs) {
writeKey(loc)
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun onRetract(event: BlockPistonRetractEvent) {
val locs = mutableListOf<Location>()
val toRemove = mutableListOf<Location>()
for (block in event.blocks) {
if (BlockUtils.isPlayerPlaced(block)) {
locs.add(block.getRelative(event.direction).location)
toRemove.add(block.location)
}
}
this.plugin.scheduler.run {
for (loc in toRemove) {
removeKey(loc)
}
for (loc in locs) {
writeKey(loc)
}
}
}
private fun writeKey(block: Block) { private fun writeKey(block: Block) {
val loc = block.location.hashCode().toString(16) writeKey(block.location)
block.chunk.persistentDataContainer.set( }
private fun writeKey(location: Location) {
val loc = location.hashCode().toString(16)
location.chunk.persistentDataContainer.set(
plugin.namespacedKeyFactory.create(loc.lowercase()), plugin.namespacedKeyFactory.create(loc.lowercase()),
PersistentDataType.INTEGER, PersistentDataType.INTEGER,
1 1
@@ -46,7 +100,11 @@ class PlayerBlockListener(
} }
private fun removeKey(block: Block) { private fun removeKey(block: Block) {
val loc = block.location.hashCode().toString(16) removeKey(block.location)
block.chunk.persistentDataContainer.remove(plugin.namespacedKeyFactory.create(loc.lowercase())) }
private fun removeKey(location: Location) {
val loc = location.hashCode().toString(16)
location.chunk.persistentDataContainer.remove(plugin.namespacedKeyFactory.create(loc.lowercase()))
} }
} }