Solved CanDestroy NBT Tag not working

Discussion in 'Plugin Development' started by GeelSponsie, Feb 4, 2021.

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

    GeelSponsie

    I am trying to edit a wooden pickaxe's NBT tag to allow a player to break a certain block (I'll change the block type later, I just want to be able to break stone blocks for now). I can edit the tags and set the pickaxe in my inventory through Bukkit Code, and it has the tag CanDestroy, but it is unable to actually break the type of block (stone).

    I also gave myself a stone pickaxe using "/give <playername> stone_pickaxe 1 {CanDestroy:["minecraft:stone"]}" and this stone pickaxe can break stone, while the wooden pickaxe cannot.

    I used "/data get entity @s SelectedItem" to see if the tags actually applied, and they did. The only differences between the 2 pickaxes is the square brackets surrounding the "minecraft:stone", and when I have advanced tooltips enables, I can see "Can Break: Stone" while hovering over the stone pickaxe, but not the wooden one.
    Here are the tags:

    2021-02-04_15.09.40.png

    I am only using WorldGuard and Essentials plugins, and I have set up a region to test the pickaxe in. Only the stone pickaxe works in this region while in adventure mode. I tested everything while not opped.

    Here is the code to set the NBT tags:

    Code:
    public ItemStack hubPickaxes() {
    
            ItemStack item = new ItemStack(Material.WOODEN_PICKAXE);  
    
            net.minecraft.server.v1_16_R3.ItemStack hubPickaxe = CraftItemStack.asNMSCopy(item);
            NBTTagCompound hubPickaxeTag = hubPickaxe.getOrCreateTag();
            hubPickaxeTag.set("CanDestroy", NBTTagString.a("minecraft:stone"));  
            hubPickaxe.setTag(hubPickaxeTag);  
            item = CraftItemStack.asBukkitCopy(hubPickaxe);
    
            return item;
        }
    
    
     
  2. Offline

    GeelSponsie

    Got it to work with the help of a friend. You need to add the item as an NBTTagList to the NBTTagCompound. Here is the working code if anyone is interested:

    Code:
    public ItemStack hubPickaxes() {
    
            ItemStack item = new ItemStack(Material.WOODEN_PICKAXE);
            net.minecraft.server.v1_16_R3.ItemStack hubPickaxe = CraftItemStack.asNMSCopy(item);
            NBTTagCompound hubPickaxeTag = hubPickaxe.getOrCreateTag();
    
            NBTTagList list = new NBTTagList();
            list.add(NBTTagString.a("minecraft:stone"));
        
            hubPickaxeTag.set("CanDestroy", list); 
            hubPickaxe.setTag(hubPickaxeTag); 
            item = CraftItemStack.asBukkitCopy(hubPickaxe);
    
            return item;
    }
     
Thread Status:
Not open for further replies.

Share This Page