Solved Are Door/TrapDoor both 'Openable'?

Discussion in 'Spigot Plugin Development' started by PRLL001, Mar 1, 2020.

  1. I'm currently trying to make sure a block is either a Door or a TrapDoor and to make things easier I'm doing a conditional which validates if the block is an instance of the interface Openable, because in the documentation I saw them both extending Openable. When the block is a door it works just fine, but not for a TrapDoor...

    Here's a snippet of the code I'm using:

    Code (Java):

    if(block.getState().getData() instanceof Openable){
        //do code
    }
     
    Is there any way this could be done?

    Thanks in advance for any advice!
     
  2. Your snippet shows the wrong Door class (org.bukkit.material.Door). The non-deprecated org.bukkit.block.data.type.Door extends from BlockData but implements the same interfaces :x

    As to OP, for the reason mentioned above, you should be checking #getBlockState instanceof Openable, rather than BlockState#getData.
     
    #3 Escad, Mar 1, 2020
    Last edited: Mar 2, 2020
  3. getBlockState is not a thing, its Block#getState

    On that note, openable its not a block state, its a block data
    So you would get the block data via Block#getBlockData ... cast it to openable, do what you need to do, then set that block data back to the block.
     
    • Winner Winner x 1
  4. Ahh, so you're right. My bad
     
    • Friendly Friendly x 1
  5. Just as @ShaneBee stated, to find if an instance of a block is Openable, no matter if it's a Door or Trapdoor, I used:

    Code (Java):
    if(block.getBlockData() instanceof Openable){
        //do code
    }