Jump to content
mlrkey

Return true if there are 10 dropped buckets on the floor

Recommended Posts

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

 

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

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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

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

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

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