Jump to content

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


roguehippo

Recommended Posts

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
Link to comment
Share on other sites

 

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
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...