Jump to content

Return true if there are 10 dropped buckets on the floor


Recommended Posts

Posted

Dont care where they came from.

 

 

You can either use the stream api afforded in Java 8, or run a for loop of all ground items.

 

Going with the second method, here is a simplistic easy to follow way (completely untested and just for demonstration, but it should work):

    public int getAmountOf(String groundItemName){
        int numberOfItems = 0;
        java.util.List<GroundItem> allGroundItems = script.getGroundItems().getAll();
        if(allGroundItems != null && allGroundItems.size() > 0){
            for(GroundItem groundItem : allGroundItems){
                if(groundItem != null && groundItem.getName() != null && groundItem.getName().equals(groundItemName)){
                    numberOfItems++;
                }
            }
        }
        return numberOfItems;
    }
Posted

 

Going with the second method, here is a simplistic easy to follow way (completely untested and just for demonstration, but it should work):

    public int getAmountOf(String groundItemName){
        int numberOfItems = 0;
        java.util.List<GroundItem> allGroundItems = script.getGroundItems().getAll();
        if(allGroundItems != null && allGroundItems.size() > 0){
            for(GroundItem groundItem : allGroundItems){
                if(groundItem != null && groundItem.getName() != null && groundItem.getName().equals(groundItemName)){
                    numberOfItems++;
                }
            }
        }
        return numberOfItems;
    }

 

Thanks a lot :) Will try soon

Posted

 

Going with the second method, here is a simplistic easy to follow way (completely untested and just for demonstration, but it should work):

    public int getAmountOf(String groundItemName){
        int numberOfItems = 0;
        java.util.List<GroundItem> allGroundItems = script.getGroundItems().getAll();
        if(allGroundItems != null && allGroundItems.size() > 0){
            for(GroundItem groundItem : allGroundItems){
                if(groundItem != null && groundItem.getName() != null && groundItem.getName().equals(groundItemName)){
                    numberOfItems++;
                }
            }
        }
        return numberOfItems;
    }

No need for

allGroundItems.size() > 0

The for loop already does this kind of check. Also, you could switch

groundItem.getName() != null && groundItem.getName().equals(groundItemName)

to

groundItemName.equals(groundItem.getName())

Allowing the equals call to perform the null-check on getName.

 

__________________________________

 

Using the Stream API:

public long getAmountOf(String itemName) {
     java.util.List<GroundItem> groundItems = script.getGroundItems().getAll();
     long numberOfItems = 0L;

     if(grounditems != null)
          numberOfItems = groundItems.stream().filter(item -> itemName.equals(item.getName())).count();

     return numberOfItems;
}

This is based off Mysteryyy's use of script.getGroundItems().getAll(); I've never actually used this myself (yet). If you want to return an int, simply cast when you return. Although, you could just have a boolean method:

public boolean near10Buckets() {
     return getAmountOf("Bucket") > 10L;
}

To prevent casting overhead.

Posted (edited)

No need for

allGroundItems.size() > 0

The for loop already does this kind of check. Also, you could switch

groundItem.getName() != null && groundItem.getName().equals(groundItemName)

to

groundItemName.equals(groundItem.getName())

Allowing the equals call to perform the null-check on getName.

 

 

 

1) Yes, the loop will take care of this. The way I had it before, I was allocating the int inside of that if statement, so it actually would save the allocation of the int if the length was 0. After I decided to give a proper snippet I just decided to make it a method and forgot to change it after I moved things around.

 

2) You are correct, but I always feel it is good to do my own null checks. I have learned that you can never be too careful. Where I work, we are constantly writing safety critical applications, and you cant assume anything, and can never be too safe. 

The null check could be considered redundant, but it is good practice to use defensive programming, especially when scripting.

Defensive programming is in my blood. happy.png

 

Edit: Also, in your example, I dont think you will ever need a long to store the number of buckets on the ground. :p

Edited by Mysteryy
Posted (edited)

I have learned that you can never be too careful. Where I work, we are constantly writing safety critical applications, and you cant assume anything, and can never be too safe.

Edit: Also, in your example, I dont think you will ever need a long to store the number of buckets on the ground. :p

It's not really about safety measures. It's the exact same execution; the null check is just performed in the equals method, hidden away.

And count() returns a long, which is why I suggested casting. But depending on the situation, you could avoid the overhead of casting (trading memory for runtime performance) by keeping it a long.

Edited by fixthissite
Posted (edited)

"script" cannot be resolved in:

java.util.List<GroundItem> allGroundItems = script.getGroundItems().getAll();

After checking out the API, it seems there is no script field. But it seems getGroundItems() is declared in the Script class, so use that knowledge to fix up your code Edited by fixthissite

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