Tracking nearest Player with a compass??

Discussion in 'Plugin Development' started by Tommy Raids, Dec 10, 2013.

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

    Tommy Raids

    Ok guys, so I am trying to make a plugin kinda like mc-hg's plugin where if you have a compass and rightclick the ground/air the compass points to the nearest player. This is the code I have so far but for some reason it does not work. Thanks for helping!

    Code:
        @EventHandler
        public void onCompassTracker(PlayerInteractEvent e){
            Player p = e.getPlayer();
            Player target = e.getPlayer();
            if((e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) && p.getItemInHand().getType() == Material.COMPASS){
                p.setCompassTarget(target.getLocation());
            }
           
        }
     
  2. Offline

    JRL1004

    Tommy Raids Your target is the player that clicked so you are setting the compass to the player's location when he clicked. You need to get the nearest player and have a scheduler update the location every few ticks to "track" said player.
     
  3. Offline

    agokhale1

    Tommy Raids
    What you want to do is player's location and check to see if there are any players within a radius using a for loop/scheduler. Then, you want to get all of the entities in that radius, get the location of the other player and set the compass target to the other player's location.
     
  4. Offline

    JRL1004

    Tommy Raids Here is a method for getting the closest player in a radius but be careful as it can return null is no player is within the chosen radius:
    Code:java
    1. public Player getNearest(Player p, Double range) {
    2. double distance = Double.POSITIVE_INFINITY; // To make sure the first
    3. // player checked is closest
    4. Player target = null;
    5. for (Entity e : p.getNearbyEntities(range, range, range)) {
    6. if (!(e instanceof Player))
    7. continue;
    8. double distanceto = p.getLocation().distance(e.getLocation());
    9. if (distanceto > distance)
    10. continue;
    11. distance = distanceto;
    12. target = (Player) e;
    13. }
    14. return target;
    15. }
     
  5. Offline

    Tommy Raids

    JRL1004 I put it in my event but it doesn't work. It has some errors. Where would I put this code. And no I did not copy and paste.
     
  6. Offline

    agokhale1

    Tommy Raids
    Have that code in your PlayerInteractEvent in a scheduler that fires each time a player right clicks a compass.
     
  7. Offline

    JRL1004

    Tommy Raids I did make one mistake, it should be:
    Code:java
    1. public Player getNearest(Player p, Double range) {
    2. double distance = Double.POSITIVE_INFINITY; // To make sure the first
    3. // player checked is closest
    4. Player target = null;
    5. for (Entity e : p.getNearbyEntities(range, range, range)) {
    6. if (!(e instanceof Player))
    7. continue;
    8. if(e == p) continue; //Added this check so you don't target yourself.
    9. double distanceto = p.getLocation().distance(e.getLocation());
    10. if (distanceto > distance)
    11. continue;
    12. distance = distanceto;
    13. target = (Player) e;
    14. }
    15. return target;
    16. }

    and what errors are you getting?
     
  8. Offline

    Tommy Raids

    JRL1004 Where do I put it In my event?
     
  9. Offline

    JRL1004

    Tommy Raids It's not meant to be in an event, I coded it was a standalone method... You would use it like this:
    Code:java
    1. // Your method stuff
    2. Player target = getNearest(/* Person you want to use */, /*distance to check around the person */);
    3. //Then you need to make sure there was a player
    4. if(target == null) {
    5. //Uh-oh, there was not a player in that range
    6. }
    7. else {
    8. //There was a player so set the compass
    9. }
     
  10. Offline

    Tommy Raids

    JRL1004 .. It still doesn't work. Whatever Im just a noob anyways. I know your trying your hardest. Sorry.
     
  11. Offline

    JRL1004

    Tommy Raids It's no issue, I like helping others (That sounds like something from a book / movie huh?). Anyways, I need to go soon so I'll try and help you out tomorrow (assuming someone else has not solved the problem).
     
  12. Offline

    auyeungyat

    Thanks!It also helps me in my plugin!
     
Thread Status:
Not open for further replies.

Share This Page