Added BlockUtils#isPlayerPlaced

This commit is contained in:
Auxilor
2021-11-01 18:38:58 +00:00
parent 58d8f72cf5
commit ef701f1f86
3 changed files with 73 additions and 1 deletions

View File

@@ -2,12 +2,14 @@ package com.willfp.eco.util;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang.Validate;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
@@ -92,6 +94,21 @@ public class BlockUtils {
blockBreakConsumer.accept(player, block);
}
/**
* Get if a block was placed by a player.
*
* @param block The block.
* @return If placed by a player.
*/
public boolean isPlayerPlaced(@NotNull final Block block) {
Chunk chunk = block.getChunk();
return chunk.getPersistentDataContainer().has(
NamespacedKeyUtils.createEcoKey(Integer.toString(block.getLocation().hashCode(), 16)),
PersistentDataType.INTEGER
);
}
/**
* Initialize the block break function.
*

View File

@@ -24,6 +24,7 @@ import com.willfp.eco.proxy.SkullProxy
import com.willfp.eco.spigot.arrows.ArrowDataListener
import com.willfp.eco.spigot.data.DataListener
import com.willfp.eco.spigot.data.EcoPlayerProfileHandler
import com.willfp.eco.spigot.data.PlayerBlockListener
import com.willfp.eco.spigot.data.storage.DataHandler
import com.willfp.eco.spigot.data.storage.MySQLDataHandler
import com.willfp.eco.spigot.data.storage.YamlDataHandler
@@ -223,7 +224,8 @@ abstract class EcoSpigotPlugin : EcoPlugin(
GUIListener(this),
ArrowDataListener(this),
ArmorChangeEventListeners(this),
DataListener()
DataListener(),
PlayerBlockListener(this)
)
}
}

View File

@@ -0,0 +1,53 @@
package com.willfp.eco.spigot.data
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.util.NamespacedKeyUtils
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.BlockBreakEvent
import org.bukkit.event.block.BlockMultiPlaceEvent
import org.bukkit.event.block.BlockPlaceEvent
import org.bukkit.persistence.PersistentDataType
class PlayerBlockListener(
private val plugin: EcoPlugin
) : 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)
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun onBreak(event: BlockBreakEvent) {
val block = event.block
this.plugin.scheduler.run {
removeKey(block)
}
}
private fun writeKey(block: Block) {
val loc = block.location.hashCode().toString(16)
block.chunk.persistentDataContainer.set(
plugin.namespacedKeyFactory.create(loc.lowercase()),
PersistentDataType.INTEGER,
1
)
}
private fun removeKey(block: Block) {
val loc = block.location.hashCode().toString(16)
block.chunk.persistentDataContainer.remove(plugin.namespacedKeyFactory.create(loc.lowercase()))
}
}