Solved Get entity player looking at?

Discussion in 'Plugin Development' started by Qaez, Dec 7, 2013.

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

    Qaez

    Hello,

    I have a question.
    How to get the entity the player is looking at?
    Because I want to make kind of a capture spell for my wand.
    So the entity the player is looking at will be the passenger.
    Thanks!
     
  2. Offline

    XDSiLeNtKiLlXD

    Code:
    @EventHandler
    SomePlayerEvent(blablabla e)
    {
      for(Entity e : e.getPlayer().getWorld().getEntities())
      {
          if(e.getLocation() == e.getPlayer.getEyeLocation())
          {
            do blablabla
          }
      }
    } 
    
     
  3. Offline

    Qaez

    the getEyeLocation is for checking where the eyes are not for where they are looking
     
  4. Offline

    XDSiLeNtKiLlXD

    Qaez
    Ow well, was worth a try :p
     
  5. Offline

    Qaez

    Haha
     
  6. Offline

    XDSiLeNtKiLlXD

    Code:
    ArrayList<Entity>entl = new ArrayList<Entity>();
     
    @EventHandler
    SomePlayerEvent(blablabla e)
    {
      for(Entity e : e.getPlayer().getWorld().getEntities())
      {
          if(e.getPlayer().hasLineOfSight(e))
          {
            entl.add(e);
            for(Entity e : e.getPlayer().getNearbyEntites(10.0, 10.0, 10.0))
            {
                if(entl.contains(e)
                {
                  do thiz
                }
            }
          }
      }
    } 
    Maybe something like this Qaez
    This should work for targets that are in a 10 block radius but might get you multiple entites idk.
     
  7. Offline

    i3ick

    player.getTargetBlock(null, 200).getLocation()
     
  8. Offline

    Wolfey

    This gets the target block, not an entity.
     
  9. Offline

    Qaez

    This doesnt work...

    Do you know a method to fix this?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  10. Offline

    Wolfey

    Try if(p.hasLineOfSight(entity)) {} and see what it does.
     
  11. Offline

    i3ick

    Here is my attempt. I didn't test it though
    Code:
    @EventHandler (priority = EventPriority.HIGH)
        public void onPlayerDamage2(PlayerInteractEvent event){
            Player me = event.getPlayer();
            World world = Bukkit.getWorld(Bukkit.getName());
            BlockIterator bit = new BlockIterator(me, 200);
            Block next;
            while(bit.hasNext())
            {
                next = bit.next();
                Location d = next.getLocation();
                int x = d.getBlockX();
                int y = d.getBlockY();
                int z = d.getBlockZ();
                Location blocklocation = new Location(world, x, y, z);
                me.getLocation().distance(blocklocation);
    //and now that you have block location search for nearby entities and verify they are Players
                   
                }
            }
    Oh I just had an idea.
    You said you want to make some sort of magic spell plugin, right?
    The easiest way to do this is launch a projectile at a player (snowball) and when the player gets hit pull the
    location info from him, then you can do whatever you want with it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  12. Offline

    Qaez

    Thanks this should work but I already found a solution for this with a BlockIterator
     
Thread Status:
Not open for further replies.

Share This Page