[SOLVED] Getting block placement orientation

Discussion in 'Plugin Development' started by mushroomhostage, Feb 15, 2012.

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

    mushroomhostage

    You know how some blocks (pistons, furnaces, stairs, etc.) can be placed facing different directions?

    I'd like to calculate this orientation for any arbitrary block placement (not only blocks which can normally be oriented) in my BlockPlaceEvent listener. What is the best way to do this?

    I'm thinking I could compare the player location to the placed block location, but getting the intended placement orientation seems to be complicated..for example, if you're looking down and placing a piston (player Y > block Y), it will face up, but you could also be looking forward and up, so the Y coordinate comparison would be the same, but the piston (or whatever) will be facing horizontally, as the player expects. I suppose I could come up with an algorithm for the orientation or just look at the native code to see how it's done for pistons, but am I overlooking something, is there an easier way? If not anyone have any ideas how exactly I would compute the orientation? Do I only need the block and player location or am I missing some other essential information?
     
  2. Offline

    Njol

    if you're lucky you can use ((Directional) block.getData()).getFacing(), if not you can use block.getData().getData() to manually determine the facing of the block using the block's id and data value (You can find a list of data values on the wiki)
     
  3. Offline

    mushroomhostage

    Thanks but I'm looking to calculate the orientation for any arbitrary block, not get the orientation from the data value.

    Not all blocks use the data value for the orientation, and those that do don't always allow the full range (pistons can be placed facing up and down, but not dispensers). I'm actually trying to add new directional data, computed from how the player is oriented when he is placing a block, exactly how piston placement is computed. Can anyone help?

    Turns out I was approaching the problem from the wrong direction. You need only the player's yaw to calculate the horizontal orientation:
    Code:
    int l = MathHelper.floor_double((double)((entityplayer.rotationYaw * 4F) / 360F) + 0.5D) & 3;
    
    and from how much they are looking up or down, vertical orientation can be computed.

    There is a private method in BlockPiston ("c") that does exactly this. I'm calling it using reflection, works great. Now I can release my "dispensers point straight up" (and more) plugin :) BetterDispensers

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
Thread Status:
Not open for further replies.

Share This Page