Solved Custom NBT on recipe result items

Discussion in 'Plugin Development' started by Digi, Feb 3, 2013.

Thread Status:
Not open for further replies.
  1. Hello, it's been a long while since I've played with Java, Minecraft and Bukkit and to avoid alot of research I want to ask you guys if there's any easy method to add custom NBT tags on results of recipes which can then be read by bukkit's crafting events. I need this to identify my plugin's internal recipe.

    I could just add a line to the description but that would be visible in the case that some other plugin is using the recipe and it doesn't know to remove that description line.

    EDIT:
    So I've tried this, but with no success, have I done something wrong or it's just not possible ?
    Code:
    ItemStack item = new ItemStack(Material.ANVIL);
    
    net.minecraft.server.v1_4_R1.ItemStack stack = CraftItemStack.asNMSCopy(item);
    
    if(stack.getTag() == null)
    	stack.tag = new NBTTagCompound();
    
    stack.getTag().setInt("rm_recipe", 214); // just a random value to see if I can read it afterwards...
    
    item = CraftItemStack.asCraftMirror(stack);
    
    // Then "item" is added as a result to a recipe...
    
    // In the PrepareItemCraftEvent:
    
    Player player = ((Player)event.getView().getPlayer());
    ItemStack item = event.getRecipe().getResult();
    
    net.minecraft.server.v1_4_R1.ItemStack stack = CraftItemStack.asNMSCopy(item);
    
    player.sendMessage("test = " + (stack.tag == null ? "NULL" : stack.tag.getInt("rm_recipe")));
    I always get NULL printed, recipe works just fine though.

    This is a blank plugin to only test this specific thing, no other plugins are running on the server.
     
  2. Offline

    KeybordPiano459

    Hey Digi, it's been awhile :p
    Anyhow, CraftBukkit is trying to stop users from using the NMS code and switch over to ItemMeta, which when combined with the methods that you can already call on ItemStacks, I'm pretty sure can do what you want. This may or may not fix the NULL thingy, I'm not too sure :p I didn't look extremely closely at the code.
     
  3. Offline

    Burnsalan18

    Are you wanting to give the crafted item a custom name and description?
     
  4. Offline

    stirante

    Well, not using NMS code is purpose of API :D. Once I created custom recipe class for shaped and shapeless recipe. These classes need updating to 1.4.7, but works.Link
     
  5. The API doesn't provide what I am asking :p I would've just used the API since it's easier.
    Also, I would've created a new enchantment but it's not allowing me to.

    And yeah I could just override the entire crafting system of MC, it will give me alot more control but it will also break very fast and there's parts of MC code that I don't really get and which I have to replace (like the tool repair system)... but anyway, I think I'll just use lore/description then :p

    No, I want to store an integer somewhere in the recipe so I can quickly know the index of my custom recipe to apply special stuff to the recipe when it is crafted.
     
  6. Offline

    Burnsalan18

    I still dont know what you mean, What exactly are you wanting to add to the crafted item?
     
  7. Offline

    Comphenix

    This appears to be by design - CraftBukkit will overwrite the custom tags whenever setItemMeta() is called (which I presume occurs somewhere in the recipe code). You can read more about that here:
    http://forums.bukkit.org/threads/nbt-tag-persistance.123394/#post-1507603

    The consensus seems to be to store auxiliary data in the "lore" section, and I'd tend to agree. At least, if you're not willing to override CraftItemFactory and add support for custom tags that way ...
     
    Ne0nx3r0 likes this.
  8. Offline

    Burnsalan18

    Heres what I use. I use the lore as the id as there is no possible way for players to edit lore in my server
    Code:
            // Scroll of Invisibility
            ItemStack is = new ItemStack(Material.PAPER, 1);
            ItemMeta im = is.getItemMeta();
            im.setDisplayName("Scroll of Invisibility");
            ArrayList<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GREEN + "Makes you invisible for 2:00");
            im.setLore(lore);
            is.setItemMeta(im);
            ShapedRecipe recipe = new ShapedRecipe(is);
            recipe.shape("  ", " *&", "  ");
            recipe.setIngredient('*', Material.DIAMOND);
            recipe.setIngredient('&', Material.PAPER);
            this.getServer().addRecipe(recipe);
               
            // Cure Scroll
            ItemStack is1 = new ItemStack(Material.PAPER, 1);
            ItemMeta im1 = is1.getItemMeta();
            im1.setDisplayName("Cure Scroll");
            ArrayList<String> lore1 = new ArrayList<String>();
            lore1.add(ChatColor.GREEN + "Restores health to 100%");
            im1.setLore(lore1);
            is1.setItemMeta(im1);
            ShapedRecipe recipe1 = new ShapedRecipe(is1);
            recipe1.shape("  ", "*&*", "  ");
            recipe1.setIngredient('*', Material.GOLDEN_APPLE);
            recipe1.setIngredient('&', Material.PAPER);
            this.getServer().addRecipe(recipe1); 
    And later on in the other part of the plugin my listener uses the Playerinteractevent and gets the metadata of the item.

    Code:
    if(p.getItemInHand().getItemMeta().getLore().contains(ChatColor.GREEN + "Makes you invisible for 2:00")){
          // Code here
    }
    
     
  9. Yes I've gone with using lore as a prefix for my IDs and set the whole string as black.
    The string won't be there for the players unless they use another plugin to craft my recipes which will bypass the result replacement.
    And lore editing is not a problem for me because I only need it when it comes out of the recipe, I delete the lore line afterwards.

    So I guess I'll mark as solved :}
     
  10. Offline

    DJSanderrr

    What does NBT means? :$$
     
  11. Offline

    stirante

    Named Binary Tag
     
  12. Offline

    DJSanderrr

    And what it does ;$?
     
  13. Offline

    stirante

    Stores data about entities and items.
     
  14. Offline

    DJSanderrr

Thread Status:
Not open for further replies.

Share This Page