Skip to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Idea for ContainsNameFilter

Featured Replies

Instead of needing to initialise this for methods with suitable parameters, why not continue to use String varargs with a new system whereby:

 

"$pickaxe" = String starts with

"pickaxe$" = String ends with

"$pickaxe$" = String contains

 

package com.liverare.utility;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.osbot.rs07.api.filter.Filter;

public class NameFilter {

	public static final String SPECIAL_CHAR = "$";

	private Map<String, Filter<String>> conditionCache;

	public NameFilter() {
		this.conditionCache = new HashMap<>();
	}

	/*
	 * Delegate methods
	 */

	public void clear() {
		conditionCache.clear();
	}

	public boolean contains(String key) {
		return conditionCache.containsKey(key);
	}

	public Set<Entry<String, Filter<String>>> entrySet() {
		return conditionCache.entrySet();
	}

	public Filter<String> get(String key) {
		Filter<String> filter = null;
		if (isSpecial(key)) {
			if (!contains(key)) {
				filter = generateCheck(getCheckType(key), key);
				if (filter != null) {
					conditionCache.put(key, filter);
				}
			} else {
				filter = conditionCache.get(key);
			}
		}
		return filter;
	}

	public boolean isEmpty() {
		return conditionCache.isEmpty();
	}

	public int size() {
		return conditionCache.size();
	}

	/*
	 * Static methods
	 */

	public static boolean isSpecial(String aString) {
		return aString != null && !aString.isEmpty()
				&& aString.contains(SPECIAL_CHAR);
	}

	public static CheckType getCheckType(String aString) {
		int i = 0;
		if (aString.startsWith(SPECIAL_CHAR)) {
			i |= 0x1;
		}
		if (aString.endsWith(SPECIAL_CHAR)) {
			i |= 0x2;
		}
		return CheckType.values()[i];
	}

	public static Filter<String> generateCheck(CheckType checkType, String source) {
		Filter<String> filter = null;
		if (checkType != null) {
			switch(checkType) {
			case STARTS_WITH:
				filter = new StartsWithFilter(source);
				break;
			case ENDS_WITH:
				filter = new EndsWithFilter(source);
				break;
			case CONTAINS:
				filter = new ContainsFilter(source);
				break;
			case NONE:
				// Do nothing
				break;
			}
		}
		return filter;
	}

	/*
	 * Additional classes
	 */
	public static enum CheckType {
		NONE, STARTS_WITH, ENDS_WITH, CONTAINS;
	}

}

/**
 * Abstract filter stub that stores a String instance that the filter will check
 * against.
 */
abstract class StringFilterStub implements Filter<String> {
	protected final String source;

	public StringFilterStub(String source) {
		if (source == null || source.isEmpty()) {
			throw new IllegalStateException(
					"String source cannot be null nor empty!");
		}
		this.source = source.toLowerCase().replace("$", "");
	}
}

class StartsWithFilter extends StringFilterStub {

	public StartsWithFilter(String source) {
		super(source);
	}

	@Override
	public boolean match(String arg0) {
		return arg0.toLowerCase().startsWith(source);
	}
	
}

class EndsWithFilter extends StringFilterStub {

	public EndsWithFilter(String source) {
		super(source);
	}

	@Override
	public boolean match(String arg0) {
		return arg0.toLowerCase().endsWith(source);
	}
	
}

class ContainsFilter extends StringFilterStub {

	public ContainsFilter(String source) {
		super(source);
	}

	@Override
	public boolean match(String arg0) {
		return arg0.toLowerCase().contains(source);
	}
	
}

Test:
 
Starts with

	public static void main(String[] arg0) {
		
		NameFilter nf = new NameFilter();
		
		Filter<String> f = nf.get("$pickaxe"); // starts with
		
		System.out.println(f.match("pickaxe")); // true
		System.out.println(f.match("Rune pickaxe")); // false
	}

Ends with

	public static void main(String[] arg0) {
		
		NameFilter nf = new NameFilter();
		
		Filter<String> f = nf.get("pickaxe$"); // ends with
		
		System.out.println(f.match("pickaxe")); // true
		System.out.println(f.match("pickaxe (broke)")); // false
	}

Contains

	public static void main(String[] arg0) {
		
		NameFilter nf = new NameFilter();
		
		Filter<String> f = nf.get("$pickaxe$"); // contains
		
		System.out.println(f.match("Adamant pickaxe (broke)")); // true
		System.out.println(f.match("Adamant pick-axe (broke)")); // false
	}

NPE

public static void main(String[] arg0) {
		
		NameFilter nf = new NameFilter();
		
		Filter<String> f = nf.get("pickaxe"); // no conditional check
		
		System.out.println(f.match("Adamant pickaxe (broke)")); // NPE
	}

Application:

npcs.closest("Guard", "Goblin", "Duck",
	"Knight$"); // White, Ardogune, etc.
		
bank.depositAllExcept("Varrock teleport", "Key", "pickaxe$");

The latter example is for this thread (which coincidentally inspired this idea!) smile.png

 

One thing I did not account for is the possibility that the dollar signs may be misplaced; they must be at the start of and/or end of a string, not in-between. Though, if this were to happen, the getCheckType will return CheckType#NONE, and it'll remain an invalid string that gets needlessly checks.

Edited by liverare

i like this idea, i see maxi liked it so do we execpt to see something like this added?

  • Author

i like this idea, i see maxi liked it so do we execpt to see something like this added?

Maybe. Thought I expect something a little better than what I farted out in 10 minutes or less. The code I've posted is functional and I'd also like to believe efficient, but doesn't offer much flexibility for multiple conditional checks on the same string.

  • Author

Couldn't you use regex with the name filter?

I suppose, but I haven't familiarised myself with much of regex. I use the online regex generator if/when I need to use it. So for this, I'll use what the String class already provides. But you're the developers, so you do what you think is best. :)

Edited by liverare

I suppose, but I haven't familiarised myself with much of regex. I use the online regex generator if/when I need to use it. So for this, I'll use what the String class already provides. But you're the developers, so you do what you think is best. smile.png

 

Although I might not implement this exact methodology, it did give me another great idea that would do essentially the same thing. I can't promise you'll see it soon but I have it scribbled down on my notebook.

Guest
This topic is now closed to further replies.

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.