Solved Get Target Player

Discussion in 'Spigot Plugin Development' started by DasDarki, May 29, 2016.

  1. How can I get the Player I looking at?
  2. What do you have so far?
  3. without interaction ? I don't know if its possible though.
  4. It is possible, but I don't think we should spoon feed.
    • Like Like x 1
  5. well nc.
  6. If you wanted to do this without endless looping, you could set metadata to a projectile and fire it with a really high (10+) velocity. Then just listen to an EntityDamageByEntityEvent for when it hits something. If it's fast enough, it'll pretty much be instant.
    • Funny Funny x 1
  7. Use a for-loop and a vector. I'll try to write something up for you which should get you started, but I'm on my phone, so it won't instantly work.
    Code (Java):

    Vector dir = player.getLocation().getDirection(); // get the player's direction.
    Location loc = player.getEyeLocatiob(); // get the player's eye location.
    for (int i = 1; i <= range; i++) {
      loc.add(x * i, y * i, z * i); // move the location forward.
      for (Entity ent : loc.getWorld().getEntitiesAroundPoint(loc, 2, 2, 2)) // get entities around 'loc'.
        if (ent instanceof Player) // the entity is a player
          return (Player) ent; // player found!
      loc.subtract(x * i, y * i, z * i); // move loc back
    • Informative Informative x 1
  8. Well, this looks fine, but wherefrom I get x,y and z at loc.add?! Do the vars come from the Vector or the location?
  9. Oh sorry! Stupid me! x y and z are the x y and z values from the Vector 'dir'. Simply replace them by dir.getX(), dir.getY() and dir.getZ().
  10. Yeah...^^ Thank you very much //close