Objects.get() returns an array of objects at the position, .get(0) returns the first one and usually there is only one objects per tile.
You could take an area around the player in a eg 10x10 grid and check if the mouse click or press event's .getPoint() is within the bounds of a tile.
(Do not check every tile loaded, 10x10 is already a significant load)
Idk if this is the best way to approach it but i know it works
Useful snippets:
ArrayList<Position> nearby_positions = new ArrayList<Position>();
private void updateNearbyPositions(int scan_radius) {
Position my_position = myPosition();
int start_x = my_position.getX() - scan_radius;
int start_y = my_position.getY() - scan_radius;
int end_x = my_position.getX() + scan_radius;
int end_y = my_position.getY() + scan_radius;
for (int xx = start_x; xx <= end_x; xx++) {
for (int yy = start_y; yy <= end_y; yy++) {
Position pp = new Position(xx, yy, my_position.getZ());
nearby_positions.add(pp);
}
}
}
@[member=Override]
public void mouseClicked(MouseEvent e) {
for (Position p : nearby_positions) {
if (p.isVisible(getBot()) && p.getPolygon(getBot()).contains(e.getPoint())) {
if (mining_rock_positions.containsKey(p)) {
mining_rock_positions.remove(p);
e.consume();
} else if (!mining_rock_positions.containsKey(p)) {
RS2Object rock = getObjects().get(p.getX(), p.getY()).get(0);
if (rock != null) {
mining_rock_positions.put(p, rock.getId());
e.consume();
}
}
}
}
}
Apologies for the structure, its a pretty old code of mine