Aviri Posted October 10, 2015 Share Posted October 10, 2015 How would I collect a list of the all ground items that I can edit?Been trying to do this for about 3 hours now with no luck... Basically need to get all the ground items, and then out of all out those remove any items I don't want, and then give me how many items are left within the list. Been trying with List and Collections, but no luck. Any tips would be great, thanks! Quote Link to comment Share on other sites More sharing options...
Apaec Posted October 10, 2015 Share Posted October 10, 2015 you could use stre/llambda but for for purpose of simplicity: for (GroundItem g : groundItems.getAll()) { if (g != null && g.exists()){ //conditions here blah } } apa Quote Link to comment Share on other sites More sharing options...
Bobrocket Posted October 10, 2015 Share Posted October 10, 2015 List<String> forbiddenNames = Arrays.asList(new String[] { "Bones", "Coins" }); getGroundItems().getAll().stream().filter((gi) -> (gi != null && gi.exists() && !forbiddenNames.contains(gi.getName()))).collect(Collectors.toList()); Quote Link to comment Share on other sites More sharing options...
Aviri Posted October 10, 2015 Author Share Posted October 10, 2015 List<String> forbiddenNames = Arrays.asList(new String[] { "Bones", "Coins" }); getGroundItems().getAll().stream().filter((gi) -> (gi != null && gi.exists() && !forbiddenNames.contains(gi.getName()))).collect(Collectors.toList()); This is pretty beast. Have no idea how it works but I'm going to figure it out. Thank you! Thanks apa, too Quote Link to comment Share on other sites More sharing options...
Flamezzz Posted October 10, 2015 Share Posted October 10, 2015 (edited) For completeness, the good old filter List<GroundItem> list = groundItems.filter(i -> <conditions>) groundItems.filter(i -> i.getName().equals("name")); Still prefer this one over java 8 streams cuz it's unreadable. Edited October 10, 2015 by Flamezzz Quote Link to comment Share on other sites More sharing options...
Aviri Posted October 10, 2015 Author Share Posted October 10, 2015 For completeness, the good old filter List<GroundItem> list = groundItems.filter(i -> <conditions>) groundItems.filter(i -> i.getName().equals("name")); Still prefer this one over java 8 streams cuz it's unreadable. Would I still be able to get the size (how many) of result items using streams as he shows? Quote Link to comment Share on other sites More sharing options...
Bobrocket Posted October 10, 2015 Share Posted October 10, 2015 This is pretty beast. Have no idea how it works but I'm going to figure it out. Thank you! Thanks apa, too My streams tutorial can somewhat explain what's happening. We filter our GroundItems list to the following conditions: They aren't null (!= null) They exist (.exists()) Their names are not in the forbidden list (!forbiddenNames.contains(gi.getName())) This will return a list with any ground item that isn't Bones or Coins. 1 Quote Link to comment Share on other sites More sharing options...
Aviri Posted October 10, 2015 Author Share Posted October 10, 2015 (edited) My streams tutorial can somewhat explain what's happening. We filter our GroundItems list to the following conditions: They aren't null (!= null) They exist (.exists()) Their names are not in the forbidden list (!forbiddenNames.contains(gi.getName())) This will return a list with any ground item that isn't Bones or Coins. Thank you so much EDIT: Okay, that part works just fine now. (Thank you Bobrocket, you taught me streams today ) Now I need to get a list of my inventory items but only make sure the list size (or amount of slots I need to clear up) only reaches the size of wanted ground items. Note: to put this in context, I only need to check this when my inv is full and my wanted items are on the ground. (simple conditions I don't need help with) Basically, my inventory is full, I have 3 items on the floor that I want, and need to make 3 spaces in my inventory, so I need to remove 3 of the shitty items I have in my inventory. In a simpler form, how would I set a condition to limit the size of the list? (I know how to set the shitty item condition mentioned above). Or can I get the list, and then trim it to the size of wanted items? Thanks in advance, sorry if I don't explain all this too well. Edited October 10, 2015 by Aviri Quote Link to comment Share on other sites More sharing options...
FrostBug Posted October 10, 2015 Share Posted October 10, 2015 (edited) Thank you so much EDIT: Okay, that part works just fine now. (Thank you Bobrocket, you taught me streams today ) Now I need to get a list of my inventory items but only make sure the list size (or amount of slots I need to clear up) only reaches the size of wanted ground items. Note: to put this in context, I only need to check this when my inv is full and my wanted items are on the ground. (simple conditions I don't need help with) Basically, my inventory is full, I have 3 items on the floor that I want, and need to make 3 spaces in my inventory, so I need to remove 3 of the shitty items I have in my inventory. In a simpler form, how would I set a condition to limit the size of the list? (I know how to set the shitty item condition mentioned above). Or can I get the list, and then trim it to the size of wanted items? Thanks in advance, sorry if I don't explain all this too well. List<Item> junk = getInventory().filter(itm -> itm.getName().equals("Junk")); List<Item> trimmed = (junk.size() > 3) ? junk.subList(0, 3) : junk; Edited October 10, 2015 by FrostBug 1 Quote Link to comment Share on other sites More sharing options...
Joseph Posted October 10, 2015 Share Posted October 10, 2015 You can also make your list like frost bug did. Then simply grab the list, search it, and remove the item you don't care about. The you will be left with a list of items you want.. Quote Link to comment Share on other sites More sharing options...
Aviri Posted October 10, 2015 Author Share Posted October 10, 2015 Yep! Thanks to everyone who helped out. Working perfectly as intended now! Quote Link to comment Share on other sites More sharing options...
Paradox68 Posted October 14, 2015 Share Posted October 14, 2015 (edited) I'm not a professional but I made a version of this that wrote the items in a list on the paint a while back, here's the snippet just because. Even though the problem was already solved. List<GroundItem> itemsog = getGroundItems().getAll(); int count = 0; for (int i = 0; i < itemsog.toArray().length; i++) { for (int i2 = 0; i2 < itemsog.toArray().length; i2++) { if (itemsog.get(i).getName().equalsIgnoreCase(itemsog.get(i2).getName()) && count == 1) { itemsog.remove(i2); count = 0; } if (itemsog.get(i).getName().equalsIgnoreCase(itemsog.get(i2).getName()) && count == 0) { count = 1; } } } g.setFont(new Font("Trebuchet MS", Font.PLAIN, 10); g.setColor(Color.WHITE); for (int i = 0; i < itemsog.toArray().length; i++) { g.drawString(itemsog.get(i).getName(), 8, 30 + (i * 11)); } I know it's lazy as hell and doesn't properly use the API but it's something I improvised due to lack of knowledge there was an easier way xD Even though it's dirty code it works fine and weeds out all duplicate item names and displays them in the top left corner. of the screen in a nice screen space saving list. Edited October 14, 2015 by Paradox68 Quote Link to comment Share on other sites More sharing options...
Bobrocket Posted October 14, 2015 Share Posted October 14, 2015 I'm not a professional but I made a version of this that wrote the items in a list on the paint a while back, here's the snippet just because. Even though the problem was already solved. List<GroundItem> itemsog = getGroundItems().getAll(); int count = 0; for (int i = 0; i < itemsog.toArray().length; i++) { for (int i2 = 0; i2 < itemsog.toArray().length; i2++) { if (itemsog.get(i).getName().equalsIgnoreCase(itemsog.get(i2).getName()) && count == 1) { itemsog.remove(i2); count = 0; } if (itemsog.get(i).getName().equalsIgnoreCase(itemsog.get(i2).getName()) && count == 0) { count = 1; } } } g.setFont(new Font("Trebuchet MS", Font.PLAIN, 10); g.setColor(Color.WHITE); for (int i = 0; i < itemsog.toArray().length; i++) { g.drawString(itemsog.get(i).getName(), 8, 30 + (i * 11)); } I know it's lazy as hell and doesn't properly use the API but it's something I improvised due to lack of knowledge there was an easier way xD Even though it's dirty code it works fine and weeds out all duplicate item names and displays them in the top left corner. of the screen in a nice screen space saving list. @OP, if you want a cleaner version of Paradox's code (pretty inefficient, hard to read etc) here you go: List<GroundItem> groundItems = getGroundItems().getAll().stream().distinct().collect(Collectors.toList()); int j = 0; groundItems.forEach((gi) -> (g.drawString(gi.getName(), 8, 30 + ((j++) * 11)))); Quote Link to comment Share on other sites More sharing options...
Paradox68 Posted October 14, 2015 Share Posted October 14, 2015 (edited) @OP, if you want a cleaner version of Paradox's code (pretty inefficient, hard to read etc) here you go: List<GroundItem> groundItems = getGroundItems().getAll().stream().distinct().collect(Collectors.toList()); int j = 0; groundItems.forEach((gi) -> (g.drawString(gi.getName(), 8, 30 + ((j++) * 11)))); I knew there had to be a way to do this in like one line. I just hadn't heard of stream().distinct() method before. Thanks for the info i'll definitely be implementing this method in future scripts. Edited October 14, 2015 by Paradox68 Quote Link to comment Share on other sites More sharing options...