diff --git a/runelite-api/src/main/java/net/runelite/api/Actor.java b/runelite-api/src/main/java/net/runelite/api/Actor.java index 78d3927d59..460e12132e 100644 --- a/runelite-api/src/main/java/net/runelite/api/Actor.java +++ b/runelite-api/src/main/java/net/runelite/api/Actor.java @@ -35,7 +35,7 @@ import net.runelite.api.coords.WorldPoint; /** * Represents a RuneScape actor/entity. */ -public interface Actor extends Entity +public interface Actor extends Entity, Locatable { /** * Gets the combat level of the actor. diff --git a/runelite-api/src/main/java/net/runelite/api/Locatable.java b/runelite-api/src/main/java/net/runelite/api/Locatable.java new file mode 100644 index 0000000000..5817df51ab --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/Locatable.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2019, tha23rd + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api; + +import net.runelite.api.coords.LocalPoint; +import net.runelite.api.coords.WorldPoint; + +public interface Locatable +{ + /** + * Gets the server-side location of the actor. + *

+ * This value is typically ahead of where the client renders and is not + * affected by things such as animations. + * + * @return the server location + */ + WorldPoint getWorldLocation(); + + /** + * Gets the client-side location of the actor. + * + * @return the client location + */ + LocalPoint getLocalLocation(); +} diff --git a/runelite-api/src/main/java/net/runelite/api/LocatableQueryResults.java b/runelite-api/src/main/java/net/runelite/api/LocatableQueryResults.java new file mode 100644 index 0000000000..917977b67f --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/LocatableQueryResults.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2019, tha23rd + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api; + +import java.util.Collection; +import java.util.Comparator; +import javax.annotation.Nullable; + +public class LocatableQueryResults extends QueryResults +{ + + public LocatableQueryResults(Collection results) + { + super(results); + } + + @Nullable + public EntityType nearestTo(Locatable locatable) + { + return this.stream() + .min(Comparator.comparing(entityType -> entityType.getLocalLocation().distanceTo(locatable.getLocalLocation()))) + .orElse(null); + } + +} diff --git a/runelite-api/src/main/java/net/runelite/api/Query.java b/runelite-api/src/main/java/net/runelite/api/Query.java new file mode 100644 index 0000000000..413cda7dfa --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/Query.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2017, Devin French + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api; + +import java.util.function.Predicate; + +/** + * A query to search the game for objects that match. + * + * @param the returned object type + * @param the query type + */ +public abstract class Query +{ + protected Predicate predicate = x -> true; + + protected Query() + { + } + + /** + * Executes the query and filters through possible objects, returning only + * those who evaluate true using {@link #predicate}. + * + * @param client the game client + * @return the matching objects + */ + public abstract QR result(Client client); + + /** + * Constructs and returns a predicate that will evaluate {@link #predicate} + * and the passed value. + * + * @param other the passed predicate + * @return the combined predicate + */ + protected Predicate and(Predicate other) + { + if (predicate == null) + { + return other; + } + return predicate.and(other); + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/QueryResults.java b/runelite-api/src/main/java/net/runelite/api/QueryResults.java new file mode 100644 index 0000000000..e62223b3c6 --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/QueryResults.java @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2019, tha23rd + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import javax.annotation.Nullable; + +public class QueryResults implements Collection +{ + + public final ArrayList list; + + public QueryResults(Collection results) + { + if (results == null) + { + list = new ArrayList<>(); + } + else + { + if (results instanceof ArrayList) + { + list = (ArrayList) results; + } + else + { + list = new ArrayList<>(results); + } + } + + } + + @Override + public int size() + { + return list.size(); + } + + @Override + public boolean isEmpty() + { + return this.size() == 0; + } + + @Override + public boolean contains(Object o) + { + return list.contains(o); + } + + @Override + public Iterator iterator() + { + return list.iterator(); + } + + @Override + public Object[] toArray() + { + return list.toArray(); + } + + @Override + public entityType[] toArray(entityType[] a) + { + return (entityType[]) this.list.toArray(); + } + + @Override + public boolean add(EntityType entityType) + { + return list.add(entityType); + } + + @Override + public boolean remove(Object o) + { + return list.remove(o); + } + + @Override + public boolean containsAll(Collection c) + { + return list.containsAll(c); + } + + @Override + public boolean addAll(Collection c) + { + return list.addAll(c); + } + + @Override + public boolean removeAll(Collection c) + { + return list.removeAll(c); + } + + @Override + public boolean retainAll(Collection c) + { + return list.retainAll(c); + } + + @Override + public void clear() + { + list.clear(); + } + + @Nullable + public final EntityType first() + { + return this.size() == 0 ? null : this.get(0); + } + + @Nullable + public final EntityType last() + { + int size; + return (size = this.size()) == 0 ? null : this.get(size - 1); + } + + public EntityType get(int index) + { + return list.get(index); + } + + public final QueryResults limit(int entries) + { + return this.limit(0, entries); + } + + public final QueryResults limit(int startIndex, int amount) + { + List limitedList = new ArrayList<>(amount); + + for (int i = startIndex; i < this.size() && i - startIndex < amount; i++) + { + limitedList.add(this.get(i)); + } + + this.list.retainAll(limitedList); + return this; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/TileObject.java b/runelite-api/src/main/java/net/runelite/api/TileObject.java index fa8e595612..604b54f51d 100644 --- a/runelite-api/src/main/java/net/runelite/api/TileObject.java +++ b/runelite-api/src/main/java/net/runelite/api/TileObject.java @@ -34,7 +34,7 @@ import javax.annotation.Nullable; /** * Represents an object that a tile holds. */ -public interface TileObject +public interface TileObject extends Locatable { /** * Gets the hashed value of this object. diff --git a/runelite-api/src/main/java/net/runelite/api/queries/ActorQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/ActorQuery.java new file mode 100644 index 0000000000..339c8c9a8a --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/queries/ActorQuery.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2017, Devin French + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.queries; + +import java.util.function.Predicate; +import net.runelite.api.Actor; +import net.runelite.api.coords.WorldPoint; + +public abstract class ActorQuery extends LocatableQuery +{ + @SuppressWarnings("unchecked") + public QueryType nameEquals(String... names) + { + predicate = and(actor -> + { + for (String name : names) + { + String actorName = actor.getName(); + if (actorName != null && actorName.equals(name)) + { + return true; + } + } + return false; + }); + return (QueryType) this; + } + + @SuppressWarnings("unchecked") + public QueryType nameContains(String... names) + { + predicate = and(actor -> + { + for (String name : names) + { + String actorName = actor.getName(); + if (actorName != null && actorName.contains(name)) + { + return true; + } + } + return false; + }); + return (QueryType) this; + } + + @SuppressWarnings("unchecked") + public QueryType isLevel(int level) + { + predicate = and(actor -> actor.getCombatLevel() == level); + return (QueryType) this; + } + + @SuppressWarnings("unchecked") + public QueryType animationEquals(int animation) + { + predicate = and(actor -> actor.getAnimation() == animation); + return (QueryType) this; + } + + @SuppressWarnings("unchecked") + public QueryType isInteractingWith(Actor actor) + { + predicate = and(a -> a.getInteracting().equals(actor)); + return (QueryType) this; + } + + @SuppressWarnings("unchecked") + public QueryType isWithinArea(WorldPoint from, int area) + { + predicate = and(a -> a.getWorldArea().distanceTo(from) <= area); + return (QueryType) this; + } + + @SuppressWarnings("unchecked") + public QueryType hasNoHealthBar() + { + predicate = and(a -> a.getHealthRatio() == -1); + return (QueryType) this; + } + + @SuppressWarnings("unchecked") + public QueryType filter(Predicate other) + { + predicate = and(other); + return (QueryType) this; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/queries/BankItemQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/BankItemQuery.java new file mode 100644 index 0000000000..31bfc77f25 --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/queries/BankItemQuery.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2017, Devin French + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.queries; + +import java.awt.Rectangle; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Objects; +import java.util.stream.Collectors; +import net.runelite.api.Client; +import net.runelite.api.QueryResults; +import net.runelite.api.widgets.Widget; +import net.runelite.api.widgets.WidgetInfo; +import net.runelite.api.widgets.WidgetItem; + +public class BankItemQuery extends WidgetItemQuery +{ + private static final int ITEM_EMPTY = 6512; + + @Override + public QueryResults result(Client client) + { + Collection widgetItems = getBankItems(client); + return new QueryResults<>(widgetItems.stream() + .filter(Objects::nonNull) + .filter(predicate) + .collect(Collectors.toList())); + } + + private Collection getBankItems(Client client) + { + Collection widgetItems = new ArrayList<>(); + Widget bank = client.getWidget(WidgetInfo.BANK_ITEM_CONTAINER); + if (bank != null && !bank.isHidden()) + { + Widget[] children = bank.getDynamicChildren(); + for (int i = 0; i < children.length; i++) + { + Widget child = children[i]; + if (child.getItemId() == ITEM_EMPTY || child.isSelfHidden()) + { + continue; + } + // set bounds to same size as default inventory + Rectangle bounds = child.getBounds(); + bounds.setBounds(bounds.x - 1, bounds.y - 1, 32, 32); + // Index is set to 0 because the widget's index does not correlate to the order in the bank + widgetItems.add(new WidgetItem(child.getItemId(), child.getItemQuantity(), 0, bounds, child)); + } + } + return widgetItems; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/queries/DecorativeObjectQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/DecorativeObjectQuery.java new file mode 100644 index 0000000000..8a9e88a315 --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/queries/DecorativeObjectQuery.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2017, Devin French + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.queries; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Objects; +import java.util.stream.Collectors; +import net.runelite.api.Client; +import net.runelite.api.DecorativeObject; +import net.runelite.api.LocatableQueryResults; +import net.runelite.api.Tile; + +public class DecorativeObjectQuery extends TileObjectQuery +{ + @Override + public LocatableQueryResults result(Client client) + { + return new LocatableQueryResults<>(getDecorativeObjects(client).stream() + .filter(Objects::nonNull) + .filter(predicate) + .distinct() + .collect(Collectors.toList())); + } + + private Collection getDecorativeObjects(Client client) + { + Collection objects = new ArrayList<>(); + for (Tile tile : getTiles(client)) + { + objects.add(tile.getDecorativeObject()); + } + return objects; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/queries/DialogQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/DialogQuery.java new file mode 100644 index 0000000000..426bdf00a4 --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/queries/DialogQuery.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2019, tha23rd + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.queries; + +import java.awt.Rectangle; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Objects; +import java.util.stream.Collectors; +import net.runelite.api.Client; +import net.runelite.api.QueryResults; +import net.runelite.api.widgets.Widget; +import net.runelite.api.widgets.WidgetInfo; +import net.runelite.api.widgets.WidgetItem; + +public class DialogQuery extends WidgetItemQuery +{ + + @Override + public QueryResults result(Client client) + { + Collection widgetItems = getDialogs(client); + return new QueryResults<>(widgetItems.stream() + .filter(Objects::nonNull) + .filter(predicate) + .collect(Collectors.toList())); + } + + private Collection getDialogs(Client client) + { + boolean npcDialog = false; + Collection widgetItems = new ArrayList<>(); + Widget dialog = client.getWidget(219, 1); + if (dialog == null || dialog.isHidden()) + { + dialog = client.getWidget(WidgetInfo.DIALOG_NPC); + npcDialog = true; + } + if (dialog == null || dialog.isHidden()) + { + dialog = client.getWidget(WidgetInfo.DIALOG_PLAYER); + npcDialog = true; + } + + if (dialog != null && !dialog.isHidden()) + { + Widget[] children = npcDialog ? dialog.getStaticChildren() : dialog.getDynamicChildren(); + for (int i = 1; i < children.length; i++) + { + Widget child = children[i]; + // set bounds to same size as default inventory + Rectangle bounds = child.getBounds(); + bounds.setBounds(bounds.x - 1, bounds.y - 1, 32, 32); + widgetItems.add(new WidgetItem(child.getId(), child.getItemQuantity(), i - 1, bounds, child)); + } + } + return widgetItems; + } + + public WidgetItemQuery textContains(String... texts) + { + predicate = and(widgetItem -> + { + for (String text : texts) + { + if (widgetItem.getWidget() != null) + { + String widgetText = widgetItem.getWidget().getText(); + if (widgetText != null && widgetText.contains(text)) + { + return true; + } + } + } + return false; + }); + return this; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/queries/EquipmentItemQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/EquipmentItemQuery.java new file mode 100644 index 0000000000..cc26a4f5c0 --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/queries/EquipmentItemQuery.java @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2017, Devin French + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.queries; + +import java.awt.Rectangle; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Objects; +import java.util.stream.Collectors; +import net.runelite.api.Client; +import net.runelite.api.QueryResults; +import net.runelite.api.widgets.Widget; +import net.runelite.api.widgets.WidgetInfo; +import net.runelite.api.widgets.WidgetItem; + +public class EquipmentItemQuery extends WidgetItemQuery +{ + private static final WidgetInfo[] ALL_EQUIPMENT_WIDGET_INFOS = + { + WidgetInfo.EQUIPMENT_HELMET, + WidgetInfo.EQUIPMENT_CAPE, + WidgetInfo.EQUIPMENT_AMULET, + WidgetInfo.EQUIPMENT_WEAPON, + WidgetInfo.EQUIPMENT_BODY, + WidgetInfo.EQUIPMENT_SHIELD, + WidgetInfo.EQUIPMENT_LEGS, + WidgetInfo.EQUIPMENT_GLOVES, + WidgetInfo.EQUIPMENT_BOOTS, + WidgetInfo.EQUIPMENT_RING, + WidgetInfo.EQUIPMENT_AMMO, + }; + + private final Collection slots = new ArrayList<>(); + + public EquipmentItemQuery slotEquals(WidgetInfo... slotWidgetInfo) + { + slots.addAll(Arrays.asList(slotWidgetInfo)); + return this; + } + + @Override + public QueryResults result(Client client) + { + Collection widgetItems = getEquippedItems(client); + return new QueryResults<>(widgetItems.stream() + .filter(Objects::nonNull) + .filter(predicate) + .collect(Collectors.toList())); + } + + private Collection getEquippedItems(Client client) + { + Collection widgetItems = new ArrayList<>(); + Widget equipment = client.getWidget(WidgetInfo.EQUIPMENT); + if (equipment != null && !equipment.isHidden()) + { + if (slots.isEmpty()) + { + slots.addAll(Arrays.asList(ALL_EQUIPMENT_WIDGET_INFOS)); + } + for (WidgetInfo slot : slots) + { + Widget parentWidget = client.getWidget(slot); + Widget itemWidget = parentWidget.getChild(1); + // Check if background icon is hidden. if hidden, item is equipped. + boolean equipped = parentWidget.getChild(2).isSelfHidden(); + // set bounds to same size as default inventory + Rectangle bounds = itemWidget.getBounds(); + bounds.setBounds(bounds.x - 1, bounds.y - 1, 32, 32); + // Index is set to 0 because there is no set in stone order of equipment slots + widgetItems.add(new WidgetItem(equipped ? itemWidget.getItemId() : -1, itemWidget.getItemQuantity(), 0, bounds, itemWidget)); + } + } + return widgetItems; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/queries/GameObjectQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/GameObjectQuery.java new file mode 100644 index 0000000000..1db7f054c1 --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/queries/GameObjectQuery.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2017, Devin French + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.queries; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Objects; +import java.util.stream.Collectors; +import net.runelite.api.Client; +import net.runelite.api.GameObject; +import net.runelite.api.LocatableQueryResults; +import net.runelite.api.Tile; + +public class GameObjectQuery extends TileObjectQuery +{ + @Override + public LocatableQueryResults result(Client client) + { + return new LocatableQueryResults<>(getGameObjects(client).stream() + .filter(Objects::nonNull) + .filter(predicate) + .distinct() + .collect(Collectors.toList())); + } + + private Collection getGameObjects(Client client) + { + Collection objects = new ArrayList<>(); + for (Tile tile : getTiles(client)) + { + GameObject[] gameObjects = tile.getGameObjects(); + if (gameObjects != null) + { + objects.addAll(Arrays.asList(gameObjects)); + } + } + return objects; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/queries/GroundObjectQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/GroundObjectQuery.java new file mode 100644 index 0000000000..86b0aa931f --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/queries/GroundObjectQuery.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2017, Devin French + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.queries; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Objects; +import java.util.stream.Collectors; +import net.runelite.api.Client; +import net.runelite.api.GroundObject; +import net.runelite.api.LocatableQueryResults; +import net.runelite.api.Tile; + +public class GroundObjectQuery extends TileObjectQuery +{ + @Override + public LocatableQueryResults result(Client client) + { + return new LocatableQueryResults<>(getGroundObjects(client).stream() + .filter(Objects::nonNull) + .filter(predicate) + .distinct() + .collect(Collectors.toList())); + } + + private Collection getGroundObjects(Client client) + { + Collection objects = new ArrayList<>(); + for (Tile tile : getTiles(client)) + { + objects.add(tile.getGroundObject()); + } + return objects; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/queries/InventoryItemQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/InventoryItemQuery.java new file mode 100644 index 0000000000..7ed7f468be --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/queries/InventoryItemQuery.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2016-2018, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.queries; + +import java.util.Arrays; +import java.util.Objects; +import java.util.stream.Collectors; +import lombok.RequiredArgsConstructor; +import net.runelite.api.Client; +import net.runelite.api.InventoryID; +import net.runelite.api.Item; +import net.runelite.api.ItemContainer; +import net.runelite.api.Query; +import net.runelite.api.QueryResults; + +@RequiredArgsConstructor +public class InventoryItemQuery extends Query> +{ + private final InventoryID inventory; + + @Override + public QueryResults result(Client client) + { + ItemContainer container = client.getItemContainer(inventory); + if (container == null) + { + return new QueryResults<>(null); + } + return new QueryResults<>(Arrays.stream(container.getItems()) + .filter(Objects::nonNull) + .filter(predicate) + .collect(Collectors.toList())); + } + + public InventoryItemQuery idEquals(int... ids) + { + predicate = and(item -> + { + for (int id : ids) + { + if (item.getId() == id) + { + return true; + } + } + return false; + }); + return this; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/queries/InventoryWidgetItemQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/InventoryWidgetItemQuery.java new file mode 100644 index 0000000000..e6d4e2a588 --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/queries/InventoryWidgetItemQuery.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2017, Devin French + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.queries; + +import java.awt.Rectangle; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Objects; +import java.util.stream.Collectors; +import net.runelite.api.Client; +import net.runelite.api.QueryResults; +import net.runelite.api.widgets.Widget; +import net.runelite.api.widgets.WidgetInfo; +import net.runelite.api.widgets.WidgetItem; + +public class InventoryWidgetItemQuery extends WidgetItemQuery +{ + private static final WidgetInfo[] INVENTORY_WIDGET_INFOS = + { + WidgetInfo.DEPOSIT_BOX_INVENTORY_ITEMS_CONTAINER, + WidgetInfo.BANK_INVENTORY_ITEMS_CONTAINER, + WidgetInfo.SHOP_INVENTORY_ITEMS_CONTAINER, + WidgetInfo.GRAND_EXCHANGE_INVENTORY_ITEMS_CONTAINER, + WidgetInfo.GUIDE_PRICES_INVENTORY_ITEMS_CONTAINER, + WidgetInfo.EQUIPMENT_INVENTORY_ITEMS_CONTAINER, + WidgetInfo.INVENTORY + }; + + @Override + public QueryResults result(Client client) + { + Collection widgetItems = getInventoryItems(client); + return new QueryResults<>(widgetItems.stream() + .filter(Objects::nonNull) + .filter(predicate) + .collect(Collectors.toList())); + } + + private Collection getInventoryItems(Client client) + { + Collection widgetItems = new ArrayList<>(); + for (WidgetInfo widgetInfo : INVENTORY_WIDGET_INFOS) + { + Widget inventory = client.getWidget(widgetInfo); + if (inventory == null || inventory.isHidden()) + { + continue; + } + if (widgetInfo == WidgetInfo.INVENTORY) + { + widgetItems.addAll(inventory.getWidgetItems()); + break; + } + else + { + Widget[] children = inventory.getDynamicChildren(); + for (int i = 0; i < children.length; i++) + { + Widget child = children[i]; + // set bounds to same size as default inventory + Rectangle bounds = child.getBounds(); + bounds.setBounds(bounds.x - 1, bounds.y - 1, 32, 32); + widgetItems.add(new WidgetItem(child.getItemId(), child.getItemQuantity(), i, bounds, child)); + } + break; + } + } + return widgetItems; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/queries/LocatableQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/LocatableQuery.java new file mode 100644 index 0000000000..07447b1944 --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/queries/LocatableQuery.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2019, tha23rd + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.queries; + +import static java.lang.Math.abs; +import java.util.function.Predicate; +import net.runelite.api.Locatable; +import net.runelite.api.LocatableQueryResults; +import net.runelite.api.Query; +import net.runelite.api.coords.LocalPoint; +import net.runelite.api.coords.WorldPoint; + +public abstract class LocatableQuery extends Query> +{ + @SuppressWarnings("unchecked") + public QueryType atWorldLocation(WorldPoint location) + { + predicate = and(object -> object.getWorldLocation().equals(location)); + return (QueryType) this; + } + + @SuppressWarnings("unchecked") + public QueryType atLocalLocation(LocalPoint location) + { + predicate = and(object -> object.getLocalLocation().equals(location)); + return (QueryType) this; + } + + @SuppressWarnings("unchecked") + public QueryType isWithinDistance(LocalPoint to, int distance) + { + predicate = and(a -> a.getLocalLocation().distanceTo(to) <= distance); + return (QueryType) this; + } + + @SuppressWarnings("unchecked") + public QueryType isWithinDistance(WorldPoint to, int distance) + { + predicate = and(a -> a.getWorldLocation().distanceTo(to) <= distance); + return (QueryType) this; + } + + @SuppressWarnings("unchecked") + public QueryType isWithinArea(LocalPoint from, int area) + { + predicate = and(a -> + { + LocalPoint localLocation = a.getLocalLocation(); + return abs(localLocation.getX() - from.getX()) < area + && abs(localLocation.getY() - from.getY()) < area; + }); + return (QueryType) this; + } + + @SuppressWarnings("unchecked") + public QueryType filter(Predicate other) + { + predicate = and(other); + return (QueryType) this; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/queries/NPCQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/NPCQuery.java new file mode 100644 index 0000000000..03ab7bba83 --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/queries/NPCQuery.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2017, Devin French + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.queries; + +import java.util.Collection; +import java.util.stream.Collectors; +import net.runelite.api.Client; +import net.runelite.api.LocatableQueryResults; +import net.runelite.api.NPC; + +public class NPCQuery extends ActorQuery +{ + @Override + public LocatableQueryResults result(Client client) + { + return new LocatableQueryResults<>(client.getNpcs().stream() + .filter(predicate) + .collect(Collectors.toList())); + } + + @SuppressWarnings("unchecked") + public NPCQuery idEquals(int... ids) + { + predicate = and(object -> + { + for (int id : ids) + { + if (object.getId() == id) + { + return true; + } + } + return false; + }); + return this; + } + + @SuppressWarnings("unchecked") + public NPCQuery idEquals(Collection ids) + { + predicate = and((object) -> ids.contains(object.getId())); + return this; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/queries/PlayerQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/PlayerQuery.java new file mode 100644 index 0000000000..a9496a8e4e --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/queries/PlayerQuery.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2017, Devin French + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.queries; + +import java.util.Collection; +import java.util.stream.Collectors; +import net.runelite.api.Client; +import net.runelite.api.LocatableQueryResults; +import net.runelite.api.Player; + +public class PlayerQuery extends ActorQuery +{ + @Override + public LocatableQueryResults result(Client client) + { + Collection players = client.getPlayers(); + return new LocatableQueryResults<>(players.stream() + .filter(predicate) + .collect(Collectors.toList())); + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/queries/ShopItemQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/ShopItemQuery.java new file mode 100644 index 0000000000..92116d2b9a --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/queries/ShopItemQuery.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2017, Devin French + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.queries; + +import java.awt.Rectangle; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Objects; +import java.util.stream.Collectors; +import net.runelite.api.Client; +import net.runelite.api.QueryResults; +import net.runelite.api.widgets.Widget; +import net.runelite.api.widgets.WidgetInfo; +import net.runelite.api.widgets.WidgetItem; + +public class ShopItemQuery extends WidgetItemQuery +{ + @Override + public QueryResults result(Client client) + { + Collection widgetItems = getShopItems(client); + return new QueryResults<>(widgetItems.stream() + .filter(Objects::nonNull) + .filter(predicate) + .collect(Collectors.toList())); + } + + private Collection getShopItems(Client client) + { + Collection widgetItems = new ArrayList<>(); + Widget shop = client.getWidget(WidgetInfo.SHOP_ITEMS_CONTAINER); + if (shop != null && !shop.isHidden()) + { + Widget[] children = shop.getDynamicChildren(); + for (int i = 1; i < children.length; i++) + { + Widget child = children[i]; + // set bounds to same size as default inventory + Rectangle bounds = child.getBounds(); + bounds.setBounds(bounds.x - 1, bounds.y - 1, 32, 32); + widgetItems.add(new WidgetItem(child.getItemId(), child.getItemQuantity(), i - 1, bounds, child)); + } + } + return widgetItems; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/queries/TileObjectQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/TileObjectQuery.java new file mode 100644 index 0000000000..f58dc01969 --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/queries/TileObjectQuery.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2017, Devin French + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.queries; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.function.Predicate; +import net.runelite.api.Client; +import net.runelite.api.Constants; +import net.runelite.api.Scene; +import net.runelite.api.Tile; +import net.runelite.api.TileObject; + +public abstract class TileObjectQuery extends LocatableQuery +{ + protected List getTiles(Client client) + { + List tilesList = new ArrayList<>(); + Scene scene = client.getScene(); + Tile[][][] tiles = scene.getTiles(); + int z = client.getPlane(); + for (int x = 0; x < Constants.SCENE_SIZE; ++x) + { + for (int y = 0; y < Constants.SCENE_SIZE; ++y) + { + Tile tile = tiles[z][x][y]; + if (tile == null) + { + continue; + } + tilesList.add(tile); + } + } + return tilesList; + } + + @SuppressWarnings("unchecked") + public QueryType idEquals(int... ids) + { + predicate = and(object -> + { + for (int id : ids) + { + if (object.getId() == id) + { + return true; + } + } + return false; + }); + return (QueryType) this; + } + + + @SuppressWarnings("unchecked") + public QueryType idEquals(Collection ids) + { + predicate = and((object) -> ids.contains(object.getId())); + return (QueryType) this; + } + + @SuppressWarnings("unchecked") + public QueryType filter(Predicate other) + { + predicate = and(other); + return (QueryType) this; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/queries/WallObjectQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/WallObjectQuery.java new file mode 100644 index 0000000000..2632003798 --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/queries/WallObjectQuery.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2017, Devin French + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.queries; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Objects; +import java.util.stream.Collectors; +import net.runelite.api.Client; +import net.runelite.api.LocatableQueryResults; +import net.runelite.api.Tile; +import net.runelite.api.WallObject; + +public class WallObjectQuery extends TileObjectQuery +{ + @Override + public LocatableQueryResults result(Client client) + { + return new LocatableQueryResults<>(getWallObjects(client).stream() + .filter(Objects::nonNull) + .filter(predicate) + .distinct() + .collect(Collectors.toList())); + } + + private Collection getWallObjects(Client client) + { + Collection objects = new ArrayList<>(); + for (Tile tile : getTiles(client)) + { + objects.add(tile.getWallObject()); + } + return objects; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/queries/WidgetItemQuery.java b/runelite-api/src/main/java/net/runelite/api/queries/WidgetItemQuery.java new file mode 100644 index 0000000000..edf60f41db --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/queries/WidgetItemQuery.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2017, Devin French + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.api.queries; + +import java.util.Collection; +import java.util.function.Predicate; +import net.runelite.api.Query; +import net.runelite.api.QueryResults; +import net.runelite.api.widgets.WidgetItem; + +public abstract class WidgetItemQuery extends Query> +{ + public WidgetItemQuery idEquals(int... ids) + { + predicate = and(item -> + { + for (int id : ids) + { + if (item.getId() == id) + { + return true; + } + } + return false; + }); + return this; + } + + public WidgetItemQuery idEquals(Collection ids) + { + predicate = and((object) -> ids.contains(object.getId())); + return this; + } + + public WidgetItemQuery indexEquals(int... indexes) + { + predicate = and(item -> + { + for (int index : indexes) + { + if (item.getIndex() == index) + { + return true; + } + } + return false; + }); + return this; + } + + public WidgetItemQuery quantityEquals(int quantity) + { + predicate = and(item -> item.getQuantity() == quantity); + return this; + } + + public WidgetItemQuery filter(Predicate other) + { + predicate = and(other); + return this; + } +}