Question How do i get nbt tags

DruideMante

New member
Feb 24, 2023
5
0
1
I'm working on a custom tool but i need to get tool tags to know that it is not just a simple pickaxe. How can i do that?
 

RimeOrReason

New member
May 9, 2023
2
0
1
I don't know if there's a better way but you can use tr7zw's item nbt api, create a new NBTItem with the itemstack you have and then use hasTag and setString, setInt etc. methods according to what you need.
An example in kotlin:

Code:
val item = ItemStack(Material.HOPPER)
val nbtitem = NBTItem(item)
nbtitem.setString("collector", this::class.java.simpleName)
nbtitem.setString("material", material.name)
player.inventory.addItem(nbtitem.item)

An example in a listener to see if the tool has the tags:

Code:
@EventHandler
fun onPlace(event: BlockPlaceEvent) {
    if (event.isCancelled) return
    if (!event.itemInHand.hasItemMeta()) return
    val nbtitem = NBTItem(event.itemInHand)
    if (!nbtitem.hasTag("collector") || !nbtitem.hasTag("material")) return
    //whatever you want to do with the item
}