Files
src
build.gradle.kts
manifest.kod
Code
5 198
src/main/kotlin/com/kodari/planificador/TerrainWandListener.kt
package com.kodari.planificador
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.block.Action
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.inventory.EquipmentSlot
import org.bukkit.inventory.ItemStack
import org.bukkit.persistence.PersistentDataType
import java.util.UUID
class TerrainWandListener(private val plugin: PlanificadorPlugin) : Listener {
private val wandKey = NamespacedKey(plugin, "terrain_wand")
private val selections = mutableMapOf<UUID, Selection>()
fun giveWand(player: Player) {
val wand = ItemStack(Material.BLAZE_ROD)
val meta = wand.itemMeta
meta.displayName(net.kyori.adventure.text.Component.text("Varinha de Planificação"))
meta.lore(
listOf(
net.kyori.adventure.text.Component.text("Clique direito: primeiro ponto"),
net.kyori.adventure.text.Component.text("Clique esquerdo: segundo ponto")
)
)
meta.persistentDataContainer.set(wandKey, PersistentDataType.BYTE, 1)
wand.itemMeta = meta
player.inventory.addItem(wand)
player.sendMessage("§aRecebeste a varinha de planificação.")
}
@EventHandler
fun onWandUse(event: PlayerInteractEvent) {
if (event.hand != EquipmentSlot.HAND || !isWand(event.item)) return
val block = event.clickedBlock ?: return
val selection = selections.getOrPut(event.player.uniqueId) { Selection() }
when (event.action) {
Action.RIGHT_CLICK_BLOCK -> {
selection.first = block.location
event.player.sendMessage("§aPrimeiro ponto: §f${block.x}, ${block.y}, ${block.z}")
event.isCancelled = true
}
Action.LEFT_CLICK_BLOCK -> {
selection.second = block.location
event.player.sendMessage("§aSegundo ponto: §f${block.x}, ${block.y}, ${block.z}")
event.isCancelled = true
}
else -> Unit
}
}
fun flattenSelection(player: Player) {
val selection = selections[player.uniqueId]
val first = selection?.first
val second = selection?.second
if (first == null || second == null) {
player.sendMessage("§cSelecione dois pontos com a varinha: clique direito e depois esquerdo.")
return
}
if (first.world.uid != second.world.uid) {
player.sendMessage("§cOs dois pontos precisam estar no mesmo mundo.")
return
}
val minX = minOf(first.blockX, second.blockX)
val maxX = maxOf(first.blockX, second.blockX)
val minZ = minOf(first.blockZ, second.blockZ)
val maxZ = maxOf(first.blockZ, second.blockZ)
val area = (maxX - minX + 1L) * (maxZ - minZ + 1L)
if (area > MAX_COLUMNS) {
player.sendMessage("§cA área é demasiado grande. O limite é de $MAX_COLUMNS colunas.")
return
}
val world = first.world
val targetY = first.blockY
val selectedMaterial = first.block.type
val fillMaterial = if (selectedMaterial.isSolid) selectedMaterial else Material.DIRT
for (x in minX..maxX) {
for (z in minZ..maxZ) {
val highestY = world.getHighestBlockYAt(x, z)
if (highestY > targetY) {
for (y in highestY downTo targetY + 1) {
world.getBlockAt(x, y, z).setType(Material.AIR, false)
}
} else if (highestY < targetY) {
for (y in highestY + 1 until targetY) {
world.getBlockAt(x, y, z).setType(fillMaterial, false)
}
}
world.getBlockAt(x, targetY, z).setType(fillMaterial, false)
}
}
player.sendMessage("§aTerreno planificado à altura Y=$targetY.")
}
private fun isWand(item: ItemStack?): Boolean =
item?.itemMeta?.persistentDataContainer?.get(wandKey, PersistentDataType.BYTE) == 1.toByte()
private data class Selection(var first: Location? = null, var second: Location? = null)
private companion object {
const val MAX_COLUMNS = 2_500L
}
}