Give Player Items?

Discussion in 'Plugin Development' started by Musicguy, Sep 17, 2012.

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

    Musicguy

    Hey, how do you give a player two different items? Example: Player types "/food" and it gives them Soup AND bread. How would I go about doing this? Thanks in advance!
     
  2. Offline

    slater96

    p.getInventory.add(new ItemStack(Material.MUSHROOM_SOUP, 1));
    p.getInventory.add(new ItemStack(Material.BREAD, 1));
     
    spideynn likes this.
  3. Offline

    kyle1320

    Musicguy
    You can also use an array to add multiple items at once.
    Code:
    ItemStack[] items = {new ItemStack(Material.BREAD), new ItemStack(Material.MUSHROOM_SOUP)};
    player.getInventory().addItem(items);
     
  4. Offline

    Musicguy

    kyle1320 Thanks, really helped! How would I add enchanted stuff? Say an a iron sword enchanted with Sharpness II ?

    Also, how would you make the items not stack?

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

    kyle1320

    Take a look at http://wiki.bukkit.org/Plugin_Tutorial#Enchantments
    Code:
    for (ItemStack i : items){
        inv.setItem(inv.firstEmpty(), i);
    }
    Should work
     
  6. Offline

    Musicguy

    Ok, thanks. Could you show me an example of how the second one is used? kyle1320
     
  7. Offline

    kyle1320

    Code:
    ItemStack[] items = {new ItemStack(Material.BREAD), new ItemStack(Material.MUSHROOM_SOUP)};
    PlayerInventory inv = player.getInventory();
    for (ItemStack i : items){
        inv.setItem(inv.firstEmpty(), i);
    }
     
  8. Offline

    Musicguy

    So, how would I get 26 Mushroom Soup in 1 soup per slot?
     
  9. Offline

    zack6849

    Add them one at a time

    this is pseudo code, not sure if i typed this correctly, so don't complain if copy + psting this doesn't work right :confused:
    Code:
    if(cmd.getName().equalsIgnoreCase("food"){
        for(int i = 0 : i <26){
          Player.getInventory.add(new ItemStack(Material.BREAD, 1))
            i++
        }
    return true;
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  10. Offline

    Infamous Jeezy

    I am not sure if they fixed this but when mushroom soup is stacked and you eat one, it consumes them all. Actually I just tested you cannot stack them anymore, but I'm not sure if the same thing would happen if you were to forcefully stack them with a plugin. Although, I suppose you could just re-add them minus the one you consumed if it didn't remove them all. Not sure if this could lead to lag but I doubt it, good luck!
     
  11. Offline

    Musicguy

    Thanks, worked a charm!
     
  12. Offline

    zack6849

    No problem! :)
     
  13. Heres for Enchants :)


    Code:
            ItemStack item1 = new ItemStack(Material.WOOD_SWORD,1);
            item1.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1);
            player.getInventory().addItem(item1);
    If needed 2 or more items change item1 to item2 in that line of code, also you can change the material.
     
  14. Offline

    Musicguy

    Thanks, I was having trouble understanding the tutorial that kyle sent me.
     
Thread Status:
Not open for further replies.

Share This Page