Help with direction

Discussion in 'Plugin Development' started by Iron_Crystal, Apr 20, 2012.

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

    Iron_Crystal

    What I am trying to do is find nearby players, and get their location relative to the player.

    I know how to get nearby players, but how do I get their location relative to the player? ie (player A is 10 blocks north)
     
  2. Offline

    Alectriciti

    Locations and distances aren't the easiest of things for me, personally, but I'll see what I can do to help.
    I'd definitely use locations of the blocks players are standing at (their X and Z only) then subtract the X from the Z to find the location of that player.

    I believe this will do direct distance:
    PHP:
    p1.sendMessage(p1.getLocation().getBlock().getLocation().distance(p2.getLocation().getBlock().getLocation()));
    This should give the x and z distances. You could have the plugin check whether it's direction (North, West, South, or East) depending on whether X or Z is positive or negative.

    This is personally how I would go about it. This is based off of Player p1 and Player p2. Feel free to use this and play around with it.

    PHP:
                        String directionx "X";
                        
    String directionz "Z";
             
                        
    int p1x p1.getLocation().getBlockX();
                        
    int p2x p2.getLocation().getBlockX();
     
                        
    int p1z p1.getLocation().getBlockZ();
                        
    int p2z p2.getLocation().getBlockZ();
     
                        
    int distx java.lang.Math.abs((p1x p2x));
                        
    int distz java.lang.Math.abs((p1z p2z));
                 
                        if (
    p1x p2x){
                            
    directionx "West";
                        }else{
                            
    directionx "East";
                        }
                        if (
    p1z p2z){
                            
    directionz "North";
                        }else{
                            
    directionz "South";
                        }
                 
                        
    p1.sendMessage(p2.getName() + " is " distx " blocks to the " directionx " and " distz " blocks to the " directionz ".");
    I'm just stating now, I'm not sure which direction is which. You'll have to figure that out on your own. Goodluck!
     
  3. Offline

    Iron_Crystal

    Thanks. I'll try this.

    Dude, you were dead on with the directions! Thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
Thread Status:
Not open for further replies.

Share This Page