Metadata doesn't work, help me!

Discussion in 'Plugin Development' started by Bananinha, Apr 23, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    Bananinha

    Hello guys, this is my first post here on the forum, so I have a code where I type a command:
    ( /tnt 1 1 player 1 ), and I get a tnt with lore, and metadata. So far so good, the problem starts here:
    Code:
    @EventHandler
        fun boostTnt (e: EntityExplodeEvent) {
            Bukkit.broadcastMessage("Algo explodindo no mundo...") //Just for debug.
            val entityTypeEvent = e.entityType
            if (e.entity.hasMetadata(TNTBOOST)) {
                Bukkit.broadcastMessage("Tnt Capturada...") //Just for debug
                e.isCancelled = true
                e.entity.removeMetadata(TNTBOOST, plugin)
            }
        }
    
    Code:
     if (e.entity.hasMetadata(TNTBOOST))
    In this line I check if that entity has Metadata, however I notice that there is an error in this line, because when I run the plugin it does not perform any action within that if. Well, obviously I'm doing something wrong ... So keep my code:

    EVENTS:
    Code:
    
    class EventMaster : Listener{
        private var plugin: JavaPlugin? = null
    
        val TNTBOOST = "tntboost"
    
        @EventHandler
        fun boostTnt (e: EntityExplodeEvent) {
            Bukkit.broadcastMessage("Algo explodindo no mundo...") //Just for debug.
            val entityTypeEvent = e.entityType
            if (e.entity.hasMetadata(TNTBOOST)) {
                Bukkit.broadcastMessage("Tnt Capturada...") //Just for debug
                e.isCancelled = true
                e.entity.removeMetadata(TNTBOOST, plugin)
            }
        }
    
        @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
        fun boostTntSetData (e: BlockPlaceEvent) {
            try {
                val itemPlayerHand: ItemStack = e.player.itemInHand
                if (itemPlayerHand.itemMeta.displayName != null) {
                    if (itemPlayerHand.itemMeta.displayName == "Tnt de impulssão!") {
                        e.blockPlaced.setMetadata(TNTBOOST, FixedMetadataValue(plugin, "boostTnt"))
                    }
                }
            } catch (e: Exception) {}
        }
    }
    
    Commands:
    Code:
    
    class CommandsMaster : CommandExecutor{
        var boostTntLore = listOf("Uma incrivel tnt de impulssão ( ELA NÃO DESTROI BLOCOS ), com essa amiguinha torne seus canhões POTENTES!!")
        override fun onCommand(sender: CommandSender?, comando: Command?, label: String?, args: Array<out String>?): Boolean {
    
            if (sender !is Player) {
                if (sender != null) {
                    sender.sendMessage("Comando indisponivel no console.")
                }
            }
    
            if (comando != null) {
                if (comando.name.equals("tnt", ignoreCase = true)) {
                    if (args?.size == 1) {
                        sender?.sendMessage("Favor utilize: /tnt <tipo> <player> <quantidade>")
                    } else if (args?.size == 2) {
                        sender?.sendMessage("Favor utilize: /tnt <tipo> <player> <quantidade>")
                    } else if (args?.size == 3) {
                        sender?.sendMessage("Favor utilize: /tnt <tipo> <player> <quantidade>")
                    } else if (args?.size == 4) {
                        val playerName: String = args[2]
                        val target: Player? = sender?.server?.getPlayerExact(playerName)
                        if (target == null) {
                            sender?.sendMessage("O jogador esta offline ou não existe.")
                            return true
                        }
    
                        var boostTnt: ItemStack = ItemStack(Material.TNT)
                        boostTnt.addUnsafeEnchantment(Enchantment.DURABILITY, 2)
                        var boostTntMeta: ItemMeta = boostTnt.itemMeta
                        boostTntMeta.displayName = "Tnt de impulssão!"
                        boostTntMeta.lore = boostTntLore
                        boostTnt.setItemMeta(boostTntMeta)
                        target.inventory.addItem(boostTnt)
    
                    }
                }
            }
            return true
        }
    }
    
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Bananinha The fact that it is Kotlin does not make it easier.
    itemPlayerHand.itemMeta.displayName == "Tnt de impulssão!"
    That would not work in Java, no idea how Kotlin handles it.

    If it would be java then I could take a look, till then you might be on your own.
     
Thread Status:
Not open for further replies.

Share This Page