scriptersteve Posted January 20, 2018 Share Posted January 20, 2018 Hi, so in one of my previous scripts people mentioned I should try and use filters more: The commented out code is what i was trying to do but was refusing to 'loot' the arrows. The non-commented code works fine and i wondered if anyone new why the commented code didn't work? // Filter<Item> arrows = item -> item.getName().contains("arrows"); //GroundItem arrows1 = getGroundItems().closest(arrows.toString()); GroundItem arrows1 = getGroundItems().closest("Bronze arrow","Iron arrow","Steel arrow","Mithril arrow","Adamant arrow"); Below is current version of my *arrows1* GroundItem arrows1 = getGroundItems().closest(gi -> gi != null && getMap().canReach(gi) && gi.getName().contains("arrow")); This one does work but is not using a filter (although it is kinda filtering by the contains name) but i wondered what would cause the toString() to not work as i wanted. is arrows toString basically just writing down arrows. I though it should be writing down the whole Bronze arrow for example Quote Link to comment Share on other sites More sharing options...
Explv Posted January 20, 2018 Share Posted January 20, 2018 (edited) 31 minutes ago, scriptersteve said: Hi, so in one of my previous scripts people mentioned I should try and use filters more: The commented out code is what i was trying to do but was refusing to 'loot' the arrows. The non-commented code works fine and i wondered if anyone new why the commented code didn't work? // Filter<Item> arrows = item -> item.getName().contains("arrows"); //GroundItem arrows1 = getGroundItems().closest(arrows.toString()); GroundItem arrows1 = getGroundItems().closest("Bronze arrow","Iron arrow","Steel arrow","Mithril arrow","Adamant arrow"); Below is current version of my *arrows1* GroundItem arrows1 = getGroundItems().closest(gi -> gi != null && getMap().canReach(gi) && gi.getName().contains("arrow")); This one does work but is not using a filter (although it is kinda filtering by the contains name) but i wondered what would cause the toString() to not work as i wanted. is arrows toString basically just writing down arrows. I though it should be writing down the whole Bronze arrow for example Don't call toString() closest() can take a Filter as a parameter. Edited January 20, 2018 by Explv Quote Link to comment Share on other sites More sharing options...
scriptersteve Posted January 20, 2018 Author Share Posted January 20, 2018 okay - thanks. Makes sense tbf Quote Link to comment Share on other sites More sharing options...
Chase Posted January 20, 2018 Share Posted January 20, 2018 38 minutes ago, scriptersteve said: Hi, so in one of my previous scripts people mentioned I should try and use filters more: The commented out code is what i was trying to do but was refusing to 'loot' the arrows. The non-commented code works fine and i wondered if anyone new why the commented code didn't work? // Filter<Item> arrows = item -> item.getName().contains("arrows"); //GroundItem arrows1 = getGroundItems().closest(arrows.toString()); GroundItem arrows1 = getGroundItems().closest("Bronze arrow","Iron arrow","Steel arrow","Mithril arrow","Adamant arrow"); Below is current version of my *arrows1* GroundItem arrows1 = getGroundItems().closest(gi -> gi != null && getMap().canReach(gi) && gi.getName().contains("arrow")); This one does work but is not using a filter (although it is kinda filtering by the contains name) but i wondered what would cause the toString() to not work as i wanted. is arrows toString basically just writing down arrows. I though it should be writing down the whole Bronze arrow for example If I'm not mistaken your current version of *arrows1* , is actually a filter. You're simply using some syntactical sugar to do it inline! For reference below is what is really happening behind the scenes GroundItem arrow = GroundItems.closest(new Filter<GroundItem>(){ public boolean match(GroundItem item){ return item != null && item.getName().contains("arrow") && Map.canReach(item); } }); Quote Link to comment Share on other sites More sharing options...
scriptersteve Posted January 20, 2018 Author Share Posted January 20, 2018 36 minutes ago, withoutidols said: If I'm not mistaken your current version of *arrows1* , is actually a filter. You're simply using some syntactical sugar to do it inline! For reference below is what is really happening behind the scenes GroundItem arrow = GroundItems.closest(new Filter<GroundItem>(){ public boolean match(GroundItem item){ return item != null && item.getName().contains("arrow") && Map.canReach(item); } }); Thanks - that does help my understanding reading it though Quote Link to comment Share on other sites More sharing options...