Advertisement
joedezzy1

Trader

Mar 12th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.93 KB | None | 0 0
  1. package scripts.autoMerchanter;
  2.  
  3. import org.tribot.api.Clicking;
  4. import org.tribot.api.General;
  5. import org.tribot.api.Timing;
  6. import org.tribot.api.input.Keyboard;
  7. import org.tribot.api.types.generic.Condition;
  8. import org.tribot.api2007.Interfaces;
  9. import org.tribot.api2007.Inventory;
  10. import org.tribot.api2007.types.RSInterface;
  11. import org.tribot.api2007.types.RSInterfaceChild;
  12. import org.tribot.api2007.types.RSInterfaceComponent;
  13.  
  14. public class Trader {
  15.    
  16.     private Check values;
  17.     private Clicker clicker;
  18.    
  19.     public Trader(Check values) {
  20.     this.values = values;
  21.     this.clicker = new Clicker(values);
  22.     }
  23.  
  24.     public void startTrade() {     
  25.     //*
  26.     /* Initializes trade with
  27.      *  Person who traded
  28.      */
  29.     RSInterface chat = Interfaces.get(137, 4);     
  30.     if (chat != null) {        
  31.         if (chat.click()) {            
  32.         Timing.waitCondition(new Condition() {
  33.             @Override
  34.             public boolean active() {
  35.             return values.inTrade();
  36.             }                  
  37.         }, General.random(1200, 1800));
  38.         }  
  39.     }
  40.     }
  41.    
  42.     public void handleFirstTrade(){
  43.     //*
  44.     /* Checks if we've offered an item, if not it offers
  45.      *  Than determines wether or not we can accept the trade
  46.      */
  47.         if (!offeredItem()) {
  48.             General.println("Offering item");
  49.             offerItem();
  50.         }
  51.         else if (!haveAnOffer()) {
  52.         if (!Timing.waitCondition(new Condition() {
  53.         @Override
  54.         public boolean active() {
  55.             return haveAnOffer();
  56.         }
  57.         }, General.random(8000, 12000))) {
  58.         clicker.handleTradeScreen(false);
  59.         }
  60.     }
  61.         else if (canAccept()) {                
  62.             clicker.handleTradeScreen(true);                   
  63.         }              
  64.         else {
  65.             clicker.handleTradeScreen(false);              
  66.         }                      
  67.     }
  68.  
  69.     private void offerItem() {
  70.     //*
  71.     /* Offers item for both buying and selling,
  72.      *  Depending on which state the bot is in
  73.      */
  74.     switch(values.status){
  75.         case "selling":
  76.         if (Clicking.click("Offer", Inventory.find(values.itemName))) {
  77.             Timing.waitCondition(new Condition() {
  78.                 @Override
  79.                 public boolean active() {
  80.                     return offeredItem();
  81.                 }
  82.             }, General.random(3300, 4300));
  83.         }
  84.         break;
  85.         case "buying":
  86.         if (Clicking.click("Offer-X", Inventory.find("Coins"))) {
  87.             if (Timing.waitCondition(new Condition() {
  88.                 @Override
  89.                 public boolean active() {
  90.                     return Interfaces.isInterfaceValid(137);
  91.                 }
  92.             }, General.random(3300, 4300))) {                  
  93.                 Keyboard.typeSend(Integer.toString(values.buyingPrice));                   
  94.             }
  95.         }
  96.         break;
  97.         }
  98.     }
  99.  
  100.     private boolean offeredItem() {
  101.         //*
  102.         /* Checks through trade screen to see if
  103.          * there is an offer
  104.          */
  105.         RSInterfaceChild ourSide = Interfaces.get(335, 48);
  106.         if(ourSide != null){
  107.             RSInterfaceComponent[] ourChildren = ourSide.getChildren();
  108.             if(ourChildren != null) {
  109.                 for(RSInterfaceComponent i : ourChildren){
  110.                     if (i != null && !i.isHidden()) {
  111.                         if (i.getComponentItem() != -1) {
  112.                             return true;
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.         return false;
  119.     }
  120.  
  121.     public void handleSecondTrade(){
  122.         if(tradeIsGood()){
  123.             clicker.handleTradeScreen(true);
  124.         }
  125.         else {
  126.             clicker.handleTradeScreen(false);
  127.         }
  128.         }
  129.        
  130.     public boolean canAccept(){  
  131.         //*
  132.         /* Checks through the trade screen to see
  133.          * if its the item that we want to have
  134.          */
  135.        RSInterface theirSide  = Interfaces.get(335, 50);
  136.        if (theirSide != null){
  137.            RSInterfaceComponent[] offers = Interfaces.get(335, 50).getChildren();
  138.            if (offers != null) {
  139.                for (RSInterfaceComponent i : offers){
  140.                    if (i != null && !i.isHidden()) {
  141.                        String item = i.getComponentName();
  142.                        if (item != null && (item.contains("Coins")
  143.                                || item.contains(values.itemName))){
  144.                            return !clickedAccept();
  145.                        }
  146.                    }
  147.                }
  148.            }
  149.        }
  150.        return false;
  151.     }
  152.  
  153.     public boolean clickedAccept() {
  154.         RSInterface accepted = Interfaces.get(Interfaces.isInterfaceValid(335) ? 335 : 334,                                                                   Interfaces.isInterfaceValid(335) ? 56 : 33);
  155.         if (accepted != null) {
  156.             String text = accepted.getText();
  157.             return text != null && (text.contains(values.WAITING_MESSAGE));
  158.         }
  159.         return false;
  160.     }
  161.  
  162.     public static boolean haveAnOffer(){
  163.         RSInterfaceChild theirSide = Interfaces.get(335, 50);
  164.         if(theirSide != null){
  165.             RSInterfaceComponent[] theirChildren = theirSide.getChildren();
  166.             if(theirChildren != null) {
  167.                 for(RSInterfaceComponent i : theirChildren){
  168.                     if (i != null && !i.isHidden()) {
  169.                         if (i.getComponentItem() != -1) {
  170.                             return true;
  171.                         }
  172.                     }
  173.                 }
  174.             }
  175.         }
  176.         return false;
  177.     }
  178.  
  179.     public boolean tradeIsGood() {
  180.         //*
  181.         /* Checks through the trade screen to see
  182.          * if its the item that we want to have,
  183.          * Or coins and the right amount
  184.          */
  185.         if (Interfaces.isInterfaceValid(334)) {
  186.             for (RSInterface i : Interfaces.get(334, 40).getChildren()) {
  187.                 if (i != null && (i.getText().contains(getCoinAmount())
  188.                                     && i.getText().contains("Coins"))                  
  189.                         || i.getText().contains(values.itemName)) {
  190.                     return true;
  191.                 }
  192.             }
  193.         }
  194.         return false;
  195.     }
  196.  
  197.     private String getCoinAmount() {
  198.         //adds nessesary commas to match the onscreen text
  199.         String amt = Integer.toString(values.sellingPrice);
  200.         StringBuffer sub = new StringBuffer(amt);
  201.         if (amt.length() == 5) {
  202.             //thousands
  203.             amt = sub.insert(2, ",").toString();
  204.             General.println(amt);
  205.         }
  206.         else if (amt.length() == 6) {
  207.             //hundred thousands
  208.             amt = sub.insert(3, ",").toString();
  209.         }
  210.         else if (amt.length() == 7) {
  211.             //millions
  212.             amt = sub.insert(1, ",").toString();
  213.             StringBuffer sub2 = new StringBuffer(amt);
  214.             amt = sub2.insert(5, ",").toString();
  215.         }
  216.         return amt;
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement