Jump to content
scriptersteve

Java query regarding filters

Recommended Posts

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

Link to comment
Share on other sites

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

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);
  
  	}
  
  });

 

Link to comment
Share on other sites

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

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