Querying

How to find any "thing" in-game.

The Query API is powerful tool used to find any "thing" in-game, providing a wide array of filters that can be used to isolate the entity you are searching for. Queries are highly optimized, and results are evaluated lazily, so query builders can be re-used.

There are many kinds of query builders, each of which way may have filters to make identifying entities easier. Here are the main types (and their inheritance structure):

Query Builder Types

There are several types of QueryBuilder, the most commonly used ones are covered below:

The SpriteItemQueryBuilder is used to search for items contained with InterfaceComponents, such as in the inventory, equipment or bank.

Depending on the origin of the item you are searching for, a SpriteItemQueryBuilder can be created using one of the following:

//Inventory items
Inventory.newQuery()

//Equipment
Equipment.newQuery()

//Bank
Bank.newQuery()

//Trade (outgoing)
Trade.Outgoing.newQuery()

//Trade (incoming)
Trade.Incoming.newQuery()

//Looting bag
LootingBag.newQuery()

//Seed vault
SeedVault.newQuery()

Utility methods like these exist for the more common inventory types. Queries can be constructed for uncommon inventories by passing an inventory to the SpriteItemQueryBuilder:

new SpriteItemQueryBuilder(Inventories.Documented.INVENTORY)

Query Results

QueryResults are evaluated lazily when QueryBuilder#results() is invoked, so builders can be re-used. QueryResults are a mutable Collection, so can be iterated and mutated. Some convenience methods are available to make accessing your desired result easier:

//Getting the first "Cake" in your inventory
SpriteItem cake = Inventory.newQuery().names("Cake").results().first();

//Get the last "Cake" in your inventory
SpriteItem cake = Inventory.newQuery().names("Cake").results().last();

//Get a random "Cake" in your inventory
SpriteItem cake = Inventory.newQuery().names("Cake").results().random();

//Finding the nearest tree to your player
GameObject tree = GameObjects.newQuery().names("Tree").results().nearest();

//Finding the nearest logs on the floor next to the tree
GroundItem logs = GroundItems.newQuery().names("Logs").results().nearestTo(tree);

Remember your null checks! RuneScape is a dynamic game and queries are not guaranteed to succeed, so failing to properly null check may result in crashes due to NullPointerExceptions.

Last updated