TheScrub Posted January 13, 2014 Share Posted January 13, 2014 (edited) First of all you need to create an Interface public interface Filter<T> { public boolean accept(T arg0); } Example usage of the Filter import java.util.ArrayList; import org.osbot.script.Script; import org.osbot.script.rs2.model.GroundItem; import org.osbot.script.rs2.model.NPC; import org.osbot.script.rs2.model.RS2Object; import utils.Filter; public class ScrubUtils { private Script script; public ScrubUtils(Script script) { this.script = script; } public RS2Object[] getAllRS2Objects(Filter<RS2Object> filter) { ArrayList<RS2Object> array = new ArrayList<RS2Object>(); for (RS2Object rs2o : script.client.getCurrentRegion().getObjects()) { if (!array.contains(rs2o) && filter.accept(rs2o)) { array.add(rs2o); } } return array.toArray(new RS2Object[array.size()]); } public GroundItem[] getAllGroundItems(Filter<GroundItem> filter) { ArrayList<GroundItem> array = new ArrayList<GroundItem>(); for (GroundItem gi : script.client.getCurrentRegion().getItems()) { if (!array.contains(gi) && filter.accept(gi)) { array.add(gi); } } return array.toArray(new GroundItem[array.size()]); } public NPC[] getAllNPCS(Filter<NPC> filter) { ArrayList<NPC> array = new ArrayList<NPC>(); for (NPC n : script.client.getLocalNPCs()) { if (!array.contains(n) && filter.accept(n)) { array.add(n); } } return array.toArray(new NPC[array.size()]); } public RS2Object[] getObjectsWithAction(final String action) { Filter<RS2Object> filter = new Filter<RS2Object>() { @Override public boolean accept(RS2Object t) { if (t != null) { if (t.getDefinition() != null) { if (t.getDefinition().getActions() != null) { if (t.getDefinition().getActions().length > 0) { for (String a : t.getDefinition().getActions()) { if (a != null) { if (a.equalsIgnoreCase(action)) { return true; } } } } } } } return false; } }; return getAllRS2Objects(filter); } } Edited January 13, 2014 by TheScrub Link to comment Share on other sites More sharing options...
While Posted January 13, 2014 Share Posted January 13, 2014 inspired by codex ;) 1 Link to comment Share on other sites More sharing options...
Erke Posted January 13, 2014 Share Posted January 13, 2014 public RS2Object[] getObjectsWithAction(final String action) { Filter<RS2Object> filter = new Filter<RS2Object>() { @Override public boolean accept(RS2Object t) { if (t != null) { if (t.getDefinition() != null) { if (t.getDefinition().getActions() != null) { if (t.getDefinition().getActions().length > 0) { for (String a : t.getDefinition().getActions()) { if (a != null) { if (a.equalsIgnoreCase(action)) { return true; } } } } } } } return false; } }; return getAllRS2Objects(filter); } Nesting if-statments much? 1 Link to comment Share on other sites More sharing options...
Parameter Posted January 13, 2014 Share Posted January 13, 2014 (edited) You could just create a generic method. public <T> T filter(final T[] types, final Filter<T> filter) { for(final T type : types) { if(filter.accept(type)) { return type; } } return null; } Edited January 13, 2014 by Parameter 4 Link to comment Share on other sites More sharing options...
TheScrub Posted January 14, 2014 Author Share Posted January 14, 2014 You could just create a generic method. public <T> T filter(final T[] types, final Filter<T> filter) { for(final T type : types) { if(filter.accept(type)) { return type; } } return null; } ahh i like this Link to comment Share on other sites More sharing options...
While Posted January 14, 2014 Share Posted January 14, 2014 You could just create a generic method. public <T> T filter(final T[] types, final Filter<T> filter) { for(final T type : types) { if(filter.accept(type)) { return type; } } return null; } Generics will make it somewhat slower, but it's fine since it's unnoticeable. Link to comment Share on other sites More sharing options...