Solved Placing items into the first empty inventory slot

Discussion in 'Plugin Development' started by Dpasi314, Jan 20, 2013.

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

    Dpasi314

    Greeting Bukkiteers,
    I'm making a Dynamic Shop, and I was just wondering how to place bought items into the first inventory slot that's open. Thanks for your help!
     
  2. Offline

    chasechocolate

    player.getInventory().addItem(ItemStack);
     
  3. Offline

    Craftiii4

    Best to also check that they have enough room in their inventory first as well
     
  4. Offline

    Dpasi314

    Thank you much.
    Noted.

    ~Solved~

    chasechocolate
    What about getting a certain amount of an item into the inventory slot?

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

    skipperguy12

  6. Offline

    Dpasi314

    So that's what I got from that thread:
    Code:
    @SuppressWarnings("deprecation")
        public void buyCommand(Material item, int amount, Player player){
            ItemStack items = new ItemStack(item, amount);
            player.getInventory().addItem(items);
            player.updateInventory();     
         
        }
    Correct?
     
  7. Offline

    skipperguy12

    Sounds legit. Export it and see if it works! c:
     
  8. Offline

    Dpasi314

    I still have some stuff to do :3 But I shall test soon!
     
  9. You should use a routine to check if there's enough place.
    Then decide if you want to drop it to the ground or cancel the trade!

    Code:
    if (inventory.firstEmpty() == -1){
        // if inventory is full, drop it to the ground (item is a ItemStack)
        player.getWorld().dropItem(player.getLocation().add(0, 1, 0), item);
    } else{
        // if there is a empty place, put it in
        int newItemSlot = inventory.firstEmpty();
        inventory.setItem(newItemSlot, item);
     }
    
     
  10. Offline

    Dpasi314

    Noted. Thanks!
     
  11. Offline

    fireblast709

    That would not allow you to stack items. If you want to know what could fit and what not, check the HashMap<Integer, ItemStack> it returns
     
  12. Offline

    Skatedog27

    How to give an item and a certain amount of that Item:
    Code:java
    1. player.getInventory().addItem(new ItemStack(Material.[Material Name], [Amount of that item]))
     
Thread Status:
Not open for further replies.

Share This Page