Clearing Items in a chest, then re-setting them

Discussion in 'Plugin Development' started by orange451, Jan 7, 2013.

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

    orange451

    Hi,
    I've been trying to clear a chest's items, then restock the chest with new items. Now this creates a problem. I can clear the chest fine, but if I set the chests items RIGHT after clearing it, all of the previous items fly out of the chest as if someone had broken it.

    I use these two methods for clearing the chest (both create the same problem):
    Code:
    chest.getInventory().clear();
    Code:
    ItemStack[] contents = new ItemStack[chest.getInventory().getSize()];
    chest.getInventory().setContents(contents);
    
    I don't think the clearing is the problem. I believe that craftbukkit doesn't realize the chest was cleared when I add items to the chest after clearing.

    What do you think the problem is?
     
  2. Offline

    Jogy34

    add chest.update() after you clear the inventories. If that method doesn't exist then it would be block.getState().update() but I think chest is a BlockState so you should be fine with it.
     
  3. Offline

    fireblast709

    Chest extends BlockState, so it definitely has the update() method ;3. Besides, block.getState().update() might not update it correctly (as you edited the BlockState chest)
     
  4. Offline

    orange451

    Even when using .update, or when using .update(true) the items still fly out as if it did nothing.
     
  5. Offline

    orange451

    I still haven't solved the problem. Anyone?
     
  6. Offline

    orange451

    Turns out I overlooked your hypothesis. I was just using the BlockState object, and updating that. Once casted to a chest (and using .update), the problem went away. Thank you.
     
Thread Status:
Not open for further replies.

Share This Page