TheScrub Posted November 25, 2013 Share Posted November 25, 2013 (edited) just updated! The Trade class Reveal hidden contents import java.awt.Point; import java.awt.Rectangle; import java.text.NumberFormat; import java.text.ParseException; import java.util.ArrayList; import java.util.List; import org.osbot.script.Script; import org.osbot.script.mouse.RectangleDestination; import org.osbot.script.rs2.model.Item; import org.osbot.script.rs2.ui.RS2InterfaceChild; /* * Please do not use this api for any money making purposes such as releasing premium scripts */ public class Trade { private final int START_WIDTH = 28; private final int START_HEIGHT = 82; // width+ height includes spacing lengths private final int WIDTH = 47; private final int HEIGHT = 32; private final int TRADE_SCREEN_ONE_PARENT = 335; private final int TRADE_SCREEN_ONE_CHILD_MY_ITEMS = 48; private final int TRADE_SCREEN_ONE_CHILD_THEIR_ITEMS = 50; private final int TRADE_SCREEN_ONE_DECLINE = 18; private final int TRADE_SCREEN_ONE_ACCEPT = 17; private final int TRADE_SCREEN_TWO_PARENT = 334; private final int TRADE_SCREEN_TWO_CHILD_MY_ITEMS = 37; private final int TRADE_SCREEN_TWO_CHILD_THEIR_ITEMS = 40; private final int TRADE_SCREEN_TWO_DECLINE = 21; private final int TRADE_SCREEN_TWO_ACCEPT = 20; private Script script; public Trade(Script script) { this.script = script; } public boolean tradeScreenOneOpen() { if (script.client.getInterface(TRADE_SCREEN_ONE_PARENT) != null) { RS2InterfaceChild rc = script.client.getInterface( TRADE_SCREEN_ONE_PARENT).getChild( TRADE_SCREEN_ONE_CHILD_MY_ITEMS); if (rc != null && rc.isVisible()) { return true; } } return false; } public Item[] getItemsFirstScreen(final int screen) { if (tradeScreenOneOpen()) { if (script.client.getInterface(TRADE_SCREEN_ONE_PARENT) != null) { RS2InterfaceChild rc = script.client.getInterface( TRADE_SCREEN_ONE_PARENT).getChild(screen); if (rc != null && rc.isVisible()) { return script.client.getInterface(TRADE_SCREEN_ONE_PARENT) .getItems(screen); } } } return null; } public int getItemSlot(Item item) { Item[] array = getItemsFirstScreen(TRADE_SCREEN_ONE_CHILD_MY_ITEMS); if (array != null && array.length > 0) { for (int i = 0; i < array.length; i++) { if (array[i] != null) { if (array[i].equals(item)) { return i; } } } } return -1; } public int getItemSlot(String item) { Item[] array = getItemsFirstScreen(TRADE_SCREEN_ONE_CHILD_MY_ITEMS); if (array != null && array.length > 0) { for (int i = 0; i < array.length; i++) { if (array[i] != null) { if (array[i].getName() != null) { if (array[i].getName().equalsIgnoreCase(item)) { return i; } } } } } return -1; } public int getItemSlot(int item) { Item[] array = getItemsFirstScreen(TRADE_SCREEN_ONE_CHILD_MY_ITEMS); if (array != null && array.length > 0) { for (int i = 0; i < array.length; i++) { if (array[i] != null) { if (array[i].getName() != null) { if (array[i].getId() == item) { return i; } } } } } return -1; } public boolean myItemsContains(int item) { if (tradeScreenOneOpen()) { Item[] array = getItemsFirstScreen(TRADE_SCREEN_ONE_CHILD_MY_ITEMS); if (array.length > 0) { for (Item i : array) { if (i != null) { if (i.getId() == item) { return true; } } } } } return false; } public Item myItemsGetItem(int item) { if (tradeScreenOneOpen()) { Item[] array = getItemsFirstScreen(TRADE_SCREEN_ONE_CHILD_MY_ITEMS); if (array.length > 0) { for (Item i : array) { if (i != null) { if (i.getId() == item) { return i; } } } } } return null; } private Point getPoint(int slot) { int column = slot / 4; int row = slot % 4; int x = (int) (START_WIDTH + (row * WIDTH)); int y = (int) START_HEIGHT + (column * HEIGHT); return new Point(x, y); } public Rectangle getRectangle(int slot) { Point p = getPoint(slot); return new Rectangle(p.x, p.y, 30, 27); } public boolean interact(Rectangle rec, String action) throws InterruptedException { if (rec != null && action != null && !action.isEmpty()) { return script.selectOption(null, new RectangleDestination(rec), action); } return false; } public boolean tradeScreenTwoOpen() { if (script.client.getInterface(TRADE_SCREEN_TWO_PARENT) != null) { RS2InterfaceChild rc = script.client.getInterface( TRADE_SCREEN_TWO_PARENT).getChild( TRADE_SCREEN_TWO_CHILD_MY_ITEMS); if (rc != null && rc.isVisible()) { return true; } } return false; } private int parseQuantity(String text) throws ParseException { if (text == null) { return -1; } if (text.contains("(") && text.contains(")")) { text = text.substring(text.indexOf('(') + 1, text.indexOf(')')); NumberFormat.getIntegerInstance(java.util.Locale.US).parse(text) .intValue(); } return NumberFormat.getIntegerInstance(java.util.Locale.US).parse(text) .intValue(); } public List<TradeItem> getItemsSecondaryScreen(String text) throws ParseException { ArrayList<TradeItem> list = new ArrayList<TradeItem>(); if (text == null || !text.contains("<br>")) { return list; } String items[] = text.split("<br>"); for (String s : items) { String[] itemData = s.split("<col=[0-9A-Fa-f]{6}>"); if (itemData.length == 2) { list.add(new TradeItem(itemData[1])); } else if (itemData.length == 4) { list.add(new TradeItem(itemData[1], parseQuantity(itemData[3]))); } else { try { throw new Exception("Sorry please check the item"); } catch (Exception e) { e.printStackTrace(); } } } return list; } public String getSecondWindowText(int screen) { if (tradeScreenTwoOpen()) { return script.client.getInterface(TRADE_SCREEN_TWO_PARENT) .getChild(screen).getMessage(); } return null; } public boolean acceptTrade() throws InterruptedException { if (tradeScreenOneOpen()) { return interact(script.client.getInterface(TRADE_SCREEN_ONE_PARENT) .getChild(TRADE_SCREEN_ONE_ACCEPT).getRectangle(), "Accept Trade"); } if (tradeScreenTwoOpen()) { return interact(script.client.getInterface(TRADE_SCREEN_TWO_PARENT) .getChild(TRADE_SCREEN_TWO_ACCEPT).getRectangle(), "Accept Trade"); } return false; } public boolean declineTrade() throws InterruptedException { if (tradeScreenOneOpen()) { return script.client.getInterface(TRADE_SCREEN_ONE_PARENT) .getChild(TRADE_SCREEN_ONE_DECLINE) .interact("Decline Trade"); } if (tradeScreenTwoOpen()) { return script.client.getInterface(TRADE_SCREEN_TWO_PARENT) .getChild(TRADE_SCREEN_TWO_DECLINE).interact("Decline"); } return false; } } TradeItem Class: Reveal hidden contents public class TradeItem { private String name; private boolean stackable; private int amount; public TradeItem(String name) { this.name = name; this.stackable = false; this.amount = 1; } public TradeItem(String name, int amount) { this.name = name; this.amount = amount; this.stackable = true; } public String getName() { return this.name; } public int getAmount() { return this.amount; } public boolean isStackable() { return this.stackable; } } Example useage; Reveal hidden contents Will determine what trade items are in the text on second screen List<TradeItem> list = getItemsSecondaryScreen(getSecondWindowText(TRADE_SCREEN_TWO_CHILD_THEIR_ITEMS)); TradeItem[] array = list.toArray(new TradeItem[list.size()]); if (array.length>0){ if (array[0].getName.equals("Lobster")){ // do action } } Edited April 23, 2014 by TheScrub 1 Link to comment Share on other sites More sharing options...
Kittens Posted November 25, 2013 Share Posted November 25, 2013 (edited) amazing ill use this in the future when i learn java mate but im not sure if i'd ever try writing a merch script it seems quite complicated haha Edited November 25, 2013 by Gh0st Link to comment Share on other sites More sharing options...
TheScrub Posted November 25, 2013 Author Share Posted November 25, 2013 (edited) -Edit spammed threads due to lag my commenting got deleted not rewriting the commenting just read through it Example usage should be there soon Needs more methods like custom interactions which will be released later Edited November 25, 2013 by TheScrub Link to comment Share on other sites More sharing options...
Mr Asshole Posted November 25, 2013 Share Posted November 25, 2013 Stark is going to need this o.o Link to comment Share on other sites More sharing options...
TheScrub Posted November 25, 2013 Author Share Posted November 25, 2013 (edited) On 11/25/2013 at 7:50 AM, Mr Asshole said: Stark is going to need this o.o if you read the terms and conditions you may not use my code for any premium scripts.. do not be talking smack mr asshole Edited November 25, 2013 by TheScrub Link to comment Share on other sites More sharing options...
kellyann Posted November 25, 2013 Share Posted November 25, 2013 Looks awesome. I dont know code... but looks great to me :P :3 Link to comment Share on other sites More sharing options...
TheScrub Posted November 25, 2013 Author Share Posted November 25, 2013 Just added some new methods Accepting/declining trades both screens... Link to comment Share on other sites More sharing options...
XavierM Posted November 25, 2013 Share Posted November 25, 2013 On 11/25/2013 at 7:55 AM, TheScrub said: On 11/25/2013 at 7:50 AM, Mr Asshole said: Stark is going to need this o.o if you read the terms and conditions you may not use my code for any premium scripts.. do not be talking smack mr asshole Problem is that you won't know if anyone uses it or not xd Anyway, some notes: public static int mytradescreen = 48; use correct java naming conventions, if you are coding for yourself I wuldn't give a fuck, but if you are trying to write modular code api/library you need to use conventions or people will have a hard time understanding the code. private Script script = Context.script; Don't use static for the sake of god. You are supposed to use oop with osbot, if you use static the vars will be "shared" to all tabs withing the client. You should init this class with a Script argument instead. public Item[] getItems(int screen) throws InterruptedException { ArrayList<Item> array = new ArrayList<Item>(); if (tradeScreenOpen()) { Item[] itemArray = Context.script.client.getInterface(335) .getItems(screen); for (Item i : itemArray) { if (i != null) array.add(i); } } return array.toArray(new Item[array.size()]); } ????? getFirstScreenItemByInteger *ById maybe public static int ourSecondScreen = 37; public static int otherPersonSecondScreen = 40; There are 4 more childs for second screen messages Only good thing worth mnetioning is the getItemsSecondaryScreen method that seems very clever Link to comment Share on other sites More sharing options...
TheScrub Posted November 25, 2013 Author Share Posted November 25, 2013 On 11/25/2013 at 11:23 AM, XavierM said: On 11/25/2013 at 7:55 AM, TheScrub said: On 11/25/2013 at 7:50 AM, Mr Asshole said: Stark is going to need this o.o if you read the terms and conditions you may not use my code for any premium scripts.. do not be talking smack mr asshole Problem is that you won't know if anyone uses it or not xd Anyway, some notes: public static int mytradescreen = 48; use correct java naming conventions, if you are coding for yourself I wuldn't give a fuck, but if you are trying to write modular code api/library you need to use conventions or people will have a hard time understanding the code. private Script script = Context.script; Don't use static for the sake of god. You are supposed to use oop with osbot, if you use static the vars will be "shared" to all tabs withing the client. You should init this class with a Script argument instead. public Item[] getItems(int screen) throws InterruptedException { ArrayList<Item> array = new ArrayList<Item>(); if (tradeScreenOpen()) { Item[] itemArray = Context.script.client.getInterface(335) .getItems(screen); for (Item i : itemArray) { if (i != null) array.add(i); } } return array.toArray(new Item[array.size()]); } ????? getFirstScreenItemByInteger *ById maybe public static int ourSecondScreen = 37; public static int otherPersonSecondScreen = 40; There are 4 more childs for second screen messages Only good thing worth mnetioning is the getItemsSecondaryScreen method that seems very clever thanks for the constructive feedback instead of the usual nice good work or i might use this Link to comment Share on other sites More sharing options...
Swizzbeat Posted November 25, 2013 Share Posted November 25, 2013 Thanks a lot! Link to comment Share on other sites More sharing options...
Stark Posted November 25, 2013 Share Posted November 25, 2013 On 11/25/2013 at 7:50 AM, Mr Asshole said: Stark is going to need this o.o Except my script has been complete for almost a week now, before this post was made. It's been pending in SDN upload requests for 5 days. 1 Link to comment Share on other sites More sharing options...
TheScrub Posted November 26, 2013 Author Share Posted November 26, 2013 On 11/25/2013 at 9:16 PM, Stark said: On 11/25/2013 at 7:50 AM, Mr Asshole said: Stark is going to need this o.o Except my script has been complete for almost a week now, before this post was made. It's been pending in SDN upload requests for 5 days. Please no Trolling/Flaming on thread both of you two idc about this crap i'm here to code and to improve my java knowledge! (I know stark your defending yourself but just don't reply to him look at his name...) Link to comment Share on other sites More sharing options...
zScripz Posted November 28, 2013 Share Posted November 28, 2013 Hi scrub, you might be interested in this method: http://osbot.org/api/org/osbot/script/MethodProvider.html#stripFomatting%28java.lang.String%29 This is what I used in my personal API. Link to comment Share on other sites More sharing options...
TheScrub Posted November 29, 2013 Author Share Posted November 29, 2013 On 11/28/2013 at 11:21 PM, zScripz said: Hi scrub, you might be interested in this method: http://osbot.org/api/org/osbot/script/MethodProvider.html#stripFomatting%28java.lang.String%29 This is what I used in my personal API. lol gg me... but i guess the good thing about mine is it's not api dependent to osbot Link to comment Share on other sites More sharing options...
zScripz Posted November 30, 2013 Share Posted November 30, 2013 On 11/29/2013 at 2:14 AM, TheScrub said: On 11/28/2013 at 11:21 PM, zScripz said: Hi scrub, you might be interested in this method: http://osbot.org/api/org/osbot/script/MethodProvider.html#stripFomatting%28java.lang.String%29 This is what I used in my personal API. lol gg me... but i guess the good thing about mine is it's not api dependent to osbot Lol yea, that is valuable.. and knowledge is the most important tbh :P Link to comment Share on other sites More sharing options...