Finding Empty Space In A Chest?

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

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

    Warreo

    Alright so here is my problem, I'm putting items into a chest, I need to check if there is room in the chest for the item(s) to fit. Now first there is two things we are assuming. 1) Any item being added is the same item. 2) Said item stacks to 64.

    Now, this is what I had tried and it didn't work the way I thought it would. I will take any suggestions on how to accomplish this.

    (m is the material being put into the chest)
    PHP:
    int total 0;
     for(
    ItemStack i chest.getInventory().getContents()){
         if(
    == null){
           
    total += 64;
         }
          if(
    != null && it.getTypeId() == m.getId()){
            
    total += i.getAmount();
          }
     }
    Thanks!
     
  2. What exactly do you want the plugin to do ?

    Tell how many and what are in chests ?

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

    Warreo

    I need to find the amount of free space left in a chest. So 1 slot is = 64 free.
     
  4. Hmm let me check some of my code

    Now what you could do is loop through the whole chest and set a variable for each slot that == null then multiply that by 64

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

    Warreo

    That's exactly what I've been trying, this is what I was using in my for loop:

    if(i == null){
    total += 64;
    }
     
  6. Try this type of loop not the exact code though. I toke this from one of my plugins

    Code:
    private ItemStack[] items;
     
    ItemStack[] tempInv = new ItemStack[items.length];
            for (int i=0; i<items.length; i++) {
                ItemStack is = items[i];
                if(is != null)
                    tempInv[i] = is.clone();
               
            }
     
  7. Offline

    Warreo

    I found a way that it was much easier. Feel free to use the code :D

    PHP:
    int slots c.getInventory().getSize();
    int totalSpace 0;
    if(
    slots == 27){
        
    totalSpace 1728;
    } else if(
    slots == 54){
        
    totalSpace 3456;
    }   
           
    for(
    ItemStack it c.getInventory().getContents()){                                       
        if(
    it != null ){
            
    totalSpace -= it.getAmount();
         }
    }
     

  8. Thanks but I don't need it. :D
     
  9. Offline

    Warreo

    Alright :D well thanks for the help anyway man! :D
     
  10. No problem :D
     
Thread Status:
Not open for further replies.

Share This Page