Advertisement
Guest User

Sp333th Newt Farmer V1.0

a guest
Apr 29th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.38 KB | None | 0 0
  1. package scripts;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.awt.Point;
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.io.Reader;
  12. import java.net.URL;
  13. import java.net.URLConnection;
  14. import java.util.ArrayList;
  15. import java.util.Random;
  16. import java.util.regex.Matcher;
  17. import java.util.regex.Pattern;
  18.  
  19. import javax.imageio.ImageIO;
  20.  
  21. import org.tribot.api.General;
  22. import org.tribot.api.Timing;
  23. import org.tribot.api.input.Mouse;
  24. import org.tribot.api2007.Banking;
  25. import org.tribot.api2007.Camera;
  26. import org.tribot.api2007.ChooseOption;
  27. import org.tribot.api2007.Combat;
  28. import org.tribot.api2007.GameTab;
  29. import org.tribot.api2007.Interfaces;
  30. import org.tribot.api2007.Inventory;
  31. import org.tribot.api2007.Login;
  32. import org.tribot.api2007.NPCs;
  33. import org.tribot.api2007.Player;
  34. import org.tribot.api2007.Screen;
  35. import org.tribot.api2007.Walking;
  36. import org.tribot.api2007.ext.Doors;
  37. import org.tribot.api2007.types.RSInterfaceChild;
  38. import org.tribot.api2007.types.RSItem;
  39. import org.tribot.api2007.types.RSNPC;
  40. import org.tribot.api2007.types.RSTile;
  41. import org.tribot.script.Script;
  42. import org.tribot.script.ScriptManifest;
  43. import org.tribot.script.interfaces.Painting;
  44.  
  45. /**
  46.  * @author Kevin Spaeth (Sp333th)
  47.  * Buys Eye of Newt from Hetty's shop in Port Sarim
  48.  */
  49. @ScriptManifest(authors = { "Sp333th" }, category = "Money Making", name = "Sp333th Newt Farmer", description = "<html><body><img src=\"http://s10.postimg.org/3xvvgubp1/Sp333th_Newt_Logo.png\" alt=\"Sp333th Newt Farmer\"></body></html>")
  50. public class Sp333thNewtFarmer extends Script implements Painting {
  51.     /**
  52.      * True - If the loop is running
  53.      */
  54.     private boolean running;
  55.     /**
  56.      * ID of an Eye of Newt
  57.      */
  58.     private int newtID = 221;
  59.     /**
  60.      * ID of gold baby!
  61.      */
  62.     private int moneyID = 995;
  63.     /**
  64.      * Price of an eye of newt
  65.      */
  66.     private int newtPrice = -1;
  67.     /**
  68.      * The amount of newts the player has collected
  69.      */
  70.     private int newts = 0;
  71.     /**
  72.      * Hetty's (store owner) ID
  73.      */
  74.     private int storeOwnerID = 5659;
  75.     /**
  76.      * The current time (when the bot started)
  77.      */
  78.     private long timeBegan = System.currentTimeMillis();
  79.     /**
  80.      * The location of the door outside Hetty's
  81.      */
  82.     private RSTile doorTile = new RSTile(3017, 3259, 0);
  83.     /**
  84.      * Inside of Hetty's shop
  85.      */
  86.     private RSTile storeTile = new RSTile(3014, 3259, 0);//Inside Hetty's shop
  87.     /**
  88.      * In front of the bank window
  89.      */
  90.     private RSTile bankTile = new RSTile(3092, 3245, 0);//In front of the bank window
  91.     /**
  92.      * Color of the text on the GUI (pink)
  93.      */
  94.     private final Color textColor = new Color(255, 74, 210);
  95.     /**
  96.      * Font of the text on the GUI
  97.      */
  98.     private final Font textFont = new Font("Arial", Font.BOLD, 12);
  99.     /**
  100.      * URL of the GUI background
  101.      */
  102.     private final Image paintImg = getImage("http://s10.postimg.org/3xvvgubp1/Sp333th_Newt_Logo.png");
  103.     /**
  104.      * Path from the bank to the store
  105.      */
  106.     private RSTile[] pathToStore = {
  107.             new RSTile(3092, 3245, 0),//In front of the bank window
  108.             new RSTile(3084, 3249, 0),
  109.             new RSTile(3081, 3260, 0),
  110.             new RSTile(3075, 3269, 0),
  111.             new RSTile(3066, 3275, 0),
  112.             new RSTile(3054, 3275, 0),
  113.             new RSTile(3043, 3274, 0),
  114.             new RSTile(3032, 3272, 0),
  115.             new RSTile(3022, 3265, 0),
  116.             new RSTile(3017, 3259, 0),//Outside Hetty's shop
  117.     };
  118.  
  119.     @Override
  120.     public void onPaint(Graphics arg0) {
  121.         //Calculate variables to draw.
  122.         long timeRan = System.currentTimeMillis() - timeBegan;
  123.         int goldEarned = newtPrice * newts;
  124.         String gold, newt;
  125.  
  126.         //Change string if its over 100k
  127.         if(goldEarned >= 100000) {
  128.             goldEarned /= 1000;
  129.             gold = goldEarned + "k";
  130.         } else {
  131.             gold = goldEarned + "";
  132.         }
  133.  
  134.         //Change string if its over 10k
  135.         if(newts >= 10000) {
  136.             int tempNewts = newts / 1000;
  137.             newt = tempNewts + "k";
  138.         } else {
  139.  
  140.             newt = newts + "";
  141.         }
  142.  
  143.         //Set font and text.
  144.         arg0.setFont(textFont);
  145.         arg0.setColor(textColor);
  146.  
  147.         //Draw paint.
  148.         arg0.drawImage(paintImg, 390, 230, null);
  149.         arg0.drawString("Time Ran: " + Timing.msToString(timeRan), 400, 308);
  150.         arg0.drawString("Eye of Newt: " + newt, 400, 308+12);
  151.         arg0.drawString("Gold Earned: " + gold, 400, 308+12+12);
  152.     }
  153.  
  154.     @Override
  155.     public void run() {
  156.         if(onStart()) {
  157.             while(running) {
  158.                 sleep(loop());
  159.             }
  160.             printReport();
  161.         }
  162.     }
  163.  
  164.     /**
  165.      * @return true - When the script has finished starting
  166.      */
  167.     private boolean onStart() {
  168.         boolean started = false;
  169.  
  170.         Walking.setControlClick(active);
  171.         Mouse.setSpeed(150);
  172.         newtPrice = lookup(92) - 3;
  173.         running = true;
  174.  
  175.         started = true;
  176.         return started;
  177.     }
  178.  
  179.     /**
  180.      * @return The duration to sleep for
  181.      */
  182.     private int loop() {
  183.         if(hasMoney()) {   
  184.             if(needToBank()) {
  185.                 if(!atBank()) {
  186.                     Walking.walkPath(Walking.invertPath(pathToStore));
  187.                     waitUntilIdle();
  188.                 }
  189.                 bank();
  190.  
  191.             } else if(!atStore()) {
  192.                 Walking.walkPath(pathToStore);
  193.                 waitUntilIdle();
  194.  
  195.             } else {
  196.                 if(!isDoorOpen()) {
  197.                     openDoor();
  198.                 }
  199.  
  200.                 if(atStoreDoor() && !isHettyOnScreen()) {
  201.                     Walking.walkTo(storeTile);
  202.                     waitUntilIdle();
  203.                 }
  204.  
  205.                 if(!isShopOpen()) {
  206.                     openShop();
  207.                 } else {
  208.                     if(getNewts() < 100) {
  209.                         worldHop();
  210.                     } else {
  211.                         buyNewts();
  212.                     }
  213.                 }
  214.             }
  215.         } else {
  216.             terminate();
  217.         }
  218.  
  219.         Random rand = new Random();
  220.         return rand.nextInt(1000) + 250;
  221.     }
  222.  
  223.     /**
  224.      * Waits until the player is idle
  225.      */
  226.     private void waitUntilIdle() {
  227.         while(true) {
  228.             if(!Player.isMoving() && Player.getAnimation() == -1) {
  229.                 break;//Break loop if idle
  230.             }
  231.  
  232.             if(!isShopOpen() && !Banking.isBankScreenOpen() && !Banking.isPinMessageOpen() && !Banking.isPinScreenOpen()) {
  233.                 antiBan();
  234.             }
  235.             sleep(100, 250);
  236.         }
  237.     }
  238.  
  239.     /**
  240.      * This happens when the script ends
  241.      */
  242.     private void terminate() {
  243.         if(!atBank()) {
  244.             Walking.walkPath(Walking.invertPath(pathToStore));
  245.             waitUntilIdle();
  246.         }
  247.  
  248.         RSItem[] items = Inventory.getAll();
  249.         if(atBank() && items != null & items.length > 0) {
  250.             //If the bank screen isn't open, open it
  251.             if(!Banking.isBankScreenOpen()) {
  252.                 Banking.openBankBooth();
  253.             } else {
  254.                 //Deposit all items except money
  255.                 Banking.depositAll();
  256.                 Banking.close();
  257.             }
  258.         }
  259.         running = false;
  260.     }
  261.  
  262.     private void printReport() {
  263.         //Calculate variables to draw.
  264.         long timeRan = System.currentTimeMillis() - timeBegan;
  265.         int goldEarned = newtPrice * newts;
  266.  
  267.         println("**********************");
  268.         println("Time Ran: " + Timing.msToString(timeRan));
  269.         println("Eye of Newt: " + newts);
  270.         println("Gold Earned: " + goldEarned);
  271.         println("**********************");
  272.     }
  273.  
  274.     /**
  275.      * Opens the doors to Hetty's shop
  276.      */
  277.     private void openDoor() {
  278.         if(atStore() && Doors.isDoorAt(doorTile, false) && Doors.getDoorAt(doorTile).isOnScreen()) {
  279.             Doors.handleDoorAt(doorTile, true);
  280.             waitUntilIdle();
  281.         }
  282.     }
  283.  
  284.     /**
  285.      * Opens Hetty's shop
  286.      */
  287.     private void openShop() {
  288.         RSNPC[] npcs = NPCs.findNearest(storeOwnerID);
  289.  
  290.         if(atStore() && npcs != null && npcs.length > 0) {
  291.             RSNPC hetty = npcs[0];
  292.  
  293.             if(hetty != null && hetty.getID() == storeOwnerID && hetty.isOnScreen()) {
  294.                 hetty.click("Trade");
  295.                 waitUntilIdle();
  296.             }
  297.         }
  298.     }
  299.  
  300.     /**
  301.      * Buys 10 newts from Hetty
  302.      */
  303.     public void buyNewts() {
  304.         RSInterfaceChild intChild = Interfaces.get(300, 75);
  305.         int index = -1;
  306.  
  307.         if(isShopOpen()) {
  308.             for(int i = 0; i < intChild.getItems().length; i++) {
  309.                 if(intChild.getItems()[i].getID() == newtID) {
  310.                     index = i;
  311.                 }
  312.             }
  313.  
  314.             if(index != -1) {
  315.                 int freeSpace = 28 - Inventory.getAll().length;
  316.                 int itemX = (int) Math.ceil((index) % 8);
  317.                 int itemY = (int) ((Math.floor(index) / 8) % 5);
  318.                 int y = (itemY * 45) + 82;
  319.                 int x = (itemX * 46) + 97;
  320.  
  321.                 Mouse.move(new Point(x, y));
  322.                 sleep(200);
  323.                 Mouse.click(3);
  324.  
  325.                 if (ChooseOption.isOpen() && ChooseOption.isOptionValid("Buy 10")) {
  326.                     ChooseOption.select("Buy 10");
  327.  
  328.                     if(freeSpace >= 10 && getMoney() >= 30) {
  329.                         newts += 10;
  330.                     } else if(getMoney() >= freeSpace*3){
  331.                         newts += freeSpace;
  332.                     } else {
  333.                         newts += Math.floor(getMoney()/3);
  334.                     }
  335.                 }
  336.             }
  337.         }
  338.     }
  339.  
  340.     /**
  341.      * Deposits newts into the player's bank
  342.      */
  343.     private void bank() {
  344.         if(atBank() && Inventory.isFull()) {
  345.             //If the bank screen isn't open, open it
  346.             if(!Banking.isBankScreenOpen()) {
  347.                 Banking.openBankBooth();
  348.             } else {
  349.                 //Deposit all items except money
  350.                 Banking.depositAllExcept(moneyID);
  351.             }
  352.         }
  353.     }
  354.  
  355.     /**
  356.      * Switches to a world with higher supply
  357.      */
  358.     private void worldHop() {
  359.         int newWorld = General.random(1, 78);
  360.  
  361.         while(newWorld == 25 || newWorld == 37) {
  362.             newWorld = General.random(1, 78);
  363.         }
  364.  
  365.         super.setLoginBotState(false);
  366.         super.setRandomSolverState(false);
  367.         worldHop(newWorld, true, false);//random world hop
  368.         super.setLoginBotState(true);
  369.         super.setRandomSolverState(true);
  370.     }
  371.  
  372.     /**
  373.      * @return Hetty's current stock of newts
  374.      */
  375.     private int getNewts() {
  376.         RSInterfaceChild intChild = Interfaces.get(300, 75);
  377.         int quantity = -1;
  378.         int index = -1;
  379.  
  380.         if(isShopOpen()) {
  381.             for(int i = 0; i < intChild.getItems().length; i++) {
  382.                 if(intChild.getItems()[i].getID() == newtID) {
  383.                     index = i;
  384.                 }
  385.             }
  386.  
  387.             if(index != -1) {
  388.                 quantity = intChild.getItems()[index].getStack();
  389.             }
  390.         }
  391.         return quantity;
  392.     }
  393.  
  394.     /**
  395.      * @return The amount of gold the Player has
  396.      */
  397.     private int getMoney() {
  398.         RSItem[] items = Inventory.find(moneyID);
  399.         int gold = -1;
  400.  
  401.         if(items != null && items.length > 0) {
  402.             RSItem money = items[0];
  403.  
  404.             if(money != null) {
  405.                 gold = money.getStack();
  406.             }
  407.         }
  408.         return gold;
  409.     }
  410.  
  411.     /**
  412.      * @param url Is the address of the image
  413.      * @return The images hosted at the URL
  414.      */
  415.     private Image getImage(String url) {
  416.         //Try to get the image at the specified URL
  417.         try{
  418.             return ImageIO.read(new URL(url));
  419.             //The following executes if the try block throws an exception
  420.         }catch(IOException e){
  421.             //Although... nothing is done
  422.             return null;
  423.         }
  424.     }
  425.  
  426.     /**
  427.      * @return true - If the player needs to bank
  428.      */
  429.     private boolean needToBank() {
  430.         boolean needBank = false;
  431.  
  432.         if(Inventory.isFull()) {
  433.             needBank = true;
  434.         }
  435.         return needBank;
  436.     }
  437.  
  438.     /**
  439.      * @return true - If the player has at least 3 coins
  440.      */
  441.     private boolean hasMoney() {
  442.         RSItem[] items = Inventory.find(moneyID);
  443.         boolean hasMoney = false;
  444.  
  445.         if(items != null && items.length > 0) {
  446.             RSItem money = items[0];
  447.  
  448.             if(money != null && money.getStack() >= 3) {
  449.                 hasMoney = true;
  450.             }
  451.         }
  452.         return hasMoney;
  453.     }
  454.  
  455.     /**
  456.      * @return true - If the play is at the bank
  457.      */
  458.     private boolean atBank() {
  459.         boolean atBank = false;
  460.  
  461.         if(Player.getPosition().distanceTo(bankTile) <= 3) {
  462.             atBank = true;
  463.         }
  464.         return atBank;
  465.     }
  466.  
  467.     /**
  468.      * @return true - If the player is at the store
  469.      */
  470.     private boolean atStore() {
  471.         boolean atShop = false;
  472.  
  473.         if(Player.getPosition().distanceTo(storeTile) <= 3) {
  474.             atShop = true;
  475.         }
  476.         return atShop;
  477.     }
  478.  
  479.     /**
  480.      * @return true - If the player has Hetty's shop open
  481.      */
  482.     private boolean isShopOpen() {
  483.         return Interfaces.get(300, 75) != null;
  484.     }
  485.  
  486.     /**
  487.      * @return true - If the door to Hetty's shop is open
  488.      */
  489.     private boolean isDoorOpen() {
  490.         boolean open = true;
  491.  
  492.         if(atStore() && Doors.isDoorAt(doorTile, false)) {
  493.             open = false;
  494.         }
  495.         return open;
  496.     }
  497.  
  498.     /**
  499.      * @return true - If the player is at the door of the store
  500.      */
  501.     private boolean atStoreDoor() {
  502.         boolean atDoor = false;
  503.  
  504.         if(Player.getPosition().distanceTo(doorTile) <= 2) {
  505.             atDoor = true;
  506.         }
  507.         return atDoor;
  508.     }
  509.  
  510.     /**
  511.      * @return true - If Hetty is on the screen
  512.      */
  513.     private boolean isHettyOnScreen() {
  514.         boolean onScreen = false;
  515.  
  516.         RSNPC[] npcs = NPCs.findNearest(storeOwnerID);
  517.  
  518.         if(atStore() && npcs != null && npcs.length > 0) {
  519.             RSNPC hetty = npcs[0];
  520.  
  521.             if(hetty != null && hetty.getID() == storeOwnerID && hetty.isOnScreen()) {
  522.                 onScreen = true;
  523.             }
  524.         }
  525.         return onScreen;
  526.     }
  527.  
  528.     /**
  529.      * Random movements to prevent getting banned
  530.      */
  531.     private void antiBan() {
  532.         //Randomly perform the Anti-ban.
  533.         int random = General.random(1, 2000) % 200;
  534.  
  535.         switch(random) {
  536.         case 29:
  537.             //Move the mouse slightly
  538.             println("Anti-Ban: Moving Mouse Slightly.");
  539.             moveMouse(25, 250);
  540.             break;
  541.         case 49:
  542.             //Move the mouse far
  543.             println("Anti-Ban: Moving Mouse Further.");
  544.             moveMouse(250, 1250);
  545.             break;
  546.         case 71:
  547.             //Check the stats
  548.             println("Anti-Ban: Checking Stats.");
  549.             checkStats();
  550.             break;
  551.         case 97:
  552.             //Check the friends list
  553.             println("Anti-Ban: Checking Friends.");
  554.             checkFriends();
  555.             break;
  556.         case 104:
  557.             //Move the camera
  558.             println("Anti-Ban: Moving the Camera.");
  559.             moveCamera();
  560.             break;
  561.         case 126:
  562.             //Move the mouse slightly
  563.             println("Anti-Ban: Moving Mouse Slightly.");
  564.             moveMouse(25, 250);
  565.             break;
  566.         }
  567.     }
  568.  
  569.     /**
  570.      * Moves the camera around
  571.      */
  572.     private void moveCamera() {
  573.         //Move the camera to a random position
  574.         Camera.setCameraAngle(General.random(25, 75));
  575.         Camera.setCameraRotation(General.random(1, 360));
  576.  
  577.         //Wait
  578.         sleep(500, 1000);
  579.  
  580.         //Restore camera Angle
  581.         Camera.setCameraAngle(General.random(80, 90));
  582.  
  583.         sleep(500, 1000);
  584.  
  585.         //Restore camera Rotation
  586.         Camera.setCameraRotation(General.random(1, 25));
  587.     }
  588.  
  589.     /**
  590.      * Checks the player's stats, and then returns to the inventory
  591.      */
  592.     private void checkStats() {
  593.         //Checks the player's stats
  594.         GameTab.open(GameTab.TABS.STATS);
  595.         sleep(500, 1000);
  596.         GameTab.open(GameTab.TABS.INVENTORY);
  597.         sleep(500, 1000);
  598.     }
  599.  
  600.     /**
  601.      * Check the player's friends and then returns to the inventory
  602.      */
  603.     private void checkFriends() {
  604.         //Checks the player's friends.
  605.         GameTab.open(GameTab.TABS.FRIENDS);
  606.         sleep(500, 1000);
  607.         GameTab.open(GameTab.TABS.INVENTORY);
  608.         sleep(500, 1000);
  609.     }
  610.  
  611.     /**
  612.      * @param min Is the minimum distance to move the mouse.
  613.      * @param max Is the maximum distance to move the mouse.
  614.      */
  615.     private void moveMouse(int min, int max) {
  616.         //Gets the mouses coordinates
  617.         int mouseX = Mouse.getPos().x;
  618.         int mouseY = Mouse.getPos().y;
  619.         //Randomly generates a distance to move the mouse
  620.         int randomX = General.random(min, max) - ((max-min)/2);
  621.         int randomY = General.random(min, max) - ((max-min)/2);
  622.         //Adds the distance to the coordinates
  623.         Mouse.move(mouseX + randomX, mouseY + randomY);
  624.     }
  625.  
  626.     /* The following code was written by wastedbro
  627.      * on the Tribot forums. He deserves credit for
  628.      * its use so please don't take it without giving
  629.      * him credit for it!
  630.      */
  631.     /**
  632.      * @author wastedbro
  633.      */
  634.     public void worldHop(int worldNumber, boolean excludeFrees, boolean excludePopular)
  635.     {
  636.         logout(); //Logs out if needed
  637.         while(loggedOut())
  638.         {
  639.             openWorldSelect();
  640.         }
  641.         while(worldSelectOpen())
  642.         {
  643.             selectWorld(worldNumber,excludeFrees,excludePopular);
  644.         }
  645.     }
  646.  
  647.     /**
  648.      * @author wastedbro
  649.      */
  650.     public void selectWorld(int worldNumber, boolean excludeFrees, boolean excludePopular)
  651.     {
  652.         ArrayList<Point> world = new ArrayList<Point>();
  653.         int counter = 1;
  654.         while((worldNumber == 7 || worldNumber == 15 || worldNumber == 23 || worldNumber == 24 || worldNumber == 31 || worldNumber == 32 || worldNumber == 39 || worldNumber == 40 || worldNumber == 47 || worldNumber == 48 || worldNumber == 55 || worldNumber == 56 || worldNumber == 63 || worldNumber == 64 || worldNumber == 71 || worldNumber == 72))
  655.         {
  656.             worldNumber = General.random(1, 78);
  657.             if((worldNumber == 1 || worldNumber == 2) && excludePopular)
  658.             {
  659.                 continue;
  660.             }
  661.             if((worldNumber == 8 || worldNumber == 16) && excludeFrees)
  662.             {
  663.                 continue;
  664.             }
  665.         } //The loops above ensure the world chosen exists and not free/popular depending on the boolean parameters
  666.  
  667.  
  668.         for(int columns = 230, y = 1; y < 5 ; columns=columns+95, y++) // Populates the world arrayList with points relative to the world
  669.         {
  670.             for(int rows = 81, x = 1; x < 17; rows=rows+24, x++, counter++)
  671.             {
  672.                 while(counter == 7 || counter == 15 || counter == 23 || counter == 24 || counter == 31 || counter == 32 || counter == 39 || counter == 40 || counter == 47 || counter == 48 || counter == 55 || counter == 56 || counter == 63 || counter == 64 || counter == 71 || counter == 72)
  673.                 {
  674.                     world.add(new Point(0,0));
  675.                     counter++;
  676.                 }
  677.                 world.add(new Point(columns,rows));
  678.             }
  679.         }
  680.  
  681.         worldNumber--;
  682.  
  683.         Mouse.clickBox(world.get(worldNumber).x - 10, world.get(worldNumber).y - 5, world.get(worldNumber).x + 10, world.get(worldNumber).y + 5, 1);
  684.         for(int x = 0; x < 6; x++) //Waits up to 3 seconds for the login screen. Checks every half a second (Approx.)
  685.         {
  686.             if(loggedOut())
  687.             {
  688.                 break;
  689.             }
  690.             sleep(490,510);
  691.         }
  692.     }
  693.  
  694.     /**
  695.      * @author wastedbro
  696.      */
  697.     public boolean worldSelectOpen()
  698.     {
  699.         if (Login.getLoginState() == Login.STATE.LOGINSCREEN)
  700.         {
  701.             Color black = new Color(0,0,0);
  702.             return (Screen.getColorAt(100, 200).equals(black));
  703.         }
  704.         return false;
  705.     }
  706.  
  707.     /**
  708.      * @author wastedbro
  709.      */
  710.     public boolean openWorldSelect()
  711.     {
  712.         if (Login.getLoginState() == Login.STATE.LOGINSCREEN)
  713.         {
  714.             Mouse.clickBox(10, 465, 100, 495, 1);
  715.             for(int x = 0; x < 10; x++)
  716.             {
  717.                 if(worldSelectOpen())
  718.                 {
  719.                     return true;
  720.                 }
  721.                 sleep(490,510);
  722.             }
  723.         }
  724.         return false;
  725.     }
  726.  
  727.     /**
  728.      * @author wastedbro
  729.      */
  730.     public boolean loggedOut()
  731.     {
  732.         return (Login.getLoginState() == Login.STATE.LOGINSCREEN) && !(Screen.getColorAt(100, 200).equals(new Color(0,0,0)));
  733.     }
  734.  
  735.     /**
  736.      * @author wastedbro
  737.      */
  738.     public boolean logout()
  739.     {
  740.         if (Login.getLoginState() == Login.STATE.INGAME)
  741.         {
  742.             Login.logout();
  743.             for(int x = 0; x < 6; x++)
  744.             {
  745.                 if(loggedOut())
  746.                 {
  747.                     return true;
  748.                 }
  749.                 sleep(490,510);
  750.             }
  751.         }
  752.         return false;
  753.     }
  754.  
  755.     /* The following code was writting by Administrator
  756.      * on the Tribot forums. Give him/her credit for its
  757.      * successes and failures.
  758.      */
  759.     private static final String LINK = "http://forums.zybez.net/pages/2007-price-guide?id=";
  760.     private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17";
  761.     private final static Pattern NAME = Pattern.compile("Price Guide: (.+?)</h1>");
  762.     private final static Pattern PRICE = Pattern.compile("Recent Trade Price: ~(.+?) GP");
  763.  
  764.     /**
  765.      * @author Administrator
  766.      */
  767.     public final static int lookup(final int id) {
  768.         try {
  769.             final BufferedReader br = getReader(id);
  770.             br.skip(18700);
  771.  
  772.             Matcher m = NAME.matcher(br.readLine());
  773.             @SuppressWarnings("unused")
  774.             String name = "";
  775.             if (m.find()) {
  776.                 name = m.group(1);
  777.             }
  778.  
  779.             String line = "";
  780.             while (!line.contains("Recent Trade Price")) {
  781.                 line = br.readLine();
  782.             }
  783.  
  784.             m = PRICE.matcher(line);
  785.             int price = 0;
  786.             if (m.find()) {
  787.                 price = Integer.parseInt(m.group(1).replaceAll(",", ""));
  788.             }
  789.             return price;
  790.         } catch (IOException e) {
  791.             e.printStackTrace();
  792.         }
  793.         return -1;
  794.     }
  795.  
  796.     /**
  797.      * @author Administrator
  798.      */
  799.     private static BufferedReader getReader(final int id) {
  800.         try {
  801.             final URL url = new URL(LINK + id);
  802.             final URLConnection con = url.openConnection();
  803.             con.setRequestProperty("User-Agent", USER_AGENT);
  804.             final Reader inr = new InputStreamReader(con.getInputStream());
  805.             return new BufferedReader(inr);
  806.         } catch (IOException e) {
  807.             System.out.println("Item not found.");
  808.         }
  809.         return null;
  810.     }
  811. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement