Jump to content

liverare

Recommended Posts

I blinked, then..

Version 2

Example code:
 

public class Test extends Script {

	AmountAPI amount;
	
	RS2Widget vambracesButton;
	RS2Widget chapsButton;
	RS2Widget bodyButton;
	RS2Widget superCombatButton;

	@Override
	public void onStart() throws InterruptedException {
		amount = new AmountAPI();
		amount.exchangeContext(bot);
		amount.initializeModule();
	}

	@Override
	public int onLoop() throws InterruptedException {
		
		if (amount.isValid()) {
			
			vambracesButton = amount.getButton("Green d'hide vamb");
			chapsButton = amount.getButton("Green d'hide chaps");
			bodyButton = amount.getButton("Green d'hide body");
			superCombatButton = amount.getButton("Super combat potion(4)");
			
		} else {
			// open the interface...
		}
		
		return 250;
	}
	
}

Di7WcxK.png

rrsl21W.png

Functions:
 

public Map<ItemDefinition, RS2Widget> getItemButtons()
public RS2Widget getButton(int... itemIds)
public RS2Widget getButton(int itemId)
public RS2Widget getButton(String... itemNames)
public RS2Widget getButton(String itemName)
public RS2Widget getButton(Predicate<ItemDefinition> itemDefinitionFilter)
public RS2Widget getButton(ItemDefinition itemDefinition)
public boolean isVisible()
public boolean isValid()
private Map<ItemDefinition, RS2Widget> findItemButtons()
private boolean isRootOf(RS2Widget widget)
private RS2Widget findAmountWidget()
private static ItemDefinition getItem(RS2Widget parent)
private static int getItemId(RS2Widget parent)
private static RS2Widget getItemIdWidget(RS2Widget parent)
private static boolean isItemIdValid(RS2Widget child)
private static boolean isWidgetMakeAction(RS2Widget child)

Source:

 

package com.liverare.api;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.osbot.rs07.api.def.ItemDefinition;
import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.script.API;

/**
 * 
 * @author LiveRare
 *
 */
public class AmountAPI extends API {

	private RS2Widget amountWidget;
	private int rootId = -1;
	private Map<ItemDefinition, RS2Widget> itemButtons;

	@Override
	public void initializeModule() {
		amountWidget = findAmountWidget();
		if (amountWidget != null) {
			rootId = amountWidget.getRootId();
			itemButtons = findItemButtons();
		}
	}

	public Map<ItemDefinition, RS2Widget> getItemButtons() {
		return itemButtons;
	}

	public RS2Widget getButton(int... itemIds) {
		Arrays.sort(itemIds);
		return getButton(itemDefininition -> Arrays.binarySearch(itemIds, itemDefininition.getId()) >= 0);
	}

	public RS2Widget getButton(int itemId) {
		return getButton(itemDefinition -> itemDefinition.getId() == itemId);
	}

	public RS2Widget getButton(String... itemNames) {
		Arrays.sort(itemNames);
		return getButton(itemDefininition -> Arrays.binarySearch(itemNames, itemDefininition.getName()) >= 0);
	}
	
	public RS2Widget getButton(String itemName) {
		return getButton(itemDefinition -> itemDefinition.getName().equalsIgnoreCase(itemName));
	}
	
	public RS2Widget getButton(Predicate<ItemDefinition> itemDefinitionFilter) {
		RS2Widget widget = null;
		ItemDefinition itemDefinition;
		if (itemButtons != null) {
			itemDefinition = itemButtons.keySet().stream()
					.filter(itemDefinitionFilter)
					.findFirst()
					.orElse(null);
			widget = getButton(itemDefinition);
		}
		return widget;
	}

	public RS2Widget getButton(ItemDefinition itemDefinition) {
		return itemDefinition != null ? itemButtons.get(itemDefinition) : null;
	}

	public boolean isVisible() {
		return amountWidget.isVisible();
	}

	public boolean isValid() {
		if (amountWidget == null || rootId == -1) {
			initializeModule();
		} else {
			itemButtons = findItemButtons();
		}
		return amountWidget != null && rootId >= 0;
	}

	private Map<ItemDefinition, RS2Widget> findItemButtons() {
		return rootId == -1 ? null
				: widgets.getAll().stream()
						.filter(this::isRootOf)
						.filter(AmountAPI::isWidgetMakeAction)
						.collect(Collectors.toMap(AmountAPI::getItem, w -> w, (a, b) -> a));
	}

	private boolean isRootOf(RS2Widget widget) {
		return widget.getRootId() == rootId;
	}

	private RS2Widget findAmountWidget() {
		List<RS2Widget> l = widgets.containingText("How many do you wish to make?");
		return l != null && !l.isEmpty() ? l.get(0) : null;
	}

	private static ItemDefinition getItem(RS2Widget parent) {
		final int itemId = getItemId(parent);
		return itemId >= 0 ? ItemDefinition.forId(itemId) : null;
	}

	private static int getItemId(RS2Widget parent) {
		final RS2Widget spellNameWidget = getItemIdWidget(parent);
		return spellNameWidget != null ? spellNameWidget.getItemId() : -1;
	}

	private static RS2Widget getItemIdWidget(RS2Widget parent) {
		return Stream.of(parent.getChildWidgets())
				.filter(AmountAPI::isItemIdValid)
				.findFirst()
				.orElse(null);
	}

	private static boolean isItemIdValid(RS2Widget child) {
		final int itemId = child.getItemId();
		return itemId >= 0;
	}

	private static boolean isWidgetMakeAction(RS2Widget child) {
		String[] actions = child.getInteractActions();
		boolean valid = false;
		if (actions != null && actions.length > 0) {
			Arrays.sort(actions);
			valid = Arrays.binarySearch(actions, "Make") >= 0;
		}
		return valid;
	}

}

V1 Source

 

package com.liverare.api;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.osbot.rs07.api.def.ItemDefinition;
import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.script.API;

/**
 * 
 * @author LiveRare
 *
 */
public class AmountAPI extends API {

	public static final int BUTTON_COLOUR_SELECTED = 9009246;
	public static final int BUTTON_SPRITE_ENABLED_NOT_SELECTED = 1552;

	@Override
	public void initializeModule() {
	}
	
	public List<ItemOption> getItemButtons() {
		List<ItemOption> options = null;
		RS2Widget amountWidget = getAmountWidget();
		int rootId;
		if (amountWidget != null) {
			rootId = amountWidget.getRootId();
			options = widgets.getAll().stream()
					.filter(widget -> widget.getRootId() == rootId)
					.filter(AmountAPIOld::isWidgetMakeAction)
					.map(ItemOption::new)
					.collect(Collectors.toList());
		}
		return options;
	}
	
	public Map<Amount, RS2Widget> getQuantityButtons() {
		Map<Amount, RS2Widget> options = null;
		RS2Widget amountWidget = getAmountWidget();
		int rootId;
		if (amountWidget != null) {
			rootId = amountWidget.getRootId();
			options = widgets.getAll().stream()
					.filter(widget -> widget.getRootId() == rootId)
					.filter(AmountAPIOld::isMessageValid)
					.filter(Amount::isValid)
					.collect(Collectors.toMap(Amount::valueOf, a -> a, (a, b) -> a));
		}
		return options;
	}
	
	private RS2Widget getAmountWidget() {
		List<RS2Widget> l = widgets.containingText("How many do you wish to make?");
		return l != null && !l.isEmpty() ? l.get(0) : null;
	}

	private static boolean isMessageValid(RS2Widget child) {
		String message = child.getMessage();
		return message != null && !message.isEmpty();
	}
	
	private static int getItemId(RS2Widget parent) {
		final RS2Widget spellNameWidget = getItemIdWidget(parent);
		return spellNameWidget != null ? spellNameWidget.getItemId() : -1;
	}
	
	private static RS2Widget getItemIdWidget(RS2Widget parent) {
		return Stream.of(parent.getChildWidgets())
				.filter(AmountAPIOld::isItemIdValid)
				.findFirst()
				.orElse(null);
	}
	
	private static boolean isItemIdValid(RS2Widget child) {
		final int itemId = child.getItemId();
		return itemId >= 0;
	}
	
	private static boolean isWidgetMakeAction(RS2Widget child) {
		String[] actions = child.getInteractActions();
		boolean valid = false;
		if (actions != null && actions.length > 0) {
			Arrays.sort(actions);
			valid = Arrays.binarySearch(actions, "Make") >= 0;
		}
		return valid;
	}
	
	public class ItemOption {
		
		private final RS2Widget source;
		private final ItemDefinition itemDefinition;
		private final int itemId;
		private final String itemName;
		
		public ItemOption(RS2Widget source) {
			super();
			this.source = source;
			this.itemId = AmountAPIOld.getItemId(source);
			this.itemDefinition = ItemDefinition.forId(itemId);
			this.itemName = itemDefinition.getName();
		}
		
		@Override
		public String toString() {
			return String.format("[%s] %s", itemId, itemName);
		}
		
		public RS2Widget getSource() {
			return source;
		}
		
		public String getItemName() {
			return itemName;
		}
		
		public int getItemId() {
			return itemId;
		}
		
	}
	
	public enum Amount implements Predicate<RS2Widget> {
		ONE("1"),
		X("X"),
		ALL("All");
		
		private final String text;
		
		private Amount(String text) {
			this.text = text;
		}

		@Override
		public boolean test(RS2Widget child) {
			return stripFormatting(child.getMessage()).equals(text);
		}
		
		public static Amount valueOf(RS2Widget child) {
			Amount amount = null;
			for (Amount next : values()) {
				if (next.test(child)) {
					amount = next;
					break;
				}
			}
			return amount;
		}
		
		public static boolean isValid(RS2Widget child) {
			return valueOf(child) != null;
		}
	}
}
Edited by liverare
  • Like 4
Link to comment
Share on other sites

3 minutes ago, Muffins said:

H O R N Y

O

R

N

Y

<3

This was rushed a little and it's nearly 2am, so I've probably naffed a few things up, or there may be room for much greater improvement. I might continue working on it tomorrow. The main foundation for the stuff is there, and I need to fix my crafting script sometimes soon. But I'd like for there to be an official API first. :)

Link to comment
Share on other sites

1 hour ago, liverare said:

<3

This was rushed a little and it's nearly 2am, so I've probably naffed a few things up, or there may be room for much greater improvement. I might continue working on it tomorrow. The main foundation for the stuff is there, and I need to fix my crafting script sometimes soon. But I'd like for there to be an official API first. :)

We appreciate your work

  • Like 1
Link to comment
Share on other sites

I've updated it to 2.0.

  • No longer supporting the quantity buttons - this is wholly unnecessary because "all" should be selected beforehand and the only one that matters.
  • No more Amount.ItemOption, instead the buttons are stored to an <ItemDefinition, RS2Widget> map for simplicity's sake, which has meant more functions to quickly find the buttons you actually want (see the code example).
  • Like 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...