How do I get the Location of where the player is looking

Discussion in 'Plugin Development' started by GeekPlaya, Dec 19, 2011.

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


    I am trying to get the location of where the player was looking. I thought it was getEyeLocation() but apparently that's just where they are standing..
  2. Offline

    Father Of Time

    This might help if you are wanting what block they are looking at.

  3. Offline


  4. Offline

    Father Of Time

    Do a block iteration trace, find the first block the trace comes in contact with that isn't air and return the location of the block; same principle, different execution. :D

    Check out block iterators, I'm pretty sure they can assist you.
  5. just use player.getTargetBlock();
  6. Following what @se1by said, heres the code...
    Code (Text):
    1. public Block getSelectedBlock(Player p) {
    2.    Block b = p.getTargetBlock(200, null);
    3.    return b;
    4. }
    So using that, I could just do something like this...
    Code (Text):
    1. if (getSelectedBlock(p).equals(Material.TNT)) {
    2.    //The block is tnt
    3. }
    kalicat, LemonLake and hammale like this.
  7. I know this is n old post but for everyone who couldnt figure this out the code would be this:
    1. Player player = (Player) sender;
    2. Block block = player.getTargetBlock(null, 100);
    3. Location bl = block.getLocation();

    That is all you need. What you would need to do (to spawn a mob maybe) is this:
    1. if(args[0].equalsIgnoreCase("Wolf")){
    2. world.spawnCreature(bl,EntityType.WOLF);
    Captain Dory likes this.
  8. But how do i setType ?
  9. Code:java
    1. private staredAtBlock(Player player){
    2. return player.getTargetBlock(null, 200);
    3. }
    4. private setStaredBlock(Block block){
    5. staredAtBlock.setType(Material.TNT);
    6. }

    that should do the trick
Thread Status:
Not open for further replies.

Share This Page