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. Offline


    just use player.getTargetBlock();
  6. Offline


    Following what @se1by said, heres the code...
    public Block getSelectedBlock(Player p) {
       Block b = p.getTargetBlock(200, null);
       return b;
    So using that, I could just do something like this...
    if (getSelectedBlock(p).equals(Material.TNT)) {
       //The block is tnt
    kalicat, LemonLake and hammale like this.
  7. Offline


    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. Offline


    But how do i setType ?
  9. Offline


    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