Filling empty slots in a players inventory

Discussion in 'Plugin Development' started by DragonGCBroZ, Jun 8, 2014.

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

    DragonGCBroZ

    So im making a Kit Plugin for someone but I got Stuck!
    When someone does /refill I want their inventoryslots that doesn't have MushRoom_Soup Bowls to get MushRoom_Soup Bowls...

    So Far this is my /refill code:
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("refill")){
    2. ItemStack[] inv = p.getInventory().getContents();
    3. boolean gotBowl = false;
    4. for (ItemStack slot : inv) {
    5. if ((slot != null) && (slot.getType() == Material.BOWL))
    6. {
    7. gotBowl = true;
    8. slot.setType(Material.MUSHROOM_SOUP);
    9. ItemMeta slotMeta = slot.getItemMeta();
    10. slotMeta.setDisplayName("§6[§b§lHoly§e§lSoup§6]");
    11. slot.setItemMeta(slotMeta);
    12. }
    13. }
    14. if (!gotBowl)
    15. {
    16. p.sendMessage(prefix + "§cYou have no empty bowls!");
    17. }
     
  2. Offline

    caseif

    I believe you'll need to add the new ItemStack to their inventory. I would recommend creating an ItemStack with the appropriate type and meta outside the loop, then just using a standard for loop to iterate from 0 to the inventory size and setting all slots to the ItemStack you created.
     
  3. Offline

    mine-care

    Try using a for loop like
    For(int I=0; I<36; I++){
    Add a itemstack to slot value of I
    }
    Hope I helped
     
  4. Well, all you want is to try and replace all items in their inventory with mushroom soup, so just go

    inv.clear();
    inv.addItem(new ItemStack(Material.Mushroom_Soup);
    inv.addItem(new ItemStack(Material.Mushroom_Soup);
    inv.addItem(new ItemStack(Material.Mushroom_Soup);
    inv.addItem(new ItemStack(Material.Mushroom_Soup);
    etc...

    But, if you wanted to replace all Bowls with mushroom soup, the easiest way to do it I can think of (as in easiest, like lazyest) would be to just do if (player.getInventory.getSlot(or however you get the slot).equals(Bowl)
    player.getInventory.getSlot.set(MushroomSoup)

    Also, dont be lazy and use the &4 and stuff codes. Use ChatColor.(COLOUR)! Makes your code look much more beatufil

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

    coasterman10

    What you're doing right now checks if the slot is an empty bowl, then changes it to a mushroom soup bowl if so. If you want to fill empty slots, you need to follow the suggestion of mine-care and then set the item at the index to a new item stack.
     
    mine-care likes this.
Thread Status:
Not open for further replies.

Share This Page