Jump to content

How to tell if there are none of a certain item in an area?


Recommended Posts

Posted (edited)

i basically just need to know how to return a true if there are no "Logs" (or whatever item)  in a given area. or if it would be easier if there are just no grounditems in an area at all, but this may cause future problems.

 

Using the OSBot API:

final boolean itemIsInArea(final Area area, final String itemName) {
    return getGroundItems().filter(new AreaFilter<>(area), new NameFilter<>(itemName)).size() > 0;
}

Using the Java 8 Stream API :

final boolean itemIsInArea(final Area area, final String itemName) {
    return getGroundItems().getAll().stream().anyMatch(groundItem -> groundItem.getName().equals(itemName) && area.contains(groundItem));
}

Using a for loop:

final boolean itemIsInArea(final Area area, final String itemName) {
    for(final GroundItem groundItem : getGroundItems().getAll()) {
        if(groundItem.getName().equals(itemName) && area.contains(groundItem)) {
            return true;
        }
    }
    return false;
}

If you want to get the closest matching item in the area you can do:

 

With the OSBot API:

final Optional<GroundItem> closestItemInArea(final Area area, final String itemName) {
    return Optional.ofNullable(getGroundItems().closest(new AreaFilter<>(area), new NameFilter<>(itemName)));
}

With the Java 8 Stream API:

final Optional<GroundItem> closestItemInArea(final Area area, final String itemName) {
    return getGroundItems().getAll()
                           .stream()
                           .filter(groundItem -> groundItem.getName().equals(itemName) && area.contains(groundItem))
                           .min((g1, g2) -> Integer.compare(myPosition().distance(g1.getPosition()), myPosition().distance(g2.getPosition())));
}

Or if you want to get any matching item in the area (not necessarily the closest) you can do:

 

With the OSBot API:

final Optional<GroundItem> getItemInArea(final Area area, final String itemName) {
    return Optional.ofNullable(getGroundItems().singleFilter(getGroundItems().getAll(), 
                                                             new AreaFilter<>(area), 
                                                             new NameFilter<>(itemName)));
}

 

With the Java 8 Stream API:

final Optional<GroundItem> getItemInArea(final Area area, final String itemName) {
    return getGroundItems().getAll()
                           .stream()
                           .filter(groundItem -> groundItem.getName().equals(itemName) && area.contains(groundItem))
                           .findAny();
}

 

Edited by Explv
  • Like 2
Posted

 

Using the OSBot API:

final boolean itemIsInArea(final Area area, final String itemName) {
    return getGroundItems().filter(new AreaFilter<>(area), new NameFilter<>(itemName)).size() > 0;
}

Using the Java 8 Stream API :

final boolean itemIsInArea(final Area area, final String itemName) {
    return getGroundItems().getAll().stream().anyMatch(groundItem -> groundItem.getName().equals(itemName) && area.contains(groundItem));
}

Using a for loop:

final boolean itemIsInArea(final Area area, final String itemName) {
    for(final GroundItem groundItem : getGroundItems().getAll()) {
        if(groundItem.getName().equals(itemName) && area.contains(groundItem)) {
            return true;
        }
    }
    return false;
}

If you want to get the closest matching item in the area you can do:

 

With the OSBot API:

final Optional<GroundItem> closestItemInArea(final Area area, final String itemName) {
    return Optional.ofNullable(getGroundItems().closest(new AreaFilter<>(area), new NameFilter<>(itemName)));
}

With the Java 8 Stream API:

final Optional<GroundItem> closestItemInArea(final Area area, final String itemName) {
    return getGroundItems().getAll()
                           .stream()
                           .filter(groundItem -> groundItem.getName().equals(itemName) && area.contains(groundItem))
                           .min((g1, g2) -> Integer.compare(myPosition().distance(g1.getPosition()), myPosition().distance(g2.getPosition())));
}

Or if you want to get any matching item in the area (not necessarily the closest) you can do:

 

With the OSBot API:

final Optional<GroundItem> getItemInArea(final Area area, final String itemName) {
    return Optional.ofNullable(getGroundItems().singleFilter(getGroundItems().getAll(), 
                                                             new AreaFilter<>(area), 
                                                             new NameFilter<>(itemName)));
}

 

With the Java 8 Stream API:

final Optional<GroundItem> getItemInArea(final Area area, final String itemName) {
    return getGroundItems().getAll()
                           .stream()
                           .filter(groundItem -> groundItem.getName().equals(itemName) && area.contains(groundItem))
                           .findAny();
}

 

 

What r u doing with ur life mane :boge:

 

  • Like 2

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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