Jump to content

Closest Object


iJodix

Recommended Posts

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.

  • Like 2
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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())
                                 ));
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...