iJodix Posted May 3, 2016 Share Posted May 3, 2016 I want to return a object closest to position, how am i supposed to do that ? Quote Link to comment Share on other sites More sharing options...
Token Posted May 3, 2016 Share Posted May 3, 2016 public RS2Object getClosestObject(Script script, Position position) { List<RS2Object> allObjects = script.objects.getAll(); if (allObjects.size() == 0) { script.log("No objects found"); return null; } else if (allObjects.size() == 1) { return allObjects.get(0); } allObjects.sort(new Comparator<RS2Object>() { @Override public int compare(RS2Object obj1, RS2Object obj2) { return obj1.getPosition().distance(position) - obj2.getPosition().distance(position); } }); return allObjects.get(0); } If it returns the furthest object, reverse the operation in the Comparator. 2 Quote Link to comment Share on other sites More sharing options...
iJodix Posted May 3, 2016 Author Share Posted May 3, 2016 public RS2Object getClosestObject(Script script, Position position) { List<RS2Object> allObjects = script.objects.getAll(); if (allObjects.size() == 0) { script.log("No objects found"); return null; } else if (allObjects.size() == 1) { return allObjects.get(0); } allObjects.sort(new Comparator<RS2Object>() { @Override public int compare(RS2Object obj1, RS2Object obj2) { return obj1.getPosition().distance(position) - obj2.getPosition().distance(position); } }); return allObjects.get(0); } If it returns the furthest object, reverse the operation in the Comparator. thanks Quote Link to comment Share on other sites More sharing options...
Explv Posted May 3, 2016 Share Posted May 3, 2016 public RS2Object getClosestObject(Script script, Position position) { List<RS2Object> allObjects = script.objects.getAll(); if (allObjects.size() == 0) { script.log("No objects found"); return null; } else if (allObjects.size() == 1) { return allObjects.get(0); } allObjects.sort(new Comparator<RS2Object>() { @Override public int compare(RS2Object obj1, RS2Object obj2) { return obj1.getPosition().distance(position) - obj2.getPosition().distance(position); } }); return allObjects.get(0); } If it returns the furthest object, reverse the operation in the Comparator. I want to return a object closest to position, how am i supposed to do that ? You can just do: Optional<RS2Object> closestObj = getObjects() .getAll() .stream() .min((o1, o2) -> Integer.compare( o1.getPosition().distance(myPosition()), o2.getPosition().distance(myPosition()) )); 1 Quote Link to comment Share on other sites More sharing options...