import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import org.powerbot.concurrent.Task;
import org.powerbot.concurrent.strategy.Strategy;
import org.powerbot.game.api.ActiveScript;
import org.powerbot.game.api.Manifest;
import org.powerbot.game.api.methods.Calculations;
import org.powerbot.game.api.methods.Walking;
import org.powerbot.game.api.methods.interactive.Players;
import org.powerbot.game.api.methods.node.GroundItems;
import org.powerbot.game.api.util.Time;
import org.powerbot.game.api.wrappers.Tile;
import org.powerbot.game.api.wrappers.node.GroundItem;
import org.powerbot.game.bot.event.listener.PaintListener;
import java.io.DataInputStream;
import java.net.URL;
import java.net.URLConnection;
import org.powerbot.game.api.methods.tab.Inventory;
import org.powerbot.game.api.util.Random;
/**
*
* @author iJava
*/
@Manifest(authors = "iJava", name = "iSwampTar", version = 1.0, description = "Collects Swamp Tar Anywhere.")
public class iSwampTar extends ActiveScript implements PaintListener {
private final int SWAMP_TAR_ID = 1939;
private long millis, minutes, hours, seconds, startTime;
private String status, mins, secs = "";
private int tarCollected, tarCollectedPH, gpEarned, gpEarnedPH, swampTarPrice = 0;
private Image paintBackground;
@Override
protected void setup() {
try {
provide(new Sleeper());
provide(new CollectTar());
provide(new WalkToTar());
startTime = System.currentTimeMillis();
status = "Starting Up";
swampTarPrice = GrandExchange.getCurrentGuidePrice(SWAMP_TAR_ID);
paintBackground = ImageIO.read(new URL("http://img440.imageshack.us/img440/7360/iswamptar.png"));
} catch (IOException ex) {
Logger.getLogger(iSwampTar.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(iSwampTar.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void onRepaint(Graphics g1) {
tarCollectedPH = (int) ((tarCollected) * 3600000D / (System.currentTimeMillis() - startTime));
gpEarned = swampTarPrice * tarCollected;
gpEarnedPH = (int) ((gpEarned) * 3600000D / (System.currentTimeMillis() - startTime));
millis = System.currentTimeMillis() - startTime;
hours = millis / (1000 * 60 * 60);
millis -= hours * (1000 * 60 * 60);
minutes = millis / (1000 * 60);
mins = "" + minutes;
while (mins.length() < 2) {
mins = "0" + mins;
}
millis -= minutes * (1000 * 60);
seconds = millis / 1000;
secs = "" + seconds;
while (secs.length() < 2) {
secs = "0" + secs;
}
Graphics2D g = (Graphics2D) g1;
g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF));
g.drawImage(paintBackground, 0, 340, null);
g.setColor(new Color(0, 0, 0));
g.setFont(new Font("Arial", 0, 14));
g.drawString("" + gpEarnedPH, 362, 415);
g.drawString("" + gpEarned, 332, 398);
g.drawString("" + hours + " : " + mins + " : " + secs, 355, 432);
g.drawString("" + tarCollected, 126, 417);
g.drawString("" + tarCollectedPH, 173, 433);
g.drawString("" + status, 85, 402);
}
private class Sleeper extends Strategy implements Task {
@Override
public boolean validate() {
return Players.getLocal().isMoving();
}
@Override
public void run() {
Time.sleep(300, 800);
}
}
private class CollectTar extends Strategy implements Task {
@Override
public boolean validate() {
GroundItem swampTar = GroundItems.getNearest(SWAMP_TAR_ID);
if (swampTar == null) {
return false;
}
return swampTar.isOnScreen();
}
@Override
public void run() {
status = "Collecting Tar";
GroundItem swampTar = GroundItems.getNearest(SWAMP_TAR_ID);
if (swampTar != null && swampTar.isOnScreen()) {
int temp = Inventory.getItem(SWAMP_TAR_ID).getStackSize();
swampTar.interact("Take");
Time.sleep(300, 500);
int temp2 = Inventory.getItem(SWAMP_TAR_ID).getStackSize();
if (temp != temp2) {
tarCollected++;
}
}
}
}
private class WalkToTar extends Strategy implements Task {
@Override
public boolean validate() {
GroundItem swampTar = GroundItems.getNearest(SWAMP_TAR_ID);
if (swampTar == null) {
return false;
}
return !swampTar.isOnScreen();
}
@Override
public void run() {
status = "Finding Tar";
if (!Walking.isRunEnabled() && Walking.getEnergy() > Random.nextInt(25, 50)) {
Walking.setRun(true);
}
GroundItem[] swampTars = GroundItems.getLoaded(SWAMP_TAR_ID);
Tile swampTarLoc = swampTars[0].getLocation();
for (GroundItem swampTar : swampTars) {
if (Calculations.distanceTo(swampTarLoc) > Calculations.distanceTo(swampTar)) {
swampTarLoc = swampTar.getLocation();
}
}
if (swampTarLoc.isOnMap()) {
while (Calculations.distanceTo(swampTarLoc) > 3) {
Walking.walk(swampTarLoc);
}
}
}
}
public static class GrandExchange {
private static String[] currGuidePriceData;
public static int getCurrentGuidePrice(int itemID) throws IOException, InterruptedException {
URL url = new URL("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + itemID);
String html = readPage(url, "http://google.com");
currGuidePriceData = html.split("\"current\":\\{\"trend\":\"neutral\",\"price\":|\\},\"today\":\\{\"trend\"");
return Integer.parseInt(currGuidePriceData[1]);
}
public static String readPage(URL url, String referer) throws IOException,
InterruptedException {
URLConnection uc = url.openConnection();
uc.addRequestProperty(
"Accept",
"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
uc.addRequestProperty("Accept-Charset",
"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
uc.addRequestProperty("Accept-Encoding", "gzip,deflate");
uc.addRequestProperty("Accept-Language", "en-gb,en;q=0.5");
uc.addRequestProperty("Connection", "keep-alive");
uc.addRequestProperty("Host", "www.runescape.com");
uc.addRequestProperty("Keep-Alive", "300");
if (referer != null && !referer.trim().equals("")) {
uc.addRequestProperty("Referer", referer);
}
uc.addRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6");
DataInputStream di = new DataInputStream(uc.getInputStream());
byte[] buffer = new byte[uc.getContentLength()];
di.readFully(buffer);
di.close();
Thread.sleep(250 + (int) Math.random() * 500);
return new String(buffer);
}
}
}