mlrkey Posted May 21, 2015 Share Posted May 21, 2015 (edited) How would I go about doing this? Don't ask why lol... Thanks Edited May 21, 2015 by mlrkey Quote Link to comment Share on other sites More sharing options...
Mysteryy Posted May 21, 2015 Share Posted May 21, 2015 If the player dropped 10 buckets, or if there are 10 buckets on the ground and you dont care where they came from? Quote Link to comment Share on other sites More sharing options...
Joseph Posted May 21, 2015 Share Posted May 21, 2015 If the player dropped 10 buckets, or if there are 10 buckets on the ground and you dont care where they came from? What he said Quote Link to comment Share on other sites More sharing options...
mlrkey Posted May 21, 2015 Author Share Posted May 21, 2015 If the player dropped 10 buckets, or if there are 10 buckets on the ground and you dont care where they came from? Dont care where they came from. Quote Link to comment Share on other sites More sharing options...
Alek Posted May 21, 2015 Share Posted May 21, 2015 You can either use the stream api afforded in Java 8, or run a for loop of all ground items. Quote Link to comment Share on other sites More sharing options...
Mysteryy Posted May 21, 2015 Share Posted May 21, 2015 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; } Quote Link to comment Share on other sites More sharing options...
mlrkey Posted May 21, 2015 Author Share Posted May 21, 2015 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 Quote Link to comment Share on other sites More sharing options...
fixthissite Posted May 21, 2015 Share Posted May 21, 2015 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. Quote Link to comment Share on other sites More sharing options...
Mysteryy Posted May 21, 2015 Share Posted May 21, 2015 (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. 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 May 21, 2015 by Mysteryy Quote Link to comment Share on other sites More sharing options...
fixthissite Posted May 21, 2015 Share Posted May 21, 2015 (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 May 21, 2015 by fixthissite Quote Link to comment Share on other sites More sharing options...
mlrkey Posted May 21, 2015 Author Share Posted May 21, 2015 "script" cannot be resolved in: java.util.List<GroundItem> allGroundItems = script.getGroundItems().getAll(); Quote Link to comment Share on other sites More sharing options...
fixthissite Posted May 21, 2015 Share Posted May 21, 2015 (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 May 21, 2015 by fixthissite Quote Link to comment Share on other sites More sharing options...
Flamezzz Posted May 21, 2015 Share Posted May 21, 2015 Or just: groundItems.filter(item -> item.getName().equals("Bucket")).size() == 10 1 Quote Link to comment Share on other sites More sharing options...