Solved Trouble creating item stacks with potion data

Discussion in 'Plugin Development' started by Moon_werewolf, Sep 15, 2016.

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

    Moon_werewolf

    So I'm trying to create recipes to make tipped allows for all potion type (including splash). But so far the only arrow I can make is Arrow of Splashing with no effect. I have searched around, but have found nothing that helps me.

    Code:
    //Make arrows of any type
    for(PotionType potionType : PotionType.values()){
        //Create the tipped arrow with potion data
        ItemStack arrow = new ItemStack(Material.TIPPED_ARROW, 8);
        ((PotionMeta)arrow.getItemMeta()).setBasePotionData(new PotionData(potionType));
    
        //Create the potion used in the recipe
        ItemStack potion = new ItemStack(Material.POTION);
        ((PotionMeta)potion.getItemMeta()).setBasePotionData(new PotionData(potionType));
    
        ShapedRecipe tippedArrowRecipe = new ShapedRecipe(arrow);
        tippedArrowRecipe.shape("AAA", "APA", "AAA");
        tippedArrowRecipe.setIngredient('A', Material.ARROW);
        tippedArrowRecipe.setIngredient('P', potion.getData());
        server.addRecipe(tippedArrowRecipe);
    }
    and I have no idea how to make splash potions, all I can find are out of date.
     
  2. Offline

    Zombie_Striker

    @Moon_werewolf
    You will need to test the values of a tipped arrow in order to know how the potion effects are attached. I recommend doing the following:
    1. Make a command suchas "testItem".
    2. In that command, print out the potion effects, durability level, and all metadata tags attached to the item in the player's hand. Also check the instanceof the item meta (because I do not think a tipped arrow has potionMeta).
    3. In-game, issue the command twice, holding a different tipped arrow each time.
    4. Post all the values here.
     
  3. Offline

    JanTuck

    Last edited: Sep 15, 2016
  4. Offline

    Moon_werewolf

    So I did some bug testing, and spawned all arrows and potions into a chest, with this code

    Code:java
    1.  
    2. for(PotionType potionType : PotionType.values()){
    3. //Create the tipped arrow with potion data
    4. ItemStack arrow = new ItemStack(Material.TIPPED_ARROW, 8);
    5. PotionMeta arrowMeta = (PotionMeta)arrow.getItemMeta();
    6. arrowMeta.setBasePotionData(new PotionData(potionType));
    7. arrow.setItemMeta(arrowMeta);
    8.  
    9. //Create the potion used in the recipe
    10. ItemStack potion = new ItemStack(Material.POTION);
    11. PotionMeta potionMeta = (PotionMeta)potion.getItemMeta();
    12. potionMeta.setBasePotionData(new PotionData(potionType));
    13. potion.setItemMeta(potionMeta);
    14.  
    15. inv.addItem(potion);
    16. inv.addItem(arrow);
    17. }
    18.  


    And all arrows and potions have their proper name, and effect. But when I try to craft any of them, I always get Arrow of Splashing, so by that logic something must be wrong with

    Code:java
    1.  
    2. ShapedRecipe tippedArrowRecipe = new ShapedRecipe(arrow);
    3. tippedArrowRecipe.shape("AAA", "APA", "AAA");
    4. tippedArrowRecipe.setIngredient('A', Material.ARROW);
    5. tippedArrowRecipe.setIngredient('P', potion.getData());
    6. server.addRecipe(tippedArrowRecipe);
    7.  
     
  5. Offline

    Zombie_Striker

    @Moon_werewolf
    This may be your issue. Bukkit does not care about the "durability" (or data) of an item. All bukkit cares about is if the Materials match.

    To solve this, use PrepareItemCraftEvent. If the player is crafting this item and if the item in the center slot is equal to a potion, then set the "result's" durability equal to the potions durability.
     
    Moon_werewolf likes this.
  6. Offline

    Moon_werewolf

    Thanks so much, I was able to fix it with that event. What I did was taking the PotionMeta from the crafting recipe and applied it onto the resulting item.

    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.LOW)
    3. public void onPrepareItemCraftEvent(PrepareItemCraftEvent event){
    4. ItemStack[] items = event.getInventory().getContents();
    5.  
    6. //Check if the crafting recipe is correct
    7. //0 = The result, so we skip it
    8. for(int i=1; i<items.length; i++)
    9. if(!tippedArrowShape[i-1].equals(items[i].getType()))
    10. return;
    11.  
    12. //Get the meta data from the potion in the crafting recipe
    13. PotionMeta potionMeta = (PotionMeta)items[5].getItemMeta();
    14.  
    15. //Change the resulting arrow stack, into the correct type
    16. PotionMeta arrowMeta = (PotionMeta)items[0].getItemMeta();
    17. arrowMeta.setBasePotionData(potionMeta.getBasePotionData());
    18. items[0].setItemMeta(arrowMeta);
    19. }[/i]
     
    Last edited: Sep 17, 2016
  7. Offline

    bwfcwalshy Retired Staff

    @Moon_werewolf Why are you making a for starting at 1 just to remove 1? Start it at 0 no need for the -1
     
  8. Offline

    Moon_werewolf

    @bwfcwalshy For whatever reason the code didn't paste correctly, now it should make a little more sense
     
Thread Status:
Not open for further replies.

Share This Page