Solved Getting 5 nearest entities?

Discussion in 'Plugin Development' started by marshmallowz, Jun 4, 2014.

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

    marshmallowz

    I know there is the getNearbyEntities method but I don't want all the nearest entities I just want to select 5 random ones. Anyone know how to go about doing this? All help is appreciated :)
     
  2. Offline

    Gater12

    marshmallowz
    getNearbyEntities returns list of entity. Just choose five values at random indexes.
     
  3. Offline

    marshmallowz

    Gater12
    I can't wrap my head around how to do this, if it won't be much trouble can you show me an example?
     
  4. Offline

    NathanWolf

  5. Offline

    marshmallowz

    NathanWolf
    I've never used that method before, can you help me?
     
  6. Offline

    thecrystalflame

    Code:java
    1.  
    2. List<Entity> ents = new ArrayList<Entity>();
    3. for (int i = 0; i < 5; i++) {
    4. Entity nearest = null;
    5. for (Entity ent : player.getNearbyEntities()) {
    6. if (nearest != null) {
    7. if (ent.getLocation().distance(player.getLocation() < nearest.getLocation().distance(player.getLocation)) {
    8. nearest = ent;
    9. }
    10. } else {
    11. nearest = ent;
    12. }
    13. }
    14. ents.add(nearest);
    15. }
    16.  
     
  7. Offline

    marshmallowz

    thecrystalflame
    The getNearbyEntities takes 3 parameters of the x,y,z for the range
     
  8. Offline

    NathanWolf


    I pretty much spelled it out there.... I don't like to spoonfeed, but I guess here:

    Code:
    Collection<Entity> nearby = player.getNearbyEntities(16, 16, 16);
    Collections.shuffle(nearby);
     
    Entity randomEntity1 = nearby.get(0);
    Entity randomEntity2 = nearby.get(1);
    Entity randomEntity3 = nearby.get(2);
    Entity randomEntity4 = nearby.get(3);
    Entity randomEntity5 = nearby.get(4);
     
  9. Offline

    garbagemule

    thecrystalflame

    Spoonfeeding is detrimental to learning, especially without any kind of explanation, and even moreso when you're spoonfeeding unnecessarily inefficient and hard-to-grok code. There is absolutely no reason to call getNearbyEntities five times. Besides, your algorithm selects the five nearest, not five random, out of the set of nearby entities. Not only that, it also calls the inefficient distance method several times for what seems like no real reason.

    For the purpose of selecting five entities at random in a list, shuffling and then popping the first (or last) five elements of the list is the cleanest way to do it (as suggested by NathanWolf), however generating five random indices would work equally well (and may be faster, depending on how big the list is in the average case, since random number generation is expensive, and the shuffle method runs in asymptotic linear time, whereas five random numbers is asymptotic constant time, both with random number generation as a constant factor).

    NathanWolf
    A minor detail to humans, but paramount to the compiler, is that the shuffle method takes a List, not a Collection, and the Collection interface has no get method :3
     
    NathanWolf likes this.
  10. Offline

    marshmallowz

Thread Status:
Not open for further replies.

Share This Page