Jump to content

Couple of inventory snippets


Assume

Recommended Posts

I needed them so I wrote them up. I haven't tested them so I cannot be sure they work but I don't see why they wouldn't.


package org.assume.api;

import java.util.ArrayList;
import java.util.List;



import org.osbot.script.Script;
import org.osbot.script.rs2.model.Item;


public class Methods extends Script
{

	public boolean inventoryContains(int...ids)
	{
		List<Integer> def = new ArrayList<Integer>();
		for(int i : ids)
		{
			def.add(i);
		}
		for(Item i : client.getInventory().getItems())
		{
			if(def.contains(i.getId()))
				return true;
		}
		return false;
	}

	public boolean inventoryContainsAll(int... ids)
	{
		int total = 0;
		List<Integer> def = new ArrayList<Integer>();
		for(int i : ids)
		{
			def.add(i);
		}
		for(Item i : client.getInventory().getItems())
		{
			if(def.contains(i.getId()))
			{
				total++;
				def.remove(i.getId());
			}
		}
		return total == ids.length;
	}

	public Item[] findInInventory(int... ids)
	{
		List<Item> list = new ArrayList<Item>();
		List<Integer> def = new ArrayList<Integer>();
		for(int i : ids)
		{
			def.add(i);
		}
		for(Item i : client.getInventory().getItems())
		{
			if(def.contains(i.getId()))
				list.add(i);
		}
		return list.toArray(new Item[list.size()]);
	}
}
Edited by Assume
Link to comment
Share on other sites

No offense, but your methods are horribly inefficient. Just use the plain old fixed arrays instead of parameterized ArrayLists. Also, in case of a "contains is true" event, you could also just directly return a 'true' boolean value instead of resizing an ArrayList. This can drastically reduces the execution time of your methods.

package org.assume.api;

import java.util.ArrayList;
import java.util.List;



import org.osbot.script.Script;
import org.osbot.script.rs2.model.Item;


public class Methods extends Script
{

	public boolean inventoryContains(int...ids)
	{
		List<Integer> def = new ArrayList<Integer>();
		for(int i : ids)
		{
			def.add(i);
		}
		for(Item i : client.getInventory().getItems())
		{
			if(def.contains(i.getId()))
				return true;
		}
		return false;
	}

	public boolean inventoryContainsAll(int... ids)
	{
		int total = 0;
		List<Integer> def = new ArrayList<Integer>();
		for(int i : ids)
		{
			def.add(i);
		}
		for(Item i : client.getInventory().getItems())
		{
			if(def.contains(i.getId()))
			{
				total++;
				def.remove(i.getId());
			}
		}
		return total == ids.length;
	}

	public Item[] findInInventory(int... ids)
	{
		List<Item> list = new ArrayList<Item>();
		List<Integer> def = new ArrayList<Integer>();
		for(int i : ids)
		{
			def.add(i);
		}
		for(Item i : client.getInventory().getItems())
		{
			if(def.contains(i.getId()))
				list.add(i);
		}
		return list.toArray(new Item[list.size()]);
	}
}

Checking an Array to see if it contains a value would be much less efficient than using a List. I would have to call a separate method to iterate through every value in the array and see if it contained the value. If you are referring to the findInInventory method using a normal array is impossible as I cannot dynamically add to it (arrays in Java have a fixed size). 

Edited by Assume
  • Like 1
Link to comment
Share on other sites

Why not use methods already provided by the inventory api, they're there for a reason.

public boolean inventoryContains(int...ids) {
	for(int id : ids) {
		if(client.getInventory().contains(id))
			return true;
	}
	return false;
}

public boolean inventoryContainsAll(int... ids) {
	for(int id : ids) {
		if(!client.getInventory().contains(id))
			return false;
	}
	return true;
}

public Item[] findInInventory(int... ids) {
	ArrayList<Item> list = new ArrayList<>();
	for(int id : ids) {
		for(Item item : client.getInventory().getItems()) {
			if(item.getId() == id && !list.contains(item))
				list.add(item);
		}
	}
	return list.toArray(new Item[list.size()]);
}
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...