Jump to content

PricedItem class - Track items gained, and their value.


Deffiliate

Recommended Posts

This is a helpful class I made for tracking items gained, as well as loading their value from Zybez.

 

To create a new PricedItem, simply use: new PricedItem(String name, Client c) or new PricedItem(String name, int id, Client c) .

I create an array of these objects for all the items I want to track.

 

 

Then to track the items you've gained simply call the .update(Client c) method on each of the PricedItems.

 

Finally, you can use .getAmount() to get the total items gained, and getValue() to get the value of all items gained.

import org.osbot.script.rs2.Client;

public class PricedItem {
	private String name;
	private int lastCount = 0;
	private int amount = 0;
	private int price = 0;
	private int id = 0;


	public PricedItem(String name, Client c){
		this.name = name;
		if(c.getInventory().contains(name))
			lastCount = (int) c.getInventory().getAmount(name);
		if(name.contains("Clue scroll")||name.contains("Looting") )
			price = 25000;
		else if(name.contains("Tooth") ||name.contains("Loop")){
			int indexOf = name.indexOf("of")+2;
			String zybezKey = name.substring(0, indexOf).concat(" a key");
			price = PriceGrab.getInstance().getPrice(zybezKey, 2);	
		}
		else if(name.contains("arrow")){
			price = PriceGrab.getInstance().getPrice(name+"s", 2);	
		}
		else{
			price = PriceGrab.getInstance().getPrice(name, 2);	
		}
	}

	public PricedItem(String name, int id , Client c){
		this.name = name;
		this.setId(id);
		if(c.getInventory().contains(id))
			lastCount = (int) c.getInventory().getAmount(id);
		if(name.contains("Clue scroll")||name.contains("Tooth"))
			price = 100000;
		else{
			price = PriceGrab.getInstance().getPrice(name, 2);	
		}
	}

	public void update(Client c){
		if(!c.getBank().isOpen()){
			int increase = 0;
			if(id==0)
				increase =  (int) (c.getInventory().getAmount(name)- lastCount);
			else
				increase =  (int) (c.getInventory().getAmount(id)- lastCount);
			if(increase < 0)
				increase = 0;
			amount = amount + increase; 
		}
		if(id==0)
			lastCount = (int) c.getInventory().getAmount(name);
		else
			lastCount = (int) c.getInventory().getAmount(id);
	}

	public String getName(){
		return name;
	}
	public int getAmount(){
		return amount;
	}

	public int getPrice(){
		return price;
	}
	public int getValue(){
		return amount * price;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
}


You'll need this as well, used to grab prices from Zyb (This class was created by:@Boots):

package def.api;

import java.io.*;
import java.net.*;

public class PriceGrab {


    private static PriceGrab oneInstance;
    private URL zybez;
    private URLConnection urlConnection;
    private BufferedReader inputScan;
    private final String zybezUrl = "http://forums.zybez.net/runescape-2007-prices/api/item/";

    public static PriceGrab getInstance(){
        if(oneInstance == null){
            oneInstance = new PriceGrab();
        }
        return oneInstance;
    }

    public int getPrice(String itemName, int command){
        final String AVERAGE = "average",LOW= "recent_high", HIGH="recent_low";
        String item = format(itemName),extracted;
        int price = 0;
        openStream(item);
        extracted = retrieveData(item);
        switch (command){
            case 1:
                return parseInfo(extracted,LOW);

            case 2:
                return parseInfo(extracted,AVERAGE);

            case 3:
               return parseInfo(extracted,HIGH);
        }
        return price;
    }

    private String format(final String string){
        if(string.contains(" "))
            return string.replaceAll(" ", "+");
        else
            return string;
    }

    private void openStream(final String param){
        String appended = zybezUrl.concat(param);

                try {
                    zybez = new URL(appended);
                    urlConnection = zybez.openConnection();
                    urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
                }
                catch (MalformedURLException e) {
                    System.out.println("Web address formatted incorrectly, printing stack trace");
                    e.printStackTrace();
                }
                catch(IOException exception) {
                    System.out.println("Url connection has thrown an IOException, printing stack trace");
                    exception.printStackTrace();
                }
    }

    private String retrieveData(final String param){
        String output = null;
        try {
            openStream(param);
            inputScan = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
             output =  inputScan.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {inputScan.close();} catch (IOException e){e.printStackTrace();}
        }
        return output;
    }

    private int parseInfo(String extracted,String value){
        int start, end, price = 0;
        if(extracted.contains(value)){
            start = extracted.indexOf(value);
            end = extracted.indexOf(",",start);
            price = Integer.parseInt(extracted.substring(start, end).replaceFirst(".*?(\\d+).*", "$1"));
        }
        else
            System.out.println("Could not retrieve price");
        return price;
    }
}
Edited by Deffiliate
  • Like 1
Link to comment
Share on other sites

Eclipse: Ctrl + a & Ctrl + Shift + F
Intellij: Ctrl + Alt + L

Also u can mix it with:

package dependencies.api;

import dependencies.api.listeners.ATInventoryListener;
import org.osbot.script.Script;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author Xavier
 */
public class ATInventoryTracker implements Runnable {

    private Script script;
    private int[] ids, stacks;
    private int updateRate = 200;
    private final ArrayList<ATInventoryListener> listeners;
    private boolean running = true;
	private boolean ignoreName;

    public ATInventoryTracker(Script script) {
        super();
        this.script = script;
        listeners = new ArrayList<>();
    }

    public void start() {
        new Thread(this).start();
    }

    public void addListener(ATInventoryListener listener) {
        if (listener != null) {
            listeners.add(listener);
        }
    }

    public ArrayList<ATInventoryListener> getListeners() {
        return listeners;
    }

    @Override
    public void run() {
        do {
            try {
                if (ids == null || stacks == null) {
                    ids = script.client.getInterface(149).getChild(0).getInv();
                    stacks = script.client.getInterface(149).getChild(0).getInvStackSizes();
                } else {
                    int[] new_ids = script.client.getInterface(149).getChild(0).getInv();
                    int[] new_stacks = script.client.getInterface(149).getChild(0).getInvStackSizes();

                    //forgot about moved :p I just need added atm
                    for (int i = 0; i < ids.length; i++) {
                        if (new_ids[i] == ids[i] && new_stacks[i] == stacks[i]) {
                            continue;
                        }
                        if (new_ids[i] != ids[i]) {
                            if (ids[i] - 1 != -1) {
                                dispatchRemoved(i, ids[i] - 1, stacks[i]);
                            }
                            if (new_ids[i] - 1 != -1) {
                                dispatchAdded(i, new_ids[i] - 1, new_stacks[i]);
                            }
                        } else if (new_stacks[i] != stacks[i]) {
                            int difference = new_stacks[i] - stacks[i];
                            if (difference > 0) {
                                dispatchAdded(i, ids[i] - 1, Math.abs(difference));
                            } else {
                                dispatchRemoved(i, ids[i] - 1, Math.abs(difference));
                            }
                        }
                    }

                    ids = Arrays.copyOf(new_ids, new_ids.length);
                    stacks = Arrays.copyOf(new_stacks, new_stacks.length);

                    sleep(updateRate);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } while (running);
    }

    public void stop() {
        running = false;
    }
	
	public void setIgnoreName(boolean ignoreNames){
		ignoreName = ignoreNames;
	}

    private void sleep(int updateRate) {
        try {
            Thread.sleep(updateRate);
        } catch (InterruptedException ex) {
            //ignore
        }
    }

    private void dispatchRemoved(int slot, int id, int stack) {
        for (ATInventoryListener listener : listeners) {
            listener.itemRemoved(slot, id, stack);
        }
    }

    private void dispatchAdded(int slot, int id, int stack) {
        for (ATInventoryListener listener : listeners) {
            listener.itemAdded(slot, id, stack, ignoreName ? null : script.client.getInventory().getItemForId(id).getName());
        }
    }

    private void dispatchMoved(int fromSlot, int toSlot, int id, int stack) {
        for (ATInventoryListener listener : listeners) {
//			listener.itemMoved(fromSlot, toSlot, id, stack);
        }
    }

}

Link to comment
Share on other sites

 

Eclipse: Ctrl + a & Ctrl + Shift + F

Intellij: Ctrl + Alt + L

Also u can mix it with:

MY SEXINESS

I hate how Eclipse's default formatting looks, and IDK how to change it. So I never format in Eclipse, just correct indentation.

 

and very sexy class sir :3 You even went to the extreme of adding an itemMoved event.

Give us all the dependencies that go with this please. (Just realized i forgot one of the dependencies for my class)

Edited by Deffiliate
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...