Class AbstractQuery<T extends Interactable<R>,Q extends AbstractQuery<T,Q,R>,R>
- java.lang.Object
-
- com.kraken.api.core.AbstractQuery<T,Q,R>
-
- Type Parameters:
T- The type of object being queried (e.g., NpcEntity, WidgetEntity)Q- The concrete query class (e.g., NpcQuery)R- The raw RuneLite type (NPC, Widget, TileObject, etc.)
- Direct Known Subclasses:
BankInventoryQuery,BankQuery,EquipmentQuery,GameObjectQuery,GroundObjectQuery,InventoryQuery,NpcQuery,PlayerQuery,WidgetQuery,WorldQuery
public abstract class AbstractQuery<T extends Interactable<R>,Q extends AbstractQuery<T,Q,R>,R> extends java.lang.ObjectForms the base class for all game client queries. This class defines generic actions which can be taken on streams of game objects like NPC's, Ground Items, Tile Objects, Players and Widgets.
-
-
Constructor Summary
Constructors Constructor Description AbstractQuery(Context ctx)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description longcount()Returns a count of objects in the streamQdistinct(java.util.function.Function<T,java.lang.Object> keyExtractor)Filters the stream to only include elements that are distinct based on a property.Qexcept(java.util.function.Predicate<T> predicate)Filters out elements that match the given predicate.Qfilter(java.util.function.Predicate<T> predicate)Applies a predicate to the stream to filter elements of the stream.Tfirst()Returns the first type of object being queried (e.g., NpcEntity, WidgetEntity) from the stream.java.util.List<T>list()Returns the stream of entities as a list of objectsjava.util.Map<java.lang.Integer,T>map()Collects the stream of entities into a map keyed by the id of the element in the map.QnameContains(java.lang.String name)Filters for entities whose name contains the substring or a portion of the name parameter.Trandom()Returns a random element from the filtered list.java.util.List<T>result()An alias forlist().java.util.stream.Stream<T>reverse()Reverses the order of elements in the stream and returns a new stream with the reversed order.java.util.stream.Stream<T>shuffle()Randomizes the order of elements in the stream and returns a new stream with the shuffled elements.Qsorted(java.util.Comparator<T> comparator)Applies a comparator to the stream for sorting elements within the stream.protected abstract java.util.function.Supplier<java.util.stream.Stream<T>>source()java.util.stream.Stream<T>stream()Returns the raw stream of elements in the query so filters and matching can be manually applied.java.util.List<T>take(int n)Takes the first N elements from the stream and returns them as a list.java.util.stream.Stream<R>toRuneLite()Returns the underlying RuneLite entities that have been wrapped by the API.QwithId(int id)Filters the stream of game entities for ones where the ID matches a provided idQwithName(java.lang.String name)Filters for only entities whose name matches the provided name.
-
-
-
Field Detail
-
ctx
protected final Context ctx
-
-
Constructor Detail
-
AbstractQuery
public AbstractQuery(Context ctx)
-
-
Method Detail
-
source
protected abstract java.util.function.Supplier<java.util.stream.Stream<T>> source()
-
filter
public Q filter(java.util.function.Predicate<T> predicate)
Applies a predicate to the stream to filter elements of the stream.- Parameters:
predicate- Filter to add- Returns:
- Q
-
withName
public Q withName(java.lang.String name)
Filters for only entities whose name matches the provided name. This is case-insensitive.- Parameters:
name- The name of the object to filter for- Returns:
- Q entities whose name matches
-
withId
public Q withId(int id)
Filters the stream of game entities for ones where the ID matches a provided id- Parameters:
id- Int the item ID to match against.- Returns:
- Q entities whose item id matches the provided ID.
-
nameContains
public Q nameContains(java.lang.String name)
Filters for entities whose name contains the substring or a portion of the name parameter.- Parameters:
name- The name to match against- Returns:
- Entities whose name contains the prefix
-
shuffle
public java.util.stream.Stream<T> shuffle()
Randomizes the order of elements in the stream and returns a new stream with the shuffled elements.This method collects all elements in the stream into a list, shuffles the list using
Collections.shuffle(List), and then converts the shuffled list back into a stream for further use.Note: This operation consumes the original stream, making it unsuitable for re-use after calling this method. Additionally, the shuffling process may impact performance for very large datasets due to memory usage (as it fully materializes the stream into a list) and the shuffling algorithm.
- Returns:
- A new
Stream<T>containing the same elements as the original stream, but in a randomized order.
-
reverse
public java.util.stream.Stream<T> reverse()
Reverses the order of elements in the stream and returns a new stream with the reversed order.This method collects all elements in the stream into a list, reverses the list using
Collections.reverse(List), and then converts the reversed list back into a stream for further use.Note: This operation consumes the original stream, making it unsuitable for re-use after calling this method. Additionally, the reversal process may impact performance for very large datasets due to memory usage (as it fully materializes the stream into a list).
- Returns:
- A new
Stream<T>containing the same elements as the original stream, but in reversed order.
-
stream
public java.util.stream.Stream<T> stream()
Returns the raw stream of elements in the query so filters and matching can be manually applied.- Returns:
- Stream of entities
-
toRuneLite
public java.util.stream.Stream<R> toRuneLite()
Returns the underlying RuneLite entities that have been wrapped by the API. For example:ctx.gameObjects().toRuneLite()returns a list ofTileObjects. You will not be able to perform any interactions on these objects after callingtoRuneLiteas they lose theirInteractablewrapping.- Returns:
- Stream of RuneLite API objects
-
count
public long count()
Returns a count of objects in the stream- Returns:
- long count of objects.
-
except
public Q except(java.util.function.Predicate<T> predicate)
Filters out elements that match the given predicate. Effectively: filter(!predicate)- Parameters:
predicate- The predicate to apply- Returns:
- Q All entities except for the ones that match the given predicate
-
random
public T random()
Returns a random element from the filtered list. Useful for anti-ban measures (e.g., picking a random cow to attack).- Returns:
- T A random entity from the stream
-
distinct
public Q distinct(java.util.function.Function<T,java.lang.Object> keyExtractor)
Filters the stream to only include elements that are distinct based on a property. Usage: ctx.npcs().distinct(NpcEntity::getName).list(); (Returns one of each type of NPC nearby)- Parameters:
keyExtractor- The function to use to determine uniqueness keys between entities- Returns:
- Q The distinct entities
-
sorted
public Q sorted(java.util.Comparator<T> comparator)
Applies a comparator to the stream for sorting elements within the stream.- Parameters:
comparator- Comparator to add- Returns:
- Q A sorted stream of entities
-
list
public java.util.List<T> list()
Returns the stream of entities as a list of objects- Returns:
- A list of objects that have been queried (e.g., NpcEntity, WidgetEntity)
-
result
public java.util.List<T> result()
An alias forlist().- Returns:
- The stream of entities as a list of objects.
-
map
public java.util.Map<java.lang.Integer,T> map()
Collects the stream of entities into a map keyed by the id of the element in the map. Generally this will be the item id for objects likeContainerItem,EquipmentEntity, andGroundObjectEntitybut can take on other ids for things like Game objects, NPC's and widgets.- Returns:
- Map of entities keyed by their id.
-
first
public T first()
Returns the first type of object being queried (e.g., NpcEntity, WidgetEntity) from the stream. If the stream contains no objects then this will return null.- Returns:
- T The type of object being queried (e.g., NpcEntity, WidgetEntity)
-
take
public java.util.List<T> take(int n)
Takes the first N elements from the stream and returns them as a list.- Parameters:
n- The number of elements to take from the stream.- Returns:
- List of entities
-
-