Check if there is a player in specified coordinate

Discussion in 'Plugin Development' started by _Fede, Aug 23, 2012.

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

    _Fede

    Hi,
    i would like to know if the there is a method to notice if in specified coordinate there is a player :)
     
  2. Offline

    skore87

    You might go about it like this:

    Code:
        public Player getPlayerAt(Location loc){
            for (Player p : loc.getWorld().getPlayers()){
                if (    (p.getLocation().getX() >= loc.getX() - 0.5 && p.getLocation().getX() <= loc.getX() + 0.5) &&
                        (p.getLocation().getY() >= loc.getY() - 0.5 && p.getLocation().getY() <= loc.getY() + 0.5) &&
                        (p.getLocation().getZ() >= loc.getZ() - 0.5 && p.getLocation().getZ() <= loc.getZ() + 0.5)){
                    return p;
                }
            }
            return null;
        }
    If it returns null, then you know that no one is within 1/2 block 'radius' of that location.

    Edit: No idea why it keeps stripping the indents... really frustrating...
     
  3. Offline

    _Fede

    It doesn't work in a switch?

    And for launch a potion with a specified event?

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

    skore87

    Explain what you're asking?
     
  5. Offline

    _Fede

    How can i do to launch/splash a potion in specified coords? skore87

    or if it is easier, how can i insert into a switch a noticer of players are in determinate coords?

    Please HELP ME!

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

    skore87

    I was; now I'm not. Goodnight, getting my sleep on =)
     
  7. Offline

    _Fede

    You just helped me a lot thanks for this, i'm asking you how to splash a potion or how to notice a player in coords :) Someone else that can help me?

    BUMP! Help me please i need it!

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

    Courier

    1. Get all entities in the chunk of the coordinate
    Code:java
    1. Block target = location.getBlock();
    2. Entity[] ents = location.getChunk().getEntities();
    2. For each of those entities, check if the entity is a player and if the player is within 1 block.
    Code:java
    1. for(Entity k : ents)
    2. if(k instanceof Player && k.getLocation().getBlock().equals(target))
    3. return true;
    4. return false;

    This will return true iff there is a player at the same block as "location".
     
  9. Offline

    skore87

    He was given an answer, besides a player will not fit exactly at a block coordinate so this code isn't going to work. In addition to his spammy bump posts, I've decided to stop helping him even though I found how to spawn a potion entity and give it attributes.

    I forgot all about the chunk having the entities method, so here is the edited version.
    Code:
        public Player getPlayerAt(Location loc){
            for (Entity e : loc.getChunk().getEntities()){
                if (e instanceof Player)
                if (    (e.getLocation().getX() >= loc.getX() - 0.5 && e.getLocation().getX() <= loc.getX() + 0.5) &&
                        (e.getLocation().getY() >= loc.getY() - 0.5 && e.getLocation().getY() <= loc.getY() + 0.5) &&
                        (e.getLocation().getZ() >= loc.getZ() - 0.5 && e.getLocation().getZ() <= loc.getZ() + 0.5)){
                    return (Player) e;
                }
            }
            return null;
        }
    I believe the coordinates of a block start at the top left, so you may want to increase the <= values to 1 for X/Z and the >= for Y though I could be wrong.
     
  10. Offline

    _Fede

    But how can i do to let this code work in a switch?
     
  11. Offline

    skore87

    You don't need to use this in a switch statement. There are only two possible outcomes for this function; a Player object or null. So...
    Code:
    Player p = getPlayerAt(Location);
    if (p != null){
      // act on Player
    }
     
  12. Offline

    _Fede

    Thanks you so much! Sorry if i disturbed you. IT WORKS ;)
     
  13. Offline

    Courier

    I am comparing location.getBlock() to player.getLocation().getBlock(), so it actually does work.
     
  14. Offline

    skore87

    Have you looked at your coordinates when in the game? You never reside exactly on a coordinate except for the Y-axis and a block resides exactly on a coordinate.
     
  15. Offline

    Courier

    Do you know what .getBlock() does? It gets the block that the location is inside, even if it isn't exactly on the edge of a block. So a location that is at 100.2, 64.0, 234.4 and a location that is at 100.9, 64.9, 234.1 return the same value for getBlock().
     
  16. Offline

    skore87

    Except if a player is on the "corner" of four blocks, then what? It's a guessing game if you'll get the results you're expecting.
     
  17. Offline

    _Fede

    skore87 Hi,
    I know i'm disturbing but you are good at java, can you explain me how to get the F: number?
    (If you press F3 in game you will get the X Z Y and the F, the F is where the player is looking)
    Thank you!
     
  18. Offline

    Courier

     
  19. Offline

    skore87

    Obviously you haven't figured out what I was talking about since there was no contradiction in the two quotes of me in your post; collision boxes.

    Two methods to get the cardinal direction of a player. The first is a little more resource intensive and works with blocks and vectors, the second works with the yaw of the player. If you want the second to give a blockface, just edit the if statements to return a BlockFace.<DIRECTION> and the return type of the function. http://forums.bukkit.org/threads/find-what-way-player-is-facing.18359/
     
  20. Offline

    Courier

    Using the block that the player is centered in isn't exactly a, "guessing game".
    The code you proposed does nothing to deal with collision boxes either. The only thing your code does differently from mine is that yours is not aligned to blocks. If you run our two methods with 0.5, 5.5, 0.5, they act exactly the same. If you run mine with 0.3, 5.3, 0.3, it works exactly the same as it does with 0.5, 5.5, 0.5.

    Yours checks to see if there is a player whose bottom-center is within half a block of the specified location.
    Mine checks to see if there is a player whose bottom-center is in the same block as the specified location.
    They are both valid, and very similar. The OP's request is too ambiguous to determine which he wanted, or if he wanted something slightly different from both of them.
     
  21. Offline

    skore87

    All you have to do is change the numbers a hair. Isn't difficult to have it apply to more than a 1 block size. Think before you speak.
     
  22. Offline

    Courier

    skore87 Yes, you can easily adjust the numbers. If the OP had asked how to find players within a rectangular prism of a certain size around a location, yours would be the correct answer. He asked, "notice if in specified coordinate there is a player", which could easily mean check if there are any players on a certain block.

    If he did want to check within a range around the point, he would probably want to check a sphere, using the distance from the point, not a rectangular prism.

    Like I said, he was too ambiguous for us to know what he wanted.
     
  23. Offline

    _Fede

    Thank to kyle1320 i got this:
    Code:
    Location loc = player.getLocation();
    int direction = (int)loc.getYaw();
     
    if(direction < 0) {
        direction += 360;
        direction = (direction + 45) / 90;
    }else {
        direction = (direction + 45) / 90;
    }
    if (direction == 4){
        direction = 0;
    }
    THIS CODE IS WHAT I NEED! Thank you guys anyways!
     
  24. Offline

    skore87

    I think you posted on the wrong thread lol. That's your thread on getting the direction the player is looking. If I'm not mistaken I gave you a link to two methods on a forum thread that you could have easily found by using the search feature of this website.
     
  25. Offline

    _Fede

    This code is for skore87 and Courier this is what i was searching..
     
Thread Status:
Not open for further replies.

Share This Page