Head NBT code [UNSOLVED]

Discussion in 'Plugin Development' started by MYCRAFTisbest, Oct 28, 2012.

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

    yottabyte

    Been trying to figure this stuff out as well... Can't get the custom NBT data to show up in the crafting result no matter what item I try it with.
     
  2. Offline

    MYCRAFTisbest

    It is nice to know others are trying to figure this out. It seems very possible, but at the moment, no one has had luck.

    I think we are getting close though.

    What code did you try that isn't working?
     
  3. Offline

    p000ison

    Got it working! :p ofc you can replace the anonymous class with a "normal" one by implementing listener.

    Code:
            final ShapelessRecipe recipe = new ShapelessRecipe(new ItemStack(397, 1, (short) 0, (byte) 3));
            recipe.addIngredient(Material.DIRT);
            Bukkit.addRecipe(recipe);
     
     
            Bukkit.getPluginManager().registerEvents(new Listener() {
     
                @EventHandler
                public void onItemCraftEvent(CraftItemEvent event)
                {
                    ItemStack rawHead = new ItemStack(397, 1, (short) 3, (byte) 3);
     
                    //Get the crafting inventory and set the index 0 to the new owner head (0 = the item on the hand)
                    event.getInventory().setItem(0, setOwner(rawHead, "p000ison"));
                }
            }, this);
    EDIT: oh wait one thing is missing. one mom


    p000ison
     
  4. Offline

    MYCRAFTisbest

    Can't wait till u update with working code
     
  5. Offline

    p000ison

    It works the only problem is, that I do not know how to keep track about which recipe is for what player. The main problem is that the recipe in event.getRecipe() is not the same instance as the new ShapedRecipe(new ItemStack(397, 1, (short) 0, (byte) 3)); This makes it impossible to create a own version a a recipe :/. I tried to write a equal method which checks for Ingredients and the results, but the event.getRecipe method returns just unusable data. Maybe its a bug. If I craft something using a full crafting grid with wood this: System.out.println(recipe.getIngredientMap().values());
    prints this:[ItemStack{WOOD x 1}]. But it should print 9 x ItemStack{WOOD x 1}

    So maybe this is a bug. But I can not find an other method to check for equal recipies, since the reference changes after you add the recipe :/

    EDIT: maybe someone can confirm this bug, so I can report it :/

    EDIT: maybe we can try this event: PrepareItemCraftEvent if there is the same bug :/

    The cope above creates this a head everytime you craft anything :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  6. Offline

    MYCRAFTisbest

    Oh. Cant u use an if statement like
    if (var = 379) {
    <CODE>
    }
     
  7. Offline

    jbman223

    Works for me too but I have the same problem as you. How could we like compare recipes or something to the one they crafted?
     
  8. Offline

    MYCRAFTisbest

    Maybe we could set the item to a different item and check for that. For instance, 35:17 is unused but exists. Wool tends to loop and other then 35:16, all wool with metadata after the main wool looks exactly like a particular block of wool. Check for that and make it a head instead.

    I would try myself but, I MADE IT IN CURSE'S PLUGIN SPOTLIGHT OMG!!!:D

    EDIT: Finished interview, now i can try.
     
  9. Offline

    jbman223

    CONGRATS! Also, how do I change the metadata of a plugin?
     
  10. Offline

    p000ison

    Not sure what this should help. Im not sure what you want to do. If you only want to add 1 craftign recipe for one player the code above would work if you check getResult == Skull, because there is no skull recipe by default.

    Reported the bug here: https://bukkit.atlassian.net/browse/BUKKIT-2804

    But this equal method should work after the bug is fixed:
    Code:
        public static boolean equalRecipe(Recipe recipe, Recipe recipe1)
        {
            if (!recipe.getResult().equals(recipe1.getResult())) {
                return false;
            }
     
            if (recipe instanceof ShapelessRecipe) {
                if (!(recipe1 instanceof ShapelessRecipe)) {
                    return false;
                }
     
                ShapelessRecipe shapelessRecipe = (ShapelessRecipe) recipe;
                ShapelessRecipe shapelessRecipe1 = (ShapelessRecipe) recipe1;
     
                if (!shapelessRecipe.getIngredientList().equals(shapelessRecipe1.getIngredientList())) {
                    return false;
                }
     
            } else if (recipe instanceof ShapedRecipe) {
                if (!(recipe1 instanceof ShapedRecipe)) {
                    return false;
                }
     
                ShapedRecipe shapedRecipe = (ShapedRecipe) recipe;
                ShapedRecipe shapedRecipe1 = (ShapedRecipe) recipe1;
     
                if (!shapedRecipe.getIngredientMap().values().equals(shapedRecipe1.getIngredientMap().values())) {
                    return false;
                }
     
                if (!Arrays.equals(shapedRecipe.getShape(), shapedRecipe1.getShape())) {
                    return false;
                }
            }
     
            return true;
        }
    It can work atm too, but it's not secure :/


    EDIT: this would be my idea:

    Code:
        private Map<Recipe, String> recipes = new HashMap<Recipe, String>();
     
        @Override
        public void onEnable()
        {
             ShapedRecipe recipe = new ShapedRecipe(new ItemStack(397, 1, (short) 0, (byte) 3));
                    recipe.shape(
                    "DDD",
                    "DDD",
                    "DDD");
            recipe.setIngredient('D', Material.WOOD);
            Bukkit.addRecipe(recipe);
            recipes.put(recipe, "p000ison");
     
            ShapedRecipe recipe1 = new ShapedRecipe(new ItemStack(397, 1, (short) 0, (byte) 3));
            recipe1.shape(
                    "DDD",
                    "DDD",
                    "DDD");
            recipe1.setIngredient('D', Material.DIRT);
            Bukkit.addRecipe(recipe1);
            recipes.put(recipe1, "Notch");
     
     
            Bukkit.getPluginManager().registerEvents(new Listener() {
     
                @EventHandler
                public void onItemCraftEvent(CraftItemEvent event)
                {
                    Recipe recipe = event.getRecipe();
                    for (Map.Entry<Recipe, String> entry : recipes.entrySet()) {
                        if (equalRecipe(entry.getKey(), recipe)) {
                            ItemStack rawHead = new ItemStack(397, 1, (short) 3, (byte) 3);
     
                            //Get the crafting inventory and set the index 0 to the new owner head (0 = the item on the hand)
                            event.getInventory().setItem(0, setOwner(rawHead, entry.getValue()));
                        }
                    }
                }
            }, this);
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  11. Offline

    MYCRAFTisbest

    I didn't get much time to work on this much recently. Did anyone come up with a solution?

     
  12. Offline

    bobacadodl

    Sorta late, but I figured out how to do this :) Use protocol lib to hook into the packet send event and check if they are mousing over the player head in the crafting box, edit the packet info and set the item in the crafting box to the customized head item.
     
  13. Offline

    fireblast709

    ItemMeta is added, use that in combination with the crafting events
     
  14. Offline

    bobacadodl

    You can only actually change the items name/lore in an onItemCraft method, because if you put an item with NBTTags into a recipe, the NBTTags don't show up when the item gets crafted. Its either a bug with minecraft or bukkit. So if you want the name/lore to show up in the crafting inventory, I'm pretty sure you need to use protocol lib to change the packets
     
  15. Offline

    fireblast709

    if you set the ItemMeta in the PrepareItemCraftEvent, it should display it correctly, or it is a bug
     
  16. Offline

    bobacadodl

    Oh wow o_o never knew about that event. Thanks for that
     
  17. Offline

    stirante

    I have class for recipes which returns items with nbt data. It's in my pretty scary lib.
     
Thread Status:
Not open for further replies.

Share This Page