Getting the Inventory of a Chest

Discussion in 'Plugin Development' started by jis2507, Feb 12, 2012.

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

    jis2507

    I can't seem to figure out how to get the inventory of a chest. Based on looking through the JavaDocs I've come up with this:

    (I want to be able to get a chest at a particular location, whose coords have previously been defined, and then add items to it).

    Code:
    Location loc = new Location(p.getWorld(), x, y, z);
    BlockState state = loc.getBlock().getState();
    Inventory inv = ((ContainerBlock) state).getInventory();
    ItemStack apples = new ItemStack(Material.APPLE, 10);
    inv.addItem(apples);
    
    I've also tried casting it to a type of "Chest" but neither has worked.

    All help appreciated :D

    BUMP. Seriously, I'm sure this isn't that hard!! can't somebody just tell me how to chest the inv? I'm looking for a full on tutorial...just how to go about it...and the answer to why what I'm doing is wrong. Thanks.

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

    Roadkill909

    Let me make this abundantly clear: I have never worked with this before, nor have I tested this. I spent 30 seconds messing around with bukkit and eclipse suggestions to see what might work based off of previous experience. This may not work based off of how difficult inventory handling can be, but it may point you in the right direction. All I know is this will compile:

    ((CraftChest) block.getState()).getInventory()

    Normally for things like this you may need to use Craftbukkit or the net.minecraft.server files, not normal bukkit. There are pros and cons to using CraftBukkit, so be sure to look into those before using it.

    Edit: also your code isn't cautious enough.
    If the block is null at that location, return/ stop and prompt the user. Or you'll get a null pointer.
    If it's not an instance of ContainerBlock/craftchest, return/ stop and prompt the user or you'll get a classcast exception.
    Check if the inventory's first empty is greater than 0, otherwise the inventory is full and it'll throw an outofbounds exception.
     
  3. Offline

    AgentME

    Why would you cast to a CraftChest? Nothing you're doing requires hooking into CraftBukkit. Just casting it to a Chest and linking against Bukkit should be enough.

    What doesn't work about that code? (Or when you cast to a Chest?)
     
  4. Offline

    jis2507

    I get a "cannot cast to type of chest/container block" error. I KNOW the coords of the block are fine, as I retrieve them by placing a chest. The code above is an in onBlockPlace event (if block type is chest etc. etc.)
     
  5. Offline

    AgentME

    Can you log the block type in the onBlockPlace event just to be sure it's a chest?
     
  6. Offline

    jis2507

    ANOTHER BUMP!

    Seriously, for something like this, shouldn't it be in the wiki or something? All that I could find was player inventory stuff - which only HINTED at chest inventory manipulation.... :(
     
  7. Offline

    nisovin

    The code you have should be fine. I'd guess you're doing something else wrong. If you're trying to do this from within a BlockPlaceEvent, then that would probably be the problem. The block is not officially a chest until after the event has run.
     
  8. Offline

    rymate1234

    correct me if i'm wrong (on phone here) but don't chests have a getInventory() method? So the code would be something like: Block block = loc.getBlock() if (block.getType() == Material.Chest()) Inventory inv = (Chest) block.getInventory
     
  9. Offline

    ryanhamshire

    Help! I can't figure this out, either. I have a similar problem - on block damaged event, I want to get the inventory of the chest. (Yes, I have checked that its material == Material.Chest), but I don't know how to get the inventory from the Block. No, casting it to a Chest does not work (in response to the previous post).
     
  10. Offline

    -_Husky_-

    I got a problem too!

    Inventory inv = ((Chest) state).getInventory();

    works.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
Thread Status:
Not open for further replies.

Share This Page