Solved Get Chest Inventory

Discussion in 'Plugin Development' started by xDeeKay, Aug 6, 2014.

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

    xDeeKay

    I need to get the inventory of a chest and then clear it. Everywhere I've looked says to use getInventory or getBlockInventory, but no doubt this has changed.

    Here's my code:
    Code:java
    1. @EventHandler
    2. public void onBlockBreak(BlockBreakEvent e) {
    3. Block b = e.getBlock();
    4. Chest chest = (Chest) b.getState();
    5. if (b.getType().equals(Material.CHEST)) {
    6. chest.getInventory().clear();
    7. }
    8. }

    Much appreciated.
     
  2. Offline

    1Rogue Retired Staff

    You're casting before your type check (which will cause an exception when it isn't a chest). This is an XY problem, what are you attempting to achieve from clearing the inventory?
     
  3. Offline

    xDeeKay

    1Rogue I simply want to clear all contents from a chest when it's broken.
     
  4. Offline

    1Rogue Retired Staff


    Then you're doing just that. What's not working?
     
  5. Offline

    xDeeKay

    1Rogue The method getInventory() is undefined for the type Chest
     
  6. Offline

    Dragonphase

    xDeeKay

    Code:java
    1. chest.getBlockInventory().clear();
     
  7. Offline

    xDeeKay

  8. Offline

    Dragonphase

    xDeeKay

    Make sure you imported the right Chest. It's org.bukkit.block.Chest, not org.bukkit.material.Chest.
     
    xDeeKay likes this.
  9. Offline

    1Rogue Retired Staff


    Bukkit version?
     
  10. Offline

    xDeeKay

    Dragonphase You're amazing, thank you. I knew it would be something silly like that.
     
  11. Offline

    Dragonphase

    xDeeKay

    It's an honest mistake, everyone makes it every now and then :)
     
  12. Offline

    xDeeKay

    Dragonphase While this thread is here, I have another question. I'm trying to do the same thing for trapped chests, but looking at this list, I can't find it anywhere.
     
  13. Offline

    Dragonphase

    xDeeKay

    Code:java
    1. if (b.getType() == Material.CHEST || b.getType() == Material.TRAPPED_CHEST) ...
     
    xDeeKay and Marten Mooij like this.
  14. Offline

    xDeeKay

    Dragonphase Thanks for that. I didn't know I could use the same Chest import for trapped chests too, makes sense though.
     
    Dragonphase likes this.
Thread Status:
Not open for further replies.

Share This Page