Getting a Chest with knowing the location

Discussion in 'Plugin Development' started by pd9937, Jul 5, 2012.

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

    pd9937

    I got the following problem:
    I have a instance of Location(), and I know it points to a chest (I have even tested it). I want to check the inventory of the chest, but how do I get the instance of Chest()?

    I have already tried the following, but it produces a null pointer exception:
     
  2. if you have an block, you can get the state, and the state can be an instance of a chest
     
  3. Offline

    pd9937

    But how can I get the Chest() instance now?
    Casting the state to a chest directly produces a null pointer exception again...
     
  4. where does the exception come from, from whits substatement?
     
  5. Offline

    pd9937

    Well, as far as I have tested the getWorld().getBlockAt(myLocation).getState(); works fine, it just doesn't want me to cast the state to a chest. (so I am pretty sure it is the 'ch = (Chest)')
     
  6. Offline

    bergerkiller

    pd9937 import the 'state' version of Chest, not the 'material' version of Chest. They are two different classes. Just like signs, furnaces and dispensers have.
     
  7. Offline

    pd9937

    Code:
    import org.bukkit.Block.Chest;
    ...
    ...
    BlockState st = mCurrentWorld.getBlockAt(loc).getState();
    Chest ch = (Chest) st;
    Still produces a cast error, while Block.Chest is the only import for chest I found in the reference with ctfl+f.
     
  8. Offline

    nisovin

    Well... if it's throwing a cast exception... are you sure there's actually a chest there?
     
  9. Offline

    pd9937

    Well, if I add this, the output is "CHEST":
    Code:
    getLogger().info(mCurrentWorld.getBlockAt(loc).getType().toString());
     
  10. Offline

    Megolas

    Try
    Chest ch = (Chest) getWorld().getBlockAt(myLocation).getState();
    worked for me
     
  11. Offline

    LucasEmanuel

    You sure you imported the correct package?
     
  12. what is the result of getWorld().getBlockAt(myLocation).getState().getClass().getName()
     
  13. Offline

    pd9937

    Really strange... I have got 2 chests to load, both are chests (I checked the coords) and I get two different results from them: Once org.bukkit.craftbukkit.block.CraftChest and once org.bukkit.craftbukkit.block.CraftBlockState.
     
  14. if you get an CraftChest, you can cast it to org.bukkit.block.Chest
    else, its not an chest
     
Thread Status:
Not open for further replies.

Share This Page