Advertisement
PayPalMeRSGP

GrandExchangeOperations.java

Mar 8th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 21.17 KB | None | 0 0
  1. package GrandExchange_Util;
  2.  
  3. import org.osbot.rs07.api.GrandExchange;
  4. import org.osbot.rs07.api.Inventory;
  5. import org.osbot.rs07.api.Mouse;
  6. import org.osbot.rs07.api.model.NPC;
  7. import org.osbot.rs07.api.ui.RS2Widget;
  8. import org.osbot.rs07.input.mouse.WidgetDestination;
  9. import org.osbot.rs07.script.MethodProvider;
  10. import org.osbot.rs07.script.Script;
  11. import org.osbot.rs07.utility.ConditionalSleep;
  12.  
  13. public class GrandExchangeOperations {
  14.  
  15.     /*
  16.     widget ids for GE boxes
  17.     [0] = root, [1] = child+, [2] = child++ for buy item, [3] child++ for sell item
  18.      */
  19.     private static final int[] GE_BOX1 = {465, 7, 26, 27};
  20.     private static final int[] GE_BOX2 = {465, 8, 26, 27};
  21.     private static final int[] GE_BOX3 = {465, 9, 26, 27};
  22.     private static final int[] GE_BOX4 = {465, 10, 26, 27};
  23.     private static final int[] GE_BOX5 = {465, 11, 26, 27};
  24.     private static final int[] GE_BOX6 = {465, 12, 26, 27};
  25.     private static final int[] GE_BOX7 = {465, 13, 26, 27};
  26.     private static final int[] GE_BOX8 = {465, 14, 26, 27};
  27.     private enum GE_OPERATIONS {BUY, SELL, COLLECT, ABORT};
  28.  
  29.     private static final int[] NUMBER_ENTRY = {162, 35};
  30.     private static final int[] GE_ITEM_ENTRY = {162, 36};
  31.     private static final int[] GE_ITEM_SELECTION = {162, 41};
  32.     private static final int[] SET_ITEM_QUANTITY = {465, 24, 7};
  33.     private static final int[] SET_ITEM_PRICE = {465, 24, 12};
  34.     private static final int[] GE_CONFIRM = {465, 27};
  35.     private static final int[] ITEM_QUANTITY = {465, 24, 32};
  36.     private static final int[] ITEM_PRICE = {465, 24, 39};
  37.  
  38.     private static final int[] COLLECT_ITEM_LEFT_BOX = {465, 23, 2};
  39.     private static final int[] COLLECT_ITEM_RIGHT_BOX = {465, 23, 3};
  40.     class GrandExchangeCollectData {
  41.         int leftBoxItemID = -1;
  42.         int leftBoxQuantity = -1;
  43.         int rightBoxItemID = -1;
  44.         int rightBoxQuantity = -1;
  45.  
  46.         @Override
  47.         public String toString() {
  48.             return "left Item: " + leftBoxItemID + " left quantity: " + leftBoxQuantity + " right Item " + rightBoxItemID + " right box quantity: " + rightBoxQuantity;
  49.  
  50.         }
  51.     }
  52.  
  53.     private static final int[] COLLECT_ALL = {465, 6, 0};
  54.  
  55.     private Script hostScriptReference;
  56.  
  57.     public GrandExchangeOperations(Script hostScriptReference) {
  58.         this.hostScriptReference = hostScriptReference;
  59.     }
  60.  
  61.     public GrandExchangeOffer buyItem(int itemID, String searchTerm, int quantity, int price) throws InterruptedException {
  62.         hostScriptReference.getWidgets().closeOpenInterface();
  63.         GrandExchange ge = hostScriptReference.getGrandExchange();
  64.         if(openGE()){
  65.             //interact with next available box
  66.             GrandExchange.Box box = findFreeGEBox();
  67.             RS2Widget geBoxWidget = getGEBoxWidget(GE_OPERATIONS.BUY, box);
  68.             if(geBoxWidget != null && geBoxWidget.isVisible()){
  69.                 WidgetDestination destination = new WidgetDestination(hostScriptReference.getBot(), geBoxWidget);
  70.                 if(hostScriptReference.getMouse().click(destination)){
  71.                     //wait until buy screen opens
  72.                     new ConditionalSleep(1500){
  73.                         @Override
  74.                         public boolean condition() throws InterruptedException {
  75.                             return ge.isBuyOfferOpen() && isGEItemEntryOpen();
  76.                         }
  77.                     }.sleep();
  78.  
  79.                     //enter in search term
  80.                     hostScriptReference.getKeyboard().typeString(searchTerm, true);
  81.                     MethodProvider.sleep(500);
  82.  
  83.                     //get all queried items off of search term and find correct widget to click
  84.                     RS2Widget geQueriedItemWidgets = hostScriptReference.getWidgets().get(GE_ITEM_SELECTION[0], GE_ITEM_SELECTION[1]);
  85.                     if(geQueriedItemWidgets != null && geQueriedItemWidgets.isVisible()){
  86.                         //the index in geItemSelectionIDs that matches the itemID to buy is the third level widget ID of the correct widget
  87.                         int[] geItemSelectionIDs = geQueriedItemWidgets.getInv();
  88.                         int thirdLvlID = -1;
  89.                         for(int i = 0; i < geItemSelectionIDs.length; i++){
  90.                             if(geItemSelectionIDs[i] == itemID){
  91.                                 thirdLvlID = i;
  92.                                 break;
  93.                             }
  94.                         }
  95.                         //interact with the found widget
  96.                         if(thirdLvlID != -1){
  97.                             RS2Widget targetItemWidget = hostScriptReference.getWidgets().get(GE_ITEM_SELECTION[0], GE_ITEM_SELECTION[1], thirdLvlID);
  98.                             if(targetItemWidget != null && targetItemWidget.isVisible()){
  99.                                 destination = new WidgetDestination(hostScriptReference.getBot(), targetItemWidget);
  100.                                 hostScriptReference.getMouse().click(destination);
  101.                                 MethodProvider.sleep(500);
  102.  
  103.                                 //set quantity and price parameter
  104.                                 if(setItemQuantity(quantity) && setItemPrice(price)){
  105.                                     if(confirmOffer()){
  106.                                         return new GrandExchangeOffer(hostScriptReference,true, box, itemID, quantity);
  107.                                     }
  108.                                 }
  109.                             }
  110.                         }
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.         else{
  116.             hostScriptReference.log("no available box");
  117.         }
  118.  
  119.         return null;
  120.     }
  121.  
  122.     public GrandExchangeOffer sellItem(int itemID, int quantity, int price) throws InterruptedException {
  123.         hostScriptReference.getWidgets().closeOpenInterface();
  124.         GrandExchange ge = hostScriptReference.getGrandExchange();
  125.         if(hostScriptReference.getInventory().contains(itemID)){
  126.             if(openGE()){
  127.                 //find a free box to use and interact with the sell widget
  128.                 GrandExchange.Box box = findFreeGEBox();
  129.                 RS2Widget geBoxWidget = getGEBoxWidget(GE_OPERATIONS.SELL, box);
  130.                 if(geBoxWidget != null && geBoxWidget.isVisible()){
  131.                     WidgetDestination destination = new WidgetDestination(hostScriptReference.getBot(), geBoxWidget);
  132.                     if(hostScriptReference.getMouse().click(destination)){
  133.                         //wait until sell screen opens
  134.                         new ConditionalSleep(1000){
  135.                             @Override
  136.                             public boolean condition() throws InterruptedException {
  137.                                 return ge.isSellOfferOpen();
  138.                             }
  139.                         }.sleep();
  140.  
  141.                         Inventory inv = hostScriptReference.getInventory();
  142.                         if(inv.interact("Offer", itemID)){
  143.                             MethodProvider.sleep(750);
  144.                             if(setItemQuantity(quantity) && setItemPrice(price)){
  145.                                 if(confirmOffer()){
  146.                                     return new GrandExchangeOffer(hostScriptReference,true, box, itemID, quantity);
  147.                                 }
  148.                             }
  149.                         }
  150.                     }
  151.                 }
  152.             }
  153.         }
  154.         return null;
  155.     }
  156.  
  157.     public int[] priceCheckItem(int itemID, String searchTerm, int estimatedHighPrice) throws InterruptedException {
  158.         int[] margin = new int[2];
  159.         GrandExchange ge = hostScriptReference.getGrandExchange();
  160.         GrandExchangeOffer buy = buyItem(itemID, searchTerm, 1, estimatedHighPrice);
  161.         new ConditionalSleep(3600){
  162.             @Override
  163.             public boolean condition() throws InterruptedException {
  164.                 if(buy == null){
  165.                     return false;
  166.                 }
  167.                 return buy.getBoxStatus() == GrandExchange.Status.FINISHED_BUY;
  168.             }
  169.         }.sleep();
  170.  
  171.         if(buy != null){
  172.             margin[0] = ge.getAmountSpent(buy.getSelectedBox());
  173.             collectFromBox(buy.getSelectedBox());
  174.             MethodProvider.sleep(1000);
  175.             GrandExchangeOffer sell = sellItem(itemID, 1, 1);
  176.             new ConditionalSleep(3600){
  177.                 @Override
  178.                 public boolean condition() throws InterruptedException {
  179.                     if(sell == null){
  180.                         return false;
  181.                     }
  182.                     return sell.getBoxStatus() == GrandExchange.Status.FINISHED_SALE;
  183.                 }
  184.             }.sleep();
  185.             if(sell != null){
  186.                 GrandExchangeCollectData collectData = collectFromBox(sell.getSelectedBox());
  187.                 if(collectData != null){
  188.                     hostScriptReference.log(collectData.toString());
  189.                     margin[1] = collectData.leftBoxQuantity;
  190.                 }
  191.             }
  192.  
  193.         }
  194.  
  195.         return margin;
  196.     }
  197.  
  198.     public GrandExchangeCollectData collectFromBox(GrandExchange.Box box) throws InterruptedException {
  199.         RS2Widget geBoxWidget = getGEBoxWidget(GE_OPERATIONS.COLLECT, box);
  200.         GrandExchange ge = hostScriptReference.getGrandExchange();
  201.         if(openGE()){
  202.             if(ge.getStatus(box) == GrandExchange.Status.EMPTY){
  203.                 return null;
  204.             }
  205.             if(geBoxWidget != null && geBoxWidget.isVisible()){
  206.                 WidgetDestination destination = new WidgetDestination(hostScriptReference.getBot(), geBoxWidget);
  207.                 Mouse m = hostScriptReference.getMouse();
  208.                 if(m.click(destination)){
  209.                     new ConditionalSleep(1000){
  210.                         @Override
  211.                         public boolean condition() throws InterruptedException {
  212.                             return ge.isOfferScreenOpen();
  213.                         }
  214.                     }.sleep();
  215.                     RS2Widget collect = hostScriptReference.getWidgets().get(COLLECT_ITEM_LEFT_BOX[0], COLLECT_ITEM_LEFT_BOX[1], COLLECT_ITEM_LEFT_BOX[2]);
  216.                     GrandExchangeCollectData collectData = new GrandExchangeCollectData(); //fill up collectData as we collect left and right boxes
  217.                     collectData.leftBoxItemID = collect.getItemId();
  218.                     collectData.leftBoxQuantity = collect.getItemAmount();
  219.                     destination = new WidgetDestination(hostScriptReference.getBot(), collect);
  220.                     if(collect.isVisible()){
  221.                         if(m.click(destination)){
  222.                             MethodProvider.sleep(1000);
  223.                             //detect if need to collect both right and left boxes. If after collecting the left box and the offer screen is still up, then the right box needs to be collected
  224.                             if(ge.isOfferScreenOpen()){
  225.                                 MethodProvider.sleep(1000);
  226.                                 collect = hostScriptReference.getWidgets().get(COLLECT_ITEM_RIGHT_BOX[0], COLLECT_ITEM_RIGHT_BOX[1], COLLECT_ITEM_RIGHT_BOX[2]);
  227.                                 if(collect != null && collect.isVisible()){
  228.                                     collectData.rightBoxItemID = collect.getItemId();
  229.                                     collectData.rightBoxQuantity = collect.getItemAmount();
  230.                                     destination = new WidgetDestination(hostScriptReference.getBot(), collect);
  231.                                     if(m.click(destination)){
  232.                                         return collectData;
  233.                                     }
  234.                                 }
  235.                             }
  236.                             //else if ge returns to the main screen, then the collection process is complete
  237.                             else if(ge.isOpen()){
  238.                                 MethodProvider.sleep(1000);
  239.                                 return collectData;
  240.                             }
  241.                         }
  242.                     }
  243.                 }
  244.             }
  245.         }
  246.         return null;
  247.     }
  248.  
  249.     public boolean collectAll() throws InterruptedException {
  250.         if(openGE()){
  251.             RS2Widget collectAll = hostScriptReference.getWidgets().get(COLLECT_ALL[0], COLLECT_ALL[1], COLLECT_ALL[2]);
  252.             if(collectAll.isVisible()){
  253.                 return collectAll.interact("Collect to inventory");
  254.             }
  255.         }
  256.  
  257.         return false;
  258.     }
  259.  
  260.     public boolean abortOffer(GrandExchange.Box box){
  261.         GrandExchange ge = hostScriptReference.getGrandExchange();
  262.         RS2Widget boxWidget = getGEBoxWidget(GE_OPERATIONS.ABORT, box);
  263.         if(boxWidget != null && boxWidget.isVisible()){
  264.             boolean hasAbortOption = false;
  265.             String[] actions = boxWidget.getInteractActions();
  266.             for(String s: actions){
  267.                 if(s.contains("Abort")){
  268.                     hasAbortOption = true;
  269.                     break;
  270.                 }
  271.             }
  272.             if(hasAbortOption){
  273.                 return boxWidget.interact("Abort");
  274.             }
  275.         }
  276.         return false;
  277.     }
  278.  
  279.     private boolean openGE() throws InterruptedException {
  280.         GrandExchange ge = hostScriptReference.getGrandExchange();
  281.         if(!ge.isOpen()){
  282.             NPC grandExchangeClerk = hostScriptReference.getNpcs().closest("Grand Exchange Clerk");
  283.             if(grandExchangeClerk != null){
  284.                 boolean didInteraction = grandExchangeClerk.interact("Exchange");
  285.                 final boolean[] isOpen = {false};
  286.                 new ConditionalSleep(1000){
  287.                     @Override
  288.                     public boolean condition() throws InterruptedException {
  289.                         isOpen[0] = didInteraction && ge.isOpen();
  290.                         return isOpen[0];
  291.                     }
  292.                 }.sleep();
  293.                 return isOpen[0];
  294.             }
  295.         }
  296.         return true;
  297.     }
  298.  
  299.     private RS2Widget getGEBoxWidget(GE_OPERATIONS operation, GrandExchange.Box box){
  300.         RS2Widget geBoxWidget = null;
  301.  
  302.         //if collecting a 3rd lvl is not needed
  303.         if(operation == GE_OPERATIONS.COLLECT){
  304.             switch(box){
  305.                 case BOX_1:
  306.                     geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX1[0], GE_BOX1[1]);
  307.                     break;
  308.                 case BOX_2:
  309.                     geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX2[0], GE_BOX2[1]);
  310.                     break;
  311.                 case BOX_3:
  312.                     geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX3[0], GE_BOX3[1]);
  313.                     break;
  314.                 case BOX_4:
  315.                     geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX4[0], GE_BOX4[1]);
  316.                     break;
  317.                 case BOX_5:
  318.                     geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX5[0], GE_BOX5[1]);
  319.                     break;
  320.                 case BOX_6:
  321.                     geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX6[0], GE_BOX6[1]);
  322.                     break;
  323.                 case BOX_7:
  324.                     geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX7[0], GE_BOX7[1]);
  325.                     break;
  326.                 case BOX_8:
  327.                     geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX8[0], GE_BOX8[1]);
  328.                     break;
  329.  
  330.             }
  331.             return geBoxWidget;
  332.         }
  333.         else if(operation == GE_OPERATIONS.ABORT){
  334.             int thirdLvlIDForCancel = 2;
  335.             switch(box){
  336.                 case BOX_1:
  337.                     geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX1[0], GE_BOX1[1], thirdLvlIDForCancel);
  338.                     break;
  339.                 case BOX_2:
  340.                     geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX2[0], GE_BOX2[1], thirdLvlIDForCancel);
  341.                     break;
  342.                 case BOX_3:
  343.                     geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX3[0], GE_BOX3[1], thirdLvlIDForCancel);
  344.                     break;
  345.                 case BOX_4:
  346.                     geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX4[0], GE_BOX4[1], thirdLvlIDForCancel);
  347.                     break;
  348.                 case BOX_5:
  349.                     geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX5[0], GE_BOX5[1], thirdLvlIDForCancel);
  350.                     break;
  351.                 case BOX_6:
  352.                     geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX6[0], GE_BOX6[1], thirdLvlIDForCancel);
  353.                     break;
  354.                 case BOX_7:
  355.                     geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX7[0], GE_BOX7[1], thirdLvlIDForCancel);
  356.                     break;
  357.                 case BOX_8:
  358.                     geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX8[0], GE_BOX8[1], thirdLvlIDForCancel);
  359.                     break;
  360.  
  361.             }
  362.             return geBoxWidget;
  363.         }
  364.  
  365.         //if buying or selling, it is needed
  366.         int thirdLvlID = -1;
  367.         if(operation == GE_OPERATIONS.BUY){
  368.             thirdLvlID = GE_BOX1[2];
  369.         } else if(operation == GE_OPERATIONS.SELL) {
  370.             thirdLvlID = GE_BOX1[3];
  371.         }
  372.  
  373.         switch(box){
  374.             case BOX_1:
  375.                 geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX1[0], GE_BOX1[1], thirdLvlID);
  376.                 break;
  377.             case BOX_2:
  378.                 geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX2[0], GE_BOX2[1], thirdLvlID);
  379.                 break;
  380.             case BOX_3:
  381.                 geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX3[0], GE_BOX3[1], thirdLvlID);
  382.                 break;
  383.             case BOX_4:
  384.                 geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX4[0], GE_BOX4[1], thirdLvlID);
  385.                 break;
  386.             case BOX_5:
  387.                 geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX5[0], GE_BOX5[1], thirdLvlID);
  388.                 break;
  389.             case BOX_6:
  390.                 geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX6[0], GE_BOX6[1], thirdLvlID);
  391.                 break;
  392.             case BOX_7:
  393.                 geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX7[0], GE_BOX7[1], thirdLvlID);
  394.                 break;
  395.             case BOX_8:
  396.                 geBoxWidget = hostScriptReference.getWidgets().get(GE_BOX8[0], GE_BOX8[1], thirdLvlID);
  397.                 break;
  398.  
  399.         }
  400.         return geBoxWidget;
  401.     }
  402.  
  403.     private boolean setItemQuantity(int quantity) throws InterruptedException {
  404.         RS2Widget quantitySelection = hostScriptReference.getWidgets().get(SET_ITEM_QUANTITY[0], SET_ITEM_QUANTITY[1], SET_ITEM_QUANTITY[2]);
  405.         if(quantitySelection != null && quantitySelection.isVisible()){
  406.             if(quantitySelection.interact("Enter quantity")){
  407.                 new ConditionalSleep(1000){
  408.                     @Override
  409.                     public boolean condition() throws InterruptedException {
  410.                         return isNumberEntryOpen();
  411.                     }
  412.                 }.sleep();
  413.                 hostScriptReference.getKeyboard().typeString(String.valueOf(quantity));
  414.                 MethodProvider.sleep(500);
  415.                 return true;
  416.             }
  417.         }
  418.  
  419.         return false;
  420.     }
  421.  
  422.     private boolean setItemPrice(int price) throws InterruptedException {
  423.         RS2Widget priceSelection = hostScriptReference.getWidgets().get(SET_ITEM_PRICE[0], SET_ITEM_PRICE[1], SET_ITEM_PRICE[2]);
  424.         if(priceSelection != null && priceSelection.isVisible())
  425.             if(priceSelection.interact("Enter price")){
  426.                 new ConditionalSleep(1000){
  427.                     @Override
  428.                     public boolean condition() throws InterruptedException {
  429.                         return isNumberEntryOpen();
  430.                     }
  431.                 }.sleep();
  432.                 hostScriptReference.getKeyboard().typeString(String.valueOf(price));
  433.                 MethodProvider.sleep(500);
  434.                 return true;
  435.             }
  436.         return false;
  437.     }
  438.  
  439.     private boolean confirmOffer() {
  440.         RS2Widget confirm = hostScriptReference.getWidgets().get(GE_CONFIRM[0], GE_CONFIRM[1]);
  441.         return confirm != null && confirm.isVisible() && confirm.interact("Confirm");
  442.     }
  443.  
  444.     private boolean isNumberEntryOpen(){
  445.         RS2Widget numberEntry = hostScriptReference.getWidgets().get(NUMBER_ENTRY[0], NUMBER_ENTRY[1]);
  446.         return numberEntry.isVisible();
  447.     }
  448.  
  449.     private boolean isGEItemEntryOpen(){
  450.         RS2Widget itemEntry = hostScriptReference.getWidgets().get(GE_ITEM_ENTRY[0], GE_ITEM_ENTRY[1]);
  451.         return itemEntry.isVisible();
  452.     }
  453.  
  454.     private GrandExchange.Box findFreeGEBox(){
  455.         GrandExchange ge = hostScriptReference.getGrandExchange();
  456.         for (GrandExchange.Box box : GrandExchange.Box.values()) {
  457.             if(ge.getStatus(box) == GrandExchange.Status.EMPTY){
  458.                 return box;
  459.             }
  460.         }
  461.         return null; //indicates no boxes are empty
  462.     }
  463. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement