Gabriel Victor

Crie uma varinha para planificar terreno para Minecraft bedrock

Kodari AI
KodariOct 11 at 12:00 pm 1
TerrainWand
A Minecraft Bedrock plugin that adds a magical wand tool for flattening terrain. Players use the wand to select two corner points of a rectangular area, then activate it to level the ground to a specified height. The plugin provides configurable flattening modes including fill-to-level, smooth, and preserve-natural options. Includes permissions for wand usage, cooldown management, block replacement settings, and visual selection preview particles. Supports undo functionality to revert terrain changes, and integrates with WorldEdit-style selection commands for advanced users.
  1. Criar o plugin Paper em Kotlin, compatível com jogadores Bedrock conectados via Geyser.

  2. Adicionar a varinha para marcar dois cantos e nivelar a área à altura do primeiro.

  3. Incluir comandos, permissões e limite de área para evitar operações excessivas.

Criado o plugin: /varinha entrega a varinha; clique direito marca o primeiro ponto e clique esquerdo marca o segundo. /planificar nivela a área na altura do primeiro ponto, usando o bloco selecionado como material de preenchimento. A área máxima é de 2.500 colunas.

É um plugin para servidor Paper; jogadores Bedrock podem usá-lo ao entrar por um servidor com Geyser. Não é um addon standalone para Bedrock.

Kodari can make mistakes. Review important code before using.
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
    }
}