Dispenser type cast

Discussion in 'Plugin Development' started by Nitnelave, Jun 5, 2011.

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

    Nitnelave

    That might be a silly question, but how do I get a Dispenser variable at a given location? I wanted to do a type cast on player.getTargetBlock(), but it tells me "org.bukkit.craftbukkit.block.CraftBlock cannot be cast to org.bukkit.material.Dispenser"
    So how do I get a Dispenser variable representing the block the user faces (BTW, I checked before that it WAS a dispenser)? Or even a containerblock...

    Oh, wait, nevermind, I just had to do a cast on the Block.getState(). Any idea why?

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

    DreadKyller

    because the block is not a dispenser, the getState returns a more precise object of what the block it, the block holds the material data for the block, the getState returns the block data or something (not sure the exact words), in which holds stuff like chest data, furnace data, door data (have to use the getState().getData() for doors) dispenser data, etc...

    also, just incase the block is not a dispenser, you should really say:

    if(block.getState() instanceof Dispenser){

    otherwise you may get some errors.
     
  3. Offline

    Nitnelave

    Ok, thanks! Didn't know about the blockstate until an hour ago :p
     
Thread Status:
Not open for further replies.

Share This Page