closest entity

Discussion in 'Plugin Development' started by Hendrik_the_best, Apr 19, 2014.

Thread Status:
Not open for further replies.
  1. what is the best way to get the closest player from a entity?
     
  2. Offline

    TheMcScavenger

    Get the location from all the players and the entity, get the distance between the two locations, compare them and get the lowest from the distances.
     
  3. Offline

    TheMcScavenger

    Code:
    public void test(){
        World world = Bukkit.getWorld("world");
        Entity entity = world.spawnEntity(new Location(world, 1, 1, 1), EntityType.ARROW);
        Location entityLocation = entity.getLocation();
       
        HashMap<Player, Double> distances = new HashMap<Player, Double>();
        for(Player p : Bukkit.getOnlinePlayers()){
            Location location = p.getLocation();
            double distance = location.distance(entityLocation);
            distances.put(p, distance);
        }
    }
    Just go through the hashmap, compare the distances, get the lowest, and get the player that one belongs to.
     
  4. Offline

    ZodiacTheories

  5. Offline

    TheMcScavenger

    I'm not, really...

     
  6. Offline

    ZodiacTheories

  7. Offline

    TheMcScavenger

  8. Offline

    Badeye

    Hendrik_the_best There is another method, do it like this:
    Code:java
    1.  
    2. //the double values are defining the search radius, you could do a loop and increase it til a player gets found
    3. List<Entity> nearbyEnts = etity.getNearbyEntities(20.0D, 20.0D, 20.0D);
    4. if (nearbyEnts != null) {
    5. for (Entity forEnt : nearbyEnts) {
    6. if (forEnt.getType() == EntityType.PLAYER) {
    7. //Do stuff and get the first List element
    8. }
    9. }
    10. }
     
  9. Badeye
    TheMcScavenger
    my problem is to get the entity /player that is the CLOSEST to the entity how could i get that out of a hashMap or a List?
     
  10. Offline

    Badeye

  11. Offline

    TheMcScavenger

    As far as I'm aware, my code would work perfect fine. All you have to do is loop through the distances and get which of them is the lowest. I'm sure googling it isn't too much work for you, considering I already wrote 90% of your code for you.

    Code:
    public void test(){
        World world = Bukkit.getWorld("world");
        Entity entity = world.spawnEntity(new Location(world, 1, 1, 1), EntityType.ARROW);
        Location entityLocation = entity.getLocation();
     
        HashMap<Player, Double> distances = new HashMap<Player, Double>();
        for(Player p : Bukkit.getOnlinePlayers()){
            Location location = p.getLocation();
            double distance = location.distance(entityLocation);
            distances.put(p, distance);
        }
     
        // Compare distances & find lowest
    }
     
  12. TheMcScavenger
    like i said i need help with how to get the closest player but how would i do that?
    I know how to store the players in a hashmap ect but how could i make it say the closest player.
    I hope you understend me.
    thanks.
     
  13. Offline

    TheMcScavenger

    "I know how to store the players in a hashmap etc": Of course you do, I wrote the entire code for that. All you had to do is read it.
    "how could i make it say the closest player": I already told you, you have to compare all the distances that are being put into the hash map, and get the lowest from that. That's plain old Java, GOOGLE IT. I'm not going write all of your code, I wrote 90% of it, all you have to do, is google and finish it. If you can't google, you will never learn to code.
     
Thread Status:
Not open for further replies.

Share This Page