[SOLVED] Getting Entity in Sight

Discussion in 'Plugin Development' started by np98765, Jul 29, 2012.

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

    np98765

    Hello,

    I was wondering how to get the living entity's location in sight of a player. (i.e. Get the pig's location that the player is looking at).

    Thanks!
     
  2. Offline

    Luee

    .getLocation() from org.bukkit.entity.Entity if you can figure out the event. Perhaps the EntityTargetEvent PlayerInteractEntityEvent
     
  3. Offline

    np98765

    Well, that's what I was asking. :p I can't figure it out for the life of me...
     
  4. Offline

    Luee

    See my edit?
     
  5. Offline

    np98765

    ? PlayerInteractEntity?

    --


    Code:
                    LivingEntity target = ENTITY-IN-LINE-OF-SIGHT;
                    if (playerIsLookingAtAnotherEntity {
                    Location entityloc = target.getLocation();
                    target.getWorld().strikeLightning(entityloc);
    
    Actually, I'm going to rephrase my question... It might have been a bit unclear.

    How do I get the LivingEntity that a player is looking at?


    I'm trying p.hasLineOfSight(something); so far, it hasn't worked out. I'm assuming this is correct, but I can't figure out how to get any living entity.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  6. Offline

    Firefly

    hasLineOfSight just checks if there's a clear path to another entity (I.E. no blocks obstructing the view of one entity to another). I suggest iterating through every block returned by getLineOfSight and checking if there are any entities "near" the blocks you iterate.
     
  7. Offline

    CorrieKay

    oh gosh, thats amazing. Thats new to RB5, right?
     
  8. Offline

    np98765


    Ok... Well, as I'm pretty new, I have no clue how do that. So far, I have this (it's really messy; I'm still not sure what to do):

    Code:
                    if (event.getAction().equals(Action.LEFT_CLICK_AIR) || event.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
                        LivingEntity target = something;
                        p.getLineOfSight(null, 101);
     
                        if (p.hasLineOfSight(target)) {
                            Location entityloc = target.getLocation();
                            target.getWorld().strikeLightning(entityloc);
                        }
                    }
    
    All I'm trying to do is make this strike lightning at the entity's location.
     
  9. Offline

    Firefly

    CorrieKay

    Yup! Unfortunately it doesn't do what I thought it did (checking if a player can physically see another entity, as in there FoV.)

    Please standby while I dig up some old code.

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

    CorrieKay

    Aye, i figured it would return true even if they have a clear LoS but they were behind.

    Do you know how cpu intensive this method is? can it be called rapidly in succession?
     
  11. Offline

    np98765

    Thanks. :)
     
  12. Offline

    Firefly

    Code:
    Block targetBlock = //The block your currently iterated
                    Location blockLoc = targetBlock.getLocation();
                    double bx = blockLoc.getX();
                    double by = blockLoc.getY();
                    double bz = blockLoc.getZ();
                    List<Entity> e = player.getNearbyEntities(100, 100, 100);
               
                    for (Entity entity : e) {
                        Location loc = entity.getLocation();
                        double ex = loc.getX();
                        double ey = loc.getY();
                        double ez = loc.getZ();
                   
                        if ((bx-1.5 <= ex && ex <= bx+2) && (bz-1.5 <= ez && ez <= bz+2) && (by-1 <= ey && ey <= by+2.5)) {
                            Now you have an entity near the location specified by targetBlock
                        }
            }
    
    Sorry for bad formatting, the code editor failed me D:

    No idea. That would require some testing or a look at the code. Apparently, it uses the same code that mobs use to target players.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  13. Offline

    CorrieKay

    >.>
    <.<

    You should hold that list outside of the iteration loop, so you dont keep calling it again and again ^ ^
     
  14. Offline

    Firefly

    CorrieKay

    Of course, I was showing some rough code from my first plugin to show him the basic outline of what he has to do :p
     
  15. Offline

    CorrieKay

    Gotcha :3
     
  16. Offline

    Firefly

    I sometimes look back at my first plugins and go "Wow, I really sucked at coding!" xD
     
  17. Offline

    CorrieKay

    Dear god, i do the exact same thing.


    Hell, i designed a plugin to replace essentials on my server back in late april/mid may.... Im already working on a better plugin to replace it xD

    By the way, i know there was someone here that had some fancy mathematics dealing with checking if someone was within FoV of another entity, though, like i said, really fancy mathematics..
     
  18. Offline

    Firefly

    I saw that post. The problem was I had terrible issues getting it to work consistently. I believe you are referring to this.
     
  19. Offline

    np98765

    Wow... There's really no way to get the entity that the player is looking at?
    Well, thanks for the code -- I'll be inputting that soon. :)

    Right now, I'm just making a simple plugin to smite the entity, not the block, that you're looking at. But, I soon want to make a plugin that allows players to kill who they're looking at, but they must be looking directly at it. So... I would change the p.getNearbyEntities(100, 100, 100) to something like 1, 1, 1?
     
  20. Offline

    Firefly

    No, otherwise the player would have to be 1 block away from the target.

    if ((bx-1.5 <= ex && ex <= bx+2) && (bz-1.5 <= ez && ez <= bz+2) && (by-1 <= ey && ey <= by+2.5))

    Change the numbers on that line to change the accuracy of how close they have to look at the player.
     
  21. Offline

    np98765

    Oh, I see... Ok. Thanks for the help! :D
     
  22. Offline

    CorrieKay

    Right, it was Kumpelblase that did it! :D
     
Thread Status:
Not open for further replies.

Share This Page