Jump to content

Filter's


TheScrub

Recommended Posts

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

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?

  • Like 1
Link to comment
Share on other sites

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 by Parameter
  • Like 4
Link to comment
Share on other sites

 

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

 

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

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...