Jump to content

Java query regarding filters


Recommended Posts

Posted

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

Posted (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 by Explv
Posted
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);
  
  	}
  
  });

 

Posted
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

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