Change a blocks (Chests, Diespenser,...) inventory

Discussion in 'Plugin Development' started by Zero9195, Sep 6, 2011.

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

    Zero9195

    hi Guys,
    my problem is the one in the title, I can't find a way to set the Inventory of a chest for example. For example something like this: chest.setInventory(Inventory). I load an inventory from my file and what to put it in the chest.
    Any help would be appreciated,
    Zero
     
  2. Offline

    Pencil

    I think you should look into Spout. Alot easier :)
     
  3. Offline

    Shamebot

    Why add a dependecy when it isn't necessary? Anyway I doubt they have something to access BlockInventories, it's easy enough.

    Code:java
    1. Block block = ... ;
    2. ContainerBlock cb =(ContainerBlock)block.getState();
    3. Inventory inv = cb.getInventory();
     
  4. Offline

    Pencil

    Spout is the former bukkitcontrib which offers a ton of stuff like easier inventory management >_> I never said it was necessary just easier D:
     
  5. Offline

    Bone008

    The "easier inventory management" is for client interactions. Bukkit's API for server-side inventories is completely functional, Spout/BukkitContrib just provides inventory events.
     
  6. Offline

    Pencil

    yeah I was just pointing out that it might be easier for stuff he wants to add in the future D:
     
  7. Offline

    Zero9195

    Thanks for yout help, by i need to SET the Inventory, and not get it. If I now change something in "inv" for example "inv.addItem(new ItemStack(1))" then it wouldn't actually change he blocks inventory wouldn't it?
     
  8. Offline

    andersonhc

    Code:java
    1.  
    2. Block block = ... ;
    3. ContainerBlock cb =(ContainerBlock)block.getState();
    4. Inventory inv = cb.getInventory()
    5.  
    6. if (inv.firstEmpty() >= 0) {
    7. inv.setItem(inv.firstEmpty(), new ItemStack(1));
    8. } else {
    9. //inventory is full
    10. }
    11.  


    With an inventory you have an array of ItemStacks. You can get, set, remove, clear, etc....
     
  9. Offline

    Shamebot

    You need to call .update().
    Code:java
    1. Block block = ... ;
    2. BlockState state = block.getState();
    3. ContainerBlock cb = (ContainerBlock)state;
    4. Inventory inv = cb.getInventory();
    5. ...
    6. state.update();
     
  10. Offline

    Zero9195

    @Shamebot
    Thank you! That was what I was searching for :D
     
  11. Offline

    Afforess

    We sure do. You can cast chests to SpoutChests to easily check for double inventories or get the entire double inventory of chests as a single object, or create inventories from scratch from our inventory builder.

    http://jd.getspout.org/org/getspout/spoutapi/block/SpoutChest.html

    http://jd.getspout.org/org/getspout/spoutapi/inventory/InventoryBuilder.html

    All server side.
     
    Pencil likes this.
  12. Offline

    Pencil

  13. Offline

    Shamebot

    I don't see the need, checking the adjacent blocks is easy, and he doesn't need both inventories in one.
    What would he need it for?
    Obviously. But it's a additional dependency which isn't really necessary.

    BTW, did you see my post regarding the PlayerKickEvent? I tagged SpoutDev.
     
  14. Offline

    Afforess

    My post was off-topic, I agree, it's not nessecary for the OP's question.
     
Thread Status:
Not open for further replies.

Share This Page