Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package scripts;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Point;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.Reader;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.ArrayList;
- import java.util.Random;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.imageio.ImageIO;
- import org.tribot.api.General;
- import org.tribot.api.Timing;
- import org.tribot.api.input.Mouse;
- import org.tribot.api2007.Banking;
- import org.tribot.api2007.Camera;
- import org.tribot.api2007.ChooseOption;
- import org.tribot.api2007.Combat;
- import org.tribot.api2007.GameTab;
- import org.tribot.api2007.Interfaces;
- import org.tribot.api2007.Inventory;
- import org.tribot.api2007.Login;
- import org.tribot.api2007.NPCs;
- import org.tribot.api2007.Player;
- import org.tribot.api2007.Screen;
- import org.tribot.api2007.Walking;
- import org.tribot.api2007.ext.Doors;
- import org.tribot.api2007.types.RSInterfaceChild;
- import org.tribot.api2007.types.RSItem;
- import org.tribot.api2007.types.RSNPC;
- import org.tribot.api2007.types.RSTile;
- import org.tribot.script.Script;
- import org.tribot.script.ScriptManifest;
- import org.tribot.script.interfaces.Painting;
- /**
- * @author Kevin Spaeth (Sp333th)
- * Buys Eye of Newt from Hetty's shop in Port Sarim
- */
- @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>")
- public class Sp333thNewtFarmer extends Script implements Painting {
- /**
- * True - If the loop is running
- */
- private boolean running;
- /**
- * ID of an Eye of Newt
- */
- private int newtID = 221;
- /**
- * ID of gold baby!
- */
- private int moneyID = 995;
- /**
- * Price of an eye of newt
- */
- private int newtPrice = -1;
- /**
- * The amount of newts the player has collected
- */
- private int newts = 0;
- /**
- * Hetty's (store owner) ID
- */
- private int storeOwnerID = 5659;
- /**
- * The current time (when the bot started)
- */
- private long timeBegan = System.currentTimeMillis();
- /**
- * The location of the door outside Hetty's
- */
- private RSTile doorTile = new RSTile(3017, 3259, 0);
- /**
- * Inside of Hetty's shop
- */
- private RSTile storeTile = new RSTile(3014, 3259, 0);//Inside Hetty's shop
- /**
- * In front of the bank window
- */
- private RSTile bankTile = new RSTile(3092, 3245, 0);//In front of the bank window
- /**
- * Color of the text on the GUI (pink)
- */
- private final Color textColor = new Color(255, 74, 210);
- /**
- * Font of the text on the GUI
- */
- private final Font textFont = new Font("Arial", Font.BOLD, 12);
- /**
- * URL of the GUI background
- */
- private final Image paintImg = getImage("http://s10.postimg.org/3xvvgubp1/Sp333th_Newt_Logo.png");
- /**
- * Path from the bank to the store
- */
- private RSTile[] pathToStore = {
- new RSTile(3092, 3245, 0),//In front of the bank window
- new RSTile(3084, 3249, 0),
- new RSTile(3081, 3260, 0),
- new RSTile(3075, 3269, 0),
- new RSTile(3066, 3275, 0),
- new RSTile(3054, 3275, 0),
- new RSTile(3043, 3274, 0),
- new RSTile(3032, 3272, 0),
- new RSTile(3022, 3265, 0),
- new RSTile(3017, 3259, 0),//Outside Hetty's shop
- };
- @Override
- public void onPaint(Graphics arg0) {
- //Calculate variables to draw.
- long timeRan = System.currentTimeMillis() - timeBegan;
- int goldEarned = newtPrice * newts;
- String gold, newt;
- //Change string if its over 100k
- if(goldEarned >= 100000) {
- goldEarned /= 1000;
- gold = goldEarned + "k";
- } else {
- gold = goldEarned + "";
- }
- //Change string if its over 10k
- if(newts >= 10000) {
- int tempNewts = newts / 1000;
- newt = tempNewts + "k";
- } else {
- newt = newts + "";
- }
- //Set font and text.
- arg0.setFont(textFont);
- arg0.setColor(textColor);
- //Draw paint.
- arg0.drawImage(paintImg, 390, 230, null);
- arg0.drawString("Time Ran: " + Timing.msToString(timeRan), 400, 308);
- arg0.drawString("Eye of Newt: " + newt, 400, 308+12);
- arg0.drawString("Gold Earned: " + gold, 400, 308+12+12);
- }
- @Override
- public void run() {
- if(onStart()) {
- while(running) {
- sleep(loop());
- }
- printReport();
- }
- }
- /**
- * @return true - When the script has finished starting
- */
- private boolean onStart() {
- boolean started = false;
- Walking.setControlClick(active);
- Mouse.setSpeed(150);
- newtPrice = lookup(92) - 3;
- running = true;
- started = true;
- return started;
- }
- /**
- * @return The duration to sleep for
- */
- private int loop() {
- if(hasMoney()) {
- if(needToBank()) {
- if(!atBank()) {
- Walking.walkPath(Walking.invertPath(pathToStore));
- waitUntilIdle();
- }
- bank();
- } else if(!atStore()) {
- Walking.walkPath(pathToStore);
- waitUntilIdle();
- } else {
- if(!isDoorOpen()) {
- openDoor();
- }
- if(atStoreDoor() && !isHettyOnScreen()) {
- Walking.walkTo(storeTile);
- waitUntilIdle();
- }
- if(!isShopOpen()) {
- openShop();
- } else {
- if(getNewts() < 100) {
- worldHop();
- } else {
- buyNewts();
- }
- }
- }
- } else {
- terminate();
- }
- Random rand = new Random();
- return rand.nextInt(1000) + 250;
- }
- /**
- * Waits until the player is idle
- */
- private void waitUntilIdle() {
- while(true) {
- if(!Player.isMoving() && Player.getAnimation() == -1) {
- break;//Break loop if idle
- }
- if(!isShopOpen() && !Banking.isBankScreenOpen() && !Banking.isPinMessageOpen() && !Banking.isPinScreenOpen()) {
- antiBan();
- }
- sleep(100, 250);
- }
- }
- /**
- * This happens when the script ends
- */
- private void terminate() {
- if(!atBank()) {
- Walking.walkPath(Walking.invertPath(pathToStore));
- waitUntilIdle();
- }
- RSItem[] items = Inventory.getAll();
- if(atBank() && items != null & items.length > 0) {
- //If the bank screen isn't open, open it
- if(!Banking.isBankScreenOpen()) {
- Banking.openBankBooth();
- } else {
- //Deposit all items except money
- Banking.depositAll();
- Banking.close();
- }
- }
- running = false;
- }
- private void printReport() {
- //Calculate variables to draw.
- long timeRan = System.currentTimeMillis() - timeBegan;
- int goldEarned = newtPrice * newts;
- println("**********************");
- println("Time Ran: " + Timing.msToString(timeRan));
- println("Eye of Newt: " + newts);
- println("Gold Earned: " + goldEarned);
- println("**********************");
- }
- /**
- * Opens the doors to Hetty's shop
- */
- private void openDoor() {
- if(atStore() && Doors.isDoorAt(doorTile, false) && Doors.getDoorAt(doorTile).isOnScreen()) {
- Doors.handleDoorAt(doorTile, true);
- waitUntilIdle();
- }
- }
- /**
- * Opens Hetty's shop
- */
- private void openShop() {
- RSNPC[] npcs = NPCs.findNearest(storeOwnerID);
- if(atStore() && npcs != null && npcs.length > 0) {
- RSNPC hetty = npcs[0];
- if(hetty != null && hetty.getID() == storeOwnerID && hetty.isOnScreen()) {
- hetty.click("Trade");
- waitUntilIdle();
- }
- }
- }
- /**
- * Buys 10 newts from Hetty
- */
- public void buyNewts() {
- RSInterfaceChild intChild = Interfaces.get(300, 75);
- int index = -1;
- if(isShopOpen()) {
- for(int i = 0; i < intChild.getItems().length; i++) {
- if(intChild.getItems()[i].getID() == newtID) {
- index = i;
- }
- }
- if(index != -1) {
- int freeSpace = 28 - Inventory.getAll().length;
- int itemX = (int) Math.ceil((index) % 8);
- int itemY = (int) ((Math.floor(index) / 8) % 5);
- int y = (itemY * 45) + 82;
- int x = (itemX * 46) + 97;
- Mouse.move(new Point(x, y));
- sleep(200);
- Mouse.click(3);
- if (ChooseOption.isOpen() && ChooseOption.isOptionValid("Buy 10")) {
- ChooseOption.select("Buy 10");
- if(freeSpace >= 10 && getMoney() >= 30) {
- newts += 10;
- } else if(getMoney() >= freeSpace*3){
- newts += freeSpace;
- } else {
- newts += Math.floor(getMoney()/3);
- }
- }
- }
- }
- }
- /**
- * Deposits newts into the player's bank
- */
- private void bank() {
- if(atBank() && Inventory.isFull()) {
- //If the bank screen isn't open, open it
- if(!Banking.isBankScreenOpen()) {
- Banking.openBankBooth();
- } else {
- //Deposit all items except money
- Banking.depositAllExcept(moneyID);
- }
- }
- }
- /**
- * Switches to a world with higher supply
- */
- private void worldHop() {
- int newWorld = General.random(1, 78);
- while(newWorld == 25 || newWorld == 37) {
- newWorld = General.random(1, 78);
- }
- super.setLoginBotState(false);
- super.setRandomSolverState(false);
- worldHop(newWorld, true, false);//random world hop
- super.setLoginBotState(true);
- super.setRandomSolverState(true);
- }
- /**
- * @return Hetty's current stock of newts
- */
- private int getNewts() {
- RSInterfaceChild intChild = Interfaces.get(300, 75);
- int quantity = -1;
- int index = -1;
- if(isShopOpen()) {
- for(int i = 0; i < intChild.getItems().length; i++) {
- if(intChild.getItems()[i].getID() == newtID) {
- index = i;
- }
- }
- if(index != -1) {
- quantity = intChild.getItems()[index].getStack();
- }
- }
- return quantity;
- }
- /**
- * @return The amount of gold the Player has
- */
- private int getMoney() {
- RSItem[] items = Inventory.find(moneyID);
- int gold = -1;
- if(items != null && items.length > 0) {
- RSItem money = items[0];
- if(money != null) {
- gold = money.getStack();
- }
- }
- return gold;
- }
- /**
- * @param url Is the address of the image
- * @return The images hosted at the URL
- */
- private Image getImage(String url) {
- //Try to get the image at the specified URL
- try{
- return ImageIO.read(new URL(url));
- //The following executes if the try block throws an exception
- }catch(IOException e){
- //Although... nothing is done
- return null;
- }
- }
- /**
- * @return true - If the player needs to bank
- */
- private boolean needToBank() {
- boolean needBank = false;
- if(Inventory.isFull()) {
- needBank = true;
- }
- return needBank;
- }
- /**
- * @return true - If the player has at least 3 coins
- */
- private boolean hasMoney() {
- RSItem[] items = Inventory.find(moneyID);
- boolean hasMoney = false;
- if(items != null && items.length > 0) {
- RSItem money = items[0];
- if(money != null && money.getStack() >= 3) {
- hasMoney = true;
- }
- }
- return hasMoney;
- }
- /**
- * @return true - If the play is at the bank
- */
- private boolean atBank() {
- boolean atBank = false;
- if(Player.getPosition().distanceTo(bankTile) <= 3) {
- atBank = true;
- }
- return atBank;
- }
- /**
- * @return true - If the player is at the store
- */
- private boolean atStore() {
- boolean atShop = false;
- if(Player.getPosition().distanceTo(storeTile) <= 3) {
- atShop = true;
- }
- return atShop;
- }
- /**
- * @return true - If the player has Hetty's shop open
- */
- private boolean isShopOpen() {
- return Interfaces.get(300, 75) != null;
- }
- /**
- * @return true - If the door to Hetty's shop is open
- */
- private boolean isDoorOpen() {
- boolean open = true;
- if(atStore() && Doors.isDoorAt(doorTile, false)) {
- open = false;
- }
- return open;
- }
- /**
- * @return true - If the player is at the door of the store
- */
- private boolean atStoreDoor() {
- boolean atDoor = false;
- if(Player.getPosition().distanceTo(doorTile) <= 2) {
- atDoor = true;
- }
- return atDoor;
- }
- /**
- * @return true - If Hetty is on the screen
- */
- private boolean isHettyOnScreen() {
- boolean onScreen = false;
- RSNPC[] npcs = NPCs.findNearest(storeOwnerID);
- if(atStore() && npcs != null && npcs.length > 0) {
- RSNPC hetty = npcs[0];
- if(hetty != null && hetty.getID() == storeOwnerID && hetty.isOnScreen()) {
- onScreen = true;
- }
- }
- return onScreen;
- }
- /**
- * Random movements to prevent getting banned
- */
- private void antiBan() {
- //Randomly perform the Anti-ban.
- int random = General.random(1, 2000) % 200;
- switch(random) {
- case 29:
- //Move the mouse slightly
- println("Anti-Ban: Moving Mouse Slightly.");
- moveMouse(25, 250);
- break;
- case 49:
- //Move the mouse far
- println("Anti-Ban: Moving Mouse Further.");
- moveMouse(250, 1250);
- break;
- case 71:
- //Check the stats
- println("Anti-Ban: Checking Stats.");
- checkStats();
- break;
- case 97:
- //Check the friends list
- println("Anti-Ban: Checking Friends.");
- checkFriends();
- break;
- case 104:
- //Move the camera
- println("Anti-Ban: Moving the Camera.");
- moveCamera();
- break;
- case 126:
- //Move the mouse slightly
- println("Anti-Ban: Moving Mouse Slightly.");
- moveMouse(25, 250);
- break;
- }
- }
- /**
- * Moves the camera around
- */
- private void moveCamera() {
- //Move the camera to a random position
- Camera.setCameraAngle(General.random(25, 75));
- Camera.setCameraRotation(General.random(1, 360));
- //Wait
- sleep(500, 1000);
- //Restore camera Angle
- Camera.setCameraAngle(General.random(80, 90));
- sleep(500, 1000);
- //Restore camera Rotation
- Camera.setCameraRotation(General.random(1, 25));
- }
- /**
- * Checks the player's stats, and then returns to the inventory
- */
- private void checkStats() {
- //Checks the player's stats
- GameTab.open(GameTab.TABS.STATS);
- sleep(500, 1000);
- GameTab.open(GameTab.TABS.INVENTORY);
- sleep(500, 1000);
- }
- /**
- * Check the player's friends and then returns to the inventory
- */
- private void checkFriends() {
- //Checks the player's friends.
- GameTab.open(GameTab.TABS.FRIENDS);
- sleep(500, 1000);
- GameTab.open(GameTab.TABS.INVENTORY);
- sleep(500, 1000);
- }
- /**
- * @param min Is the minimum distance to move the mouse.
- * @param max Is the maximum distance to move the mouse.
- */
- private void moveMouse(int min, int max) {
- //Gets the mouses coordinates
- int mouseX = Mouse.getPos().x;
- int mouseY = Mouse.getPos().y;
- //Randomly generates a distance to move the mouse
- int randomX = General.random(min, max) - ((max-min)/2);
- int randomY = General.random(min, max) - ((max-min)/2);
- //Adds the distance to the coordinates
- Mouse.move(mouseX + randomX, mouseY + randomY);
- }
- /* The following code was written by wastedbro
- * on the Tribot forums. He deserves credit for
- * its use so please don't take it without giving
- * him credit for it!
- */
- /**
- * @author wastedbro
- */
- public void worldHop(int worldNumber, boolean excludeFrees, boolean excludePopular)
- {
- logout(); //Logs out if needed
- while(loggedOut())
- {
- openWorldSelect();
- }
- while(worldSelectOpen())
- {
- selectWorld(worldNumber,excludeFrees,excludePopular);
- }
- }
- /**
- * @author wastedbro
- */
- public void selectWorld(int worldNumber, boolean excludeFrees, boolean excludePopular)
- {
- ArrayList<Point> world = new ArrayList<Point>();
- int counter = 1;
- 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))
- {
- worldNumber = General.random(1, 78);
- if((worldNumber == 1 || worldNumber == 2) && excludePopular)
- {
- continue;
- }
- if((worldNumber == 8 || worldNumber == 16) && excludeFrees)
- {
- continue;
- }
- } //The loops above ensure the world chosen exists and not free/popular depending on the boolean parameters
- for(int columns = 230, y = 1; y < 5 ; columns=columns+95, y++) // Populates the world arrayList with points relative to the world
- {
- for(int rows = 81, x = 1; x < 17; rows=rows+24, x++, counter++)
- {
- 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)
- {
- world.add(new Point(0,0));
- counter++;
- }
- world.add(new Point(columns,rows));
- }
- }
- worldNumber--;
- Mouse.clickBox(world.get(worldNumber).x - 10, world.get(worldNumber).y - 5, world.get(worldNumber).x + 10, world.get(worldNumber).y + 5, 1);
- for(int x = 0; x < 6; x++) //Waits up to 3 seconds for the login screen. Checks every half a second (Approx.)
- {
- if(loggedOut())
- {
- break;
- }
- sleep(490,510);
- }
- }
- /**
- * @author wastedbro
- */
- public boolean worldSelectOpen()
- {
- if (Login.getLoginState() == Login.STATE.LOGINSCREEN)
- {
- Color black = new Color(0,0,0);
- return (Screen.getColorAt(100, 200).equals(black));
- }
- return false;
- }
- /**
- * @author wastedbro
- */
- public boolean openWorldSelect()
- {
- if (Login.getLoginState() == Login.STATE.LOGINSCREEN)
- {
- Mouse.clickBox(10, 465, 100, 495, 1);
- for(int x = 0; x < 10; x++)
- {
- if(worldSelectOpen())
- {
- return true;
- }
- sleep(490,510);
- }
- }
- return false;
- }
- /**
- * @author wastedbro
- */
- public boolean loggedOut()
- {
- return (Login.getLoginState() == Login.STATE.LOGINSCREEN) && !(Screen.getColorAt(100, 200).equals(new Color(0,0,0)));
- }
- /**
- * @author wastedbro
- */
- public boolean logout()
- {
- if (Login.getLoginState() == Login.STATE.INGAME)
- {
- Login.logout();
- for(int x = 0; x < 6; x++)
- {
- if(loggedOut())
- {
- return true;
- }
- sleep(490,510);
- }
- }
- return false;
- }
- /* The following code was writting by Administrator
- * on the Tribot forums. Give him/her credit for its
- * successes and failures.
- */
- private static final String LINK = "http://forums.zybez.net/pages/2007-price-guide?id=";
- 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";
- private final static Pattern NAME = Pattern.compile("Price Guide: (.+?)</h1>");
- private final static Pattern PRICE = Pattern.compile("Recent Trade Price: ~(.+?) GP");
- /**
- * @author Administrator
- */
- public final static int lookup(final int id) {
- try {
- final BufferedReader br = getReader(id);
- br.skip(18700);
- Matcher m = NAME.matcher(br.readLine());
- @SuppressWarnings("unused")
- String name = "";
- if (m.find()) {
- name = m.group(1);
- }
- String line = "";
- while (!line.contains("Recent Trade Price")) {
- line = br.readLine();
- }
- m = PRICE.matcher(line);
- int price = 0;
- if (m.find()) {
- price = Integer.parseInt(m.group(1).replaceAll(",", ""));
- }
- return price;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return -1;
- }
- /**
- * @author Administrator
- */
- private static BufferedReader getReader(final int id) {
- try {
- final URL url = new URL(LINK + id);
- final URLConnection con = url.openConnection();
- con.setRequestProperty("User-Agent", USER_AGENT);
- final Reader inr = new InputStreamReader(con.getInputStream());
- return new BufferedReader(inr);
- } catch (IOException e) {
- System.out.println("Item not found.");
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement