Getting the direction of a diode/repeater

Discussion in 'Plugin Development' started by petrifiednightmares, Apr 19, 2011.

Thread Status:
Not open for further replies.
  1. So when the player is standing on a diode/repeater, I want to be able to get the direction it is facing and do something with that.

    I see that the Diode class has a .getFacing() method. However, I can only get the Block by doing a
    Code:
    Block b = player.getWorld().getBlockAt(player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ());
    How do I convert this Block to a Diode. No simply casting it like this: ((Diode) b).getFacing() doesn't work. It says cannot cast from a Block to a Diode.

    [cake]
     
  2. Offline

    eisental

    You can see in the javadocs that the Diode class extends MaterialData. Block.getState().getData() returns a MaterialData class for that block so you can use something like this:
    Code:
    Diode d = (Diode)player.getLocation().getBlock().getState().getData();
    if (d.getFacing()==BlockFace.NORTH) {
       // the diode direction is north.
    }
    
    Of course, don't forget to check that the block is actually a diode before trying to cast.
     
  3. Ok thanks! Does the diode have to be powered for this to work?
     
  4. Offline

    eisental

    Not that I know of...
     
  5. Offline

    krisrok

    you have to wait a tick to read the right data.. if you read the facing in a BlockPlaceEvent-handler, you'll always get EAST. so use a Task class or whatever to delay the access.

    applies to the delay and other events as well btw
     
  6. eisental
    Why use getState() if it's about MaterialData which is just ID and data values ?
    EDIT: actually, just create a new Diode instance directly =)
    Code:
    Diode diode = new Diode(block.getTypeId(), block.getData());
    Still petrifiednightmares a switch() with only block.getData() would be even faster, see wiki for values.

    Also, a powered diode has a different ID than a unpowered one, they're basically different blocks.
     
Thread Status:
Not open for further replies.

Share This Page