Don't like ads? PRO users don't see any ads ;-)
Guest

iSwampTar

By: a guest on Apr 28th, 2012  |  syntax: Java  |  size: 7.92 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Image;
  7. import java.awt.RenderingHints;
  8. import java.io.IOException;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import javax.imageio.ImageIO;
  12. import org.powerbot.concurrent.Task;
  13. import org.powerbot.concurrent.strategy.Strategy;
  14. import org.powerbot.game.api.ActiveScript;
  15. import org.powerbot.game.api.Manifest;
  16. import org.powerbot.game.api.methods.Calculations;
  17. import org.powerbot.game.api.methods.Walking;
  18. import org.powerbot.game.api.methods.interactive.Players;
  19. import org.powerbot.game.api.methods.node.GroundItems;
  20. import org.powerbot.game.api.util.Time;
  21. import org.powerbot.game.api.wrappers.Tile;
  22. import org.powerbot.game.api.wrappers.node.GroundItem;
  23. import org.powerbot.game.bot.event.listener.PaintListener;
  24. import java.io.DataInputStream;
  25. import java.net.URL;
  26. import java.net.URLConnection;
  27. import org.powerbot.game.api.methods.tab.Inventory;
  28. import org.powerbot.game.api.util.Random;
  29.  
  30. /**
  31.  *
  32.  * @author iJava
  33.  */
  34. @Manifest(authors = "iJava", name = "iSwampTar", version = 1.0, description = "Collects Swamp Tar Anywhere.")
  35. public class iSwampTar extends ActiveScript implements PaintListener {
  36.  
  37.     private final int SWAMP_TAR_ID = 1939;
  38.     private long millis, minutes, hours, seconds, startTime;
  39.     private String status, mins, secs = "";
  40.     private int tarCollected, tarCollectedPH, gpEarned, gpEarnedPH, swampTarPrice = 0;
  41.     private Image paintBackground;
  42.  
  43.     @Override
  44.     protected void setup() {
  45.         try {
  46.             provide(new Sleeper());
  47.             provide(new CollectTar());
  48.             provide(new WalkToTar());
  49.             startTime = System.currentTimeMillis();
  50.             status = "Starting Up";
  51.             swampTarPrice = GrandExchange.getCurrentGuidePrice(SWAMP_TAR_ID);
  52.             paintBackground = ImageIO.read(new URL("http://img440.imageshack.us/img440/7360/iswamptar.png"));
  53.         } catch (IOException ex) {
  54.             Logger.getLogger(iSwampTar.class.getName()).log(Level.SEVERE, null, ex);
  55.         } catch (InterruptedException ex) {
  56.             Logger.getLogger(iSwampTar.class.getName()).log(Level.SEVERE, null, ex);
  57.         }
  58.     }
  59.  
  60.     @Override
  61.     public void onRepaint(Graphics g1) {
  62.         tarCollectedPH = (int) ((tarCollected) * 3600000D / (System.currentTimeMillis() - startTime));
  63.         gpEarned = swampTarPrice * tarCollected;
  64.         gpEarnedPH = (int) ((gpEarned) * 3600000D / (System.currentTimeMillis() - startTime));
  65.         millis = System.currentTimeMillis() - startTime;
  66.         hours = millis / (1000 * 60 * 60);
  67.         millis -= hours * (1000 * 60 * 60);
  68.         minutes = millis / (1000 * 60);
  69.         mins = "" + minutes;
  70.         while (mins.length() < 2) {
  71.             mins = "0" + mins;
  72.         }
  73.         millis -= minutes * (1000 * 60);
  74.         seconds = millis / 1000;
  75.         secs = "" + seconds;
  76.         while (secs.length() < 2) {
  77.             secs = "0" + secs;
  78.         }
  79.         Graphics2D g = (Graphics2D) g1;
  80.         g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF));
  81.         g.drawImage(paintBackground, 0, 340, null);
  82.         g.setColor(new Color(0, 0, 0));
  83.         g.setFont(new Font("Arial", 0, 14));
  84.         g.drawString("" + gpEarnedPH, 362, 415);
  85.         g.drawString("" + gpEarned, 332, 398);
  86.         g.drawString("" + hours + " : " + mins + " : " + secs, 355, 432);
  87.         g.drawString("" + tarCollected, 126, 417);
  88.         g.drawString("" + tarCollectedPH, 173, 433);
  89.         g.drawString("" + status, 85, 402);
  90.     }
  91.  
  92.     private class Sleeper extends Strategy implements Task {
  93.  
  94.         @Override
  95.         public boolean validate() {
  96.             return Players.getLocal().isMoving();
  97.         }
  98.  
  99.         @Override
  100.         public void run() {
  101.             Time.sleep(300, 800);
  102.         }
  103.     }
  104.  
  105.     private class CollectTar extends Strategy implements Task {
  106.  
  107.         @Override
  108.         public boolean validate() {
  109.             GroundItem swampTar = GroundItems.getNearest(SWAMP_TAR_ID);
  110.             if (swampTar == null) {
  111.                 return false;
  112.             }
  113.             return swampTar.isOnScreen();
  114.         }
  115.  
  116.         @Override
  117.         public void run() {
  118.             status = "Collecting Tar";
  119.             GroundItem swampTar = GroundItems.getNearest(SWAMP_TAR_ID);
  120.             if (swampTar != null && swampTar.isOnScreen()) {
  121.                 int temp = Inventory.getItem(SWAMP_TAR_ID).getStackSize();
  122.                 swampTar.interact("Take");
  123.                 Time.sleep(300, 500);
  124.                 int temp2 = Inventory.getItem(SWAMP_TAR_ID).getStackSize();
  125.                 if (temp != temp2) {
  126.                     tarCollected++;
  127.                 }
  128.             }
  129.         }
  130.     }
  131.  
  132.     private class WalkToTar extends Strategy implements Task {
  133.  
  134.         @Override
  135.         public boolean validate() {
  136.             GroundItem swampTar = GroundItems.getNearest(SWAMP_TAR_ID);
  137.             if (swampTar == null) {
  138.                 return false;
  139.             }
  140.             return !swampTar.isOnScreen();
  141.         }
  142.  
  143.         @Override
  144.         public void run() {
  145.             status = "Finding Tar";
  146.             if (!Walking.isRunEnabled() && Walking.getEnergy() > Random.nextInt(25, 50)) {
  147.                 Walking.setRun(true);
  148.             }
  149.             GroundItem[] swampTars = GroundItems.getLoaded(SWAMP_TAR_ID);
  150.             Tile swampTarLoc = swampTars[0].getLocation();
  151.             for (GroundItem swampTar : swampTars) {
  152.                 if (Calculations.distanceTo(swampTarLoc) > Calculations.distanceTo(swampTar)) {
  153.                     swampTarLoc = swampTar.getLocation();
  154.                 }
  155.             }
  156.             if (swampTarLoc.isOnMap()) {
  157.                 while (Calculations.distanceTo(swampTarLoc) > 3) {
  158.                     Walking.walk(swampTarLoc);
  159.                 }
  160.             }
  161.         }
  162.     }
  163.  
  164.     public static class GrandExchange {
  165.  
  166.         private static String[] currGuidePriceData;
  167.  
  168.         public static int getCurrentGuidePrice(int itemID) throws IOException, InterruptedException {
  169.             URL url = new URL("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + itemID);
  170.             String html = readPage(url, "http://google.com");
  171.             currGuidePriceData = html.split("\"current\":\\{\"trend\":\"neutral\",\"price\":|\\},\"today\":\\{\"trend\"");
  172.             return Integer.parseInt(currGuidePriceData[1]);
  173.         }
  174.  
  175.         public static String readPage(URL url, String referer) throws IOException,
  176.                 InterruptedException {
  177.             URLConnection uc = url.openConnection();
  178.             uc.addRequestProperty(
  179.                     "Accept",
  180.                     "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
  181.             uc.addRequestProperty("Accept-Charset",
  182.                     "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
  183.             uc.addRequestProperty("Accept-Encoding", "gzip,deflate");
  184.             uc.addRequestProperty("Accept-Language", "en-gb,en;q=0.5");
  185.             uc.addRequestProperty("Connection", "keep-alive");
  186.             uc.addRequestProperty("Host", "www.runescape.com");
  187.             uc.addRequestProperty("Keep-Alive", "300");
  188.             if (referer != null && !referer.trim().equals("")) {
  189.                 uc.addRequestProperty("Referer", referer);
  190.             }
  191.             uc.addRequestProperty(
  192.                     "User-Agent",
  193.                     "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6");
  194.             DataInputStream di = new DataInputStream(uc.getInputStream());
  195.             byte[] buffer = new byte[uc.getContentLength()];
  196.             di.readFully(buffer);
  197.             di.close();
  198.             Thread.sleep(250 + (int) Math.random() * 500);
  199.             return new String(buffer);
  200.         }
  201.     }
  202. }