All pastes #2065939 Raw Edit

BarbVillageFisher

public text v1 · immutable
#2065939 ·published 2011-05-20 18:43 UTC
rendered paste body
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;
import javax.swing.border.EmptyBorder;

import org.rsbot.event.events.MessageEvent;
import org.rsbot.event.listeners.MessageListener;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Game.ChatMode;
import org.rsbot.script.methods.Players;
import org.rsbot.script.methods.Skills;
import org.rsbot.script.util.Timer;
import org.rsbot.script.wrappers.RSArea;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSTile;
import org.rsbot.script.wrappers.RSTilePath;

@ScriptManifest(authors = "Robert G", name = "BarbVillageFisher", version = 1.2, description = "Fishes and bank's")
public class BarbVillageFisher extends Script implements PaintListener,
MessageListener, MouseListener {
	ScriptManifest props = getClass().getAnnotation(ScriptManifest.class);

	private int[] fishingGear = { 314, 313, 307, 309 };
	private int featherID = 314;
	private int fishingBaitID = 313;
	private int fishingSpotID = 328;
	private int[] rodID = { 307, 309 };
	private int bait;
	private Timer timer;
	RSTile destination = new RSTile(3104, 3429);
	String addZero = "", addZero2 = "";
	public long startTime = System.currentTimeMillis();
	long runTime, seconds, minutes = 0, hours = 0;
	long runTime2, seconds2, minutes2 = 0, hours2 = 0;
	private boolean chatOff = false;
	private boolean powerFishing = false;
	private int expGained, startExp, startLVL, gainedLVL, currentLVL, TNL,
	expPH;
	private long timeTNL;
	private int fish = 0;
	private int fishPH;
	private String status = "Loading..";
	private String catchOption;
	Rectangle close = new Rectangle(6, 344, 506, 131);
	Point p;
	boolean hide = false;

	public final RSTile[] banktiles = { new RSTile(3102, 3426),
			new RSTile(3104, 3431), new RSTile(3101, 3435),
			new RSTile(3097, 3438), new RSTile(3097, 3443),
			new RSTile(3096, 3448), new RSTile(3094, 3453),
			new RSTile(3090, 3457), new RSTile(3088, 3462),
			new RSTile(3084, 3465), new RSTile(3080, 3469),
			new RSTile(3079, 3474), new RSTile(3081, 3479),
			new RSTile(3081, 3484), new RSTile(3085, 3488),
			new RSTile(3089, 3491), new RSTile(3094, 3490) };
	RSTilePath pathToBank;

	RSArea bank_area = new RSArea(new RSTile(3090, 3487),
			new RSTile(3099, 3499));
	RSArea spot_area = new RSArea(new RSTile(3098, 3422),
			new RSTile(3108, 3435));

	private boolean guiWait = true;
	BarbFisherGui g = new BarbFisherGui();

	@Override
	public boolean onStart() {
		startTime = System.currentTimeMillis();
		startExp = skills.getCurrentExp(Skills.FISHING);
		startLVL = skills.getCurrentLevel(Skills.FISHING);
		pathToBank = walking.newTilePath(banktiles);
		timer = new Timer(1800000);
		g.setVisible(true);
		while (guiWait || g.isVisible()) {
			sleep(100);
		}
		return true;
	}

	private boolean atBank() {
		if (bank_area.contains(getMyPlayer().getLocation())) {
			return true;
		}
		return false;
	}

	private void walk2bank() {
		pathToBank.traverse();
	}

	private void walk2spot() {
		pathToBank.reverse();
		pathToBank.traverse();
		pathToBank.reverse();
	}

	private boolean atSpot() {
		RSNPC spot = npcs.getNearest(fishingSpotID);
		if (spot != null) {
			return true;
		}
		return false;
	}

	private void fish() {
		status = "Catching fish.";
		RSNPC spot = npcs.getNearest(fishingSpotID);
		if (spot != null) {
			if (!spot.isOnScreen()) {
				camera.setPitch(false);
				camera.turnTo(spot);
				walking.walkTo(spot.getLocation());
			}
			if (spot.isOnScreen()) {
				spot.doAction(catchOption);
				sleep(random(1000, 1500));
			}
		}
	}

	private void doBank() {
		status = "Banking";
		if (!bank.open()) {
			bank.open();
			sleep(random(1200, 1400));
		}
		if (bank.open()) {
			bank.depositAllExcept(fishingGear);
			sleep(random(1000, 1200));
		}
		bank.close();
	}

	@Override
	public void messageReceived(MessageEvent e) {
		String txt = e.getMessage().toLowerCase();
		if (txt.contains("salmon")) {
			fish++;
		}
		if (txt.contains("trout")) {
			fish++;
		}
		if (txt.contains("pike")) {
			fish++;
		}
		if (txt.contains("herring")) {
			fish++;
		}
	}

	public void antiban() {

		int b = random(0, 15);
		switch (b) {
		case 1:
			if (random(0, 10) == 5) {
				log("[Antiban] move mouse");
				mouse.moveSlightly();
				sleep(2000, 6000);
				mouse.moveRandomly(1500, 3500);
			}
			break;
		case 2:
			if (random(0, 13) == 2) {
				log("[Antiban] Turn screen");
				camera.setAngle(random(30, 130));
				sleep(400, 800);

			}
			break;
		case 3:
			if (random(0, 11) == 6) {
				log("[Antiban] turn screen");
				camera.setAngle(random(150, 270));
				sleep(random(1200, 1500));
			}
			break;
		case 4:
			if (random(0, 18) == 3) {
				log("[antiban] Checking Xp.");
				game.openTab(2);
				skills.doHover(Skills.INTERFACE_FISHING);
				sleep(random(2100, 3400));
			}
			break;
		case 5:
			if (random(0, 22) == 4) {
				log("[Antiban] Checking friend's.");
				game.openTab(Game.TAB_FRIENDS);
				sleep(random(3000, 3400));
			}
			break;
		case 6:
			if (random(0, 22) == 1) {
				if (!chatOff) {
					if (timer.getElapsed() == 900000) {
						log("[Antiban] Turning public chat off.");
						game.openTab(Game.CHAT_OPTION);
						if (!game.setChatOption(Game.CHAT_OPTION_PUBLIC,
								ChatMode.OFF))
							game.setChatOption(Game.CHAT_OPTION_PUBLIC,
									ChatMode.OFF);
						chatOff = true;
						sleep(random(3000, 3400));
					}
				}
			}
			break;
		case 7:
			if (random(0, 22) == 9) {
				log("[Antiban] Looking at music.");
				game.openTab(Game.TAB_MUSIC);
				sleep(random(3000, 3400));
			}
			break;
		case 8:
			try {
				if (random(0, 18) == 7) {
					log("[Antiban] Hovering random player.");
					mouse.setSpeed(random(12, 18));
					players.getNearest(Players.ALL_FILTER).hover();
					mouse.click(false);
					sleep(random(1400, 1800));
					mouse.moveRandomly(150, 350);
					sleep(random(2200, 2400));
				}
			} catch (Exception e) {
				log("[Antiban] No other player's on screen.");
			}
			break;
		case 9:
			if (random(0, 22) == 1) {
				if (chatOff && timer.getRemaining() == 500) {
					log("[Antiban] Turning public chat on.");
					game.openTab(Game.CHAT_OPTION);
					game.setChatOption(Game.CHAT_OPTION_PUBLIC, ChatMode.ON);
					chatOff = false;
					sleep(random(3000, 3400));
				}
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		p = e.getPoint();
		if (close.contains(p) && !hide) {
			hide = true;
		} else if (close.contains(p) && hide) {
			hide = false;
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public int loop() {
		if (timer.getElapsed() == 1800000) {
			timer.reset();
		}
		if (!game.isLoggedIn()) {
			sleep(1000);
		}
		try {
			mouse.setSpeed(random(10, 12));
			if (interfaces.canContinue()) {
				interfaces.clickContinue();
			}
			if (players.getMyPlayer().getAnimation() != -1) {
				antiban();
			}
			if (atSpot() && !inventory.contains(bait)) {
				if (game.getCurrentTab() == Game.TAB_INVENTORY) {
					sleep(10000);
					log.severe("Out of bait/feather's!");
					stopScript();
				}
			}
			if (atSpot() && players.getMyPlayer().getAnimation() == -1
					&& !inventory.isFull()) {
				if (inventory.containsOneOf(rodID) && inventory.contains(bait)
						&& !players.getMyPlayer().isMoving()) {
					fish();
				}
				return 0;
			}
			if (powerFishing) {
				if (!atSpot()) {
					walking.getPath(destination).traverse();
				}
			}
			if (powerFishing) {
				if (inventory.isFull())
					inventory.dropAllExcept(fishingGear);
				return 0;
			}
			if (!inventory.isFull() && !atSpot()) {
				status = "Walking.";
				walk2spot();
				return 0;
			}
			if (!powerFishing) {
				if (atBank() && inventory.isFull()) {
					doBank();
					return 0;
				}
				if (!atBank() && inventory.isFull()) {
					status = "Walking.";
					walk2bank();
					return 0;
				}
			}
		} catch (Exception e) {
		}
		return 0;
	}

	@Override
	public void onFinish() {
		env.saveScreenshot(true);
		log("Thank you for using " + props.name() + " v" + props.version()
				+ "!");
		log("You gained " + gainedLVL + " fishing level's.");
	}

	private String formatTime(final long time) {
		final int sec = (int) (time / 1000), h = sec / 3600, m = sec / 60 % 60, s = sec % 60;
		return (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":"
		+ (s < 10 ? "0" + s : s);
	}

	private Image getImage(String url) {
		try {
			return ImageIO.read(new URL(url));
		} catch (IOException e) {
			return null;
		}
	}

	private final Color color1 = new Color(0, 0, 0);
	private final BasicStroke stroke1 = new BasicStroke(1);

	private final Font font1 = new Font("Georgia", 1, 12);

	private final Image img1 = getImage("http://img24.imageshack.us/img24/43/cooltext518447301.png");
	private final Image img2 = getImage("http://images4.wikia.nocookie.net/__cb20080501002738/runescape/images/archive/3/34/20080919211925%21Fishing_cape.PNG");
	private final Image img3 = getImage("http://img233.imageshack.us/img233/7172/coollogocom114933586.png");

	@Override
	public void onRepaint(Graphics g1) {
		Graphics2D g = (Graphics2D) g1;
		if (game.isLoggedIn()) {
			if (!hide) {
				g.drawImage(img1, -2, 337, null);
				g.drawImage(img2, 459, 357, null);
				g.drawImage(img3, 15, 5, null);
				g.setFont(font1);
				g.setColor(color1);
				g.drawString("Run Time: " + hours2 + ":" + addZero + minutes2
						+ ":" + addZero2 + seconds2, 20, 370);
				g.drawString("Start Level: " + startLVL, 20, 385);
				g.drawString("Current Level: " + currentLVL, 20, 400);
				g.drawString("Level's Gained: " + gainedLVL, 20, 415);
				g.drawString("XP Gained: " + expGained, 20, 430);
				g.drawString("XP / PH: " + expPH, 220, 370);
				g.drawString("Time / TNL: " + formatTime(timeTNL), 220, 385);
				g.drawString("Fish Caught: " + fish, 220, 400);
				g.drawString("Fish / PH: " + fishPH, 220, 415);
				g.drawString("Status: " + status, 220, 430);

				// percent bar.
				final int percent = skills
				.getPercentToNextLevel(Skills.FISHING);
				int length = (percent * 436) / 100;
				// red bar
				g.setColor(Color.red);
				g.fillRect(20, 435, 436, 20);
				g.setColor(color1);
				g.setStroke(stroke1);
				g.drawRect(20, 435, 436, 20);
				// green bar
				g.setColor(Color.green);
				g.fillRect(20, 435, +length, 20);
				g.setColor(color1);
				g.setStroke(stroke1);
				g.drawRect(20, 435, +length, 20);
				g.drawString(percent + "% (" + TNL + " XP / TNL)", 168, 449);

				// mouse paint
				g.setColor(Color.black);
				g.drawLine(0, (int) (mouse.getLocation().getY()) + 1, 800,
						(int) (mouse.getLocation().getY()) + 1);
				g.drawLine((int) (mouse.getLocation().getX()) + 1, 0,
						(int) (mouse.getLocation().getX()) + 1, 800);

				runTime = System.currentTimeMillis() - startTime;
				runTime2 = System.currentTimeMillis() - startTime;
				seconds2 = runTime2 / 1000; // seconds
				if (seconds2 >= 60) { // minutes
					minutes2 = seconds2 / 60;
					seconds2 -= (minutes2 * 60);
				}
				if (minutes2 >= 60) { // hours
					hours2 = minutes2 / 60;
					minutes2 -= (hours2 * 60);
				}

				// adds zero before seconds
				if (seconds2 >= 10) {
					addZero2 = "";
				} else if (seconds2 < 10) {
					addZero2 = "0";
				}

				timeTNL = (long) ((double) TNL / (double) expPH * 3600000);
				fishPH = (int) (3600000.0 / runTime2 * fish);
				expPH = (int) (3600000.0 / runTime2 * expGained);
				expGained = skills.getCurrentExp(Skills.FISHING) - startExp;
				currentLVL = skills.getCurrentLevel(Skills.FISHING);
				gainedLVL = skills.getCurrentLevel(Skills.FISHING) - startLVL;
				TNL = skills.getExpToNextLevel(Skills.FISHING);

			}
			if (hide) {
			}
		}
	}
		/**
		 * @author Robert G
		 */
		public class BarbFisherGui extends JFrame {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			public BarbFisherGui() {
				initComponents();
			}

			private void okButtonActionPerformed(ActionEvent e) {
				  String chosen = fishSelected.getSelectedItem().toString().toLowerCase();
		          if(chosen.contains("lure")) {
		        	  catchOption = "Lure";
		        	  bait = featherID;
		          }else if(chosen.contains("bait")) {
		        	  catchOption = "Bait";
		        	  bait = fishingBaitID;
		          }
		          if (checkBox1.isSelected()) {
		        	  powerFishing = true;
		          }
		          guiWait = false;
		          g.dispose();
			}

			private void thisWindowClosed(WindowEvent e) {
				
			}

			private void initComponents() {
				// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
				// Generated using JFormDesigner Evaluation license - Robert G
				dialogPane = new JPanel();
				contentPanel = new JPanel();
				label1 = new JLabel();
				label2 = new JLabel();
				fishSelected = new JComboBox();
				label3 = new JLabel();
				checkBox1 = new JCheckBox();
				buttonBar = new JPanel();
				okButton = new JButton();

				//======== this ========
				addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosed(WindowEvent e) {
						thisWindowClosed(e);
					}
				});
				Container contentPane = getContentPane();
				contentPane.setLayout(new BorderLayout());

				//======== dialogPane ========
				{
					dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));

					// JFormDesigner evaluation mark
					dialogPane.setBorder(new javax.swing.border.CompoundBorder(
						new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
							"", javax.swing.border.TitledBorder.CENTER,
							javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
							java.awt.Color.red), dialogPane.getBorder())); dialogPane.addPropertyChangeListener(new java.beans.PropertyChangeListener(){@Override
							public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});

					dialogPane.setLayout(new BorderLayout());

					//======== contentPanel ========
					{

						//---- label1 ----
						label1.setText("Welcome to Robert G's BarbarianVillageFisher!");
						label1.setFont(new Font("Georgia", Font.BOLD, 12));

						//---- label2 ----
						label2.setText("Select fishing style:");

						//---- fishSelected ----
						fishSelected.setModel(new DefaultComboBoxModel(new String[] {
							"Lure",
							"Bait"
						}));

						//---- label3 ----
						label3.setText("By default the script will bank ");

						//---- checkBox1 ----
						checkBox1.setText("tick here for powerfishing mode.");

						GroupLayout contentPanelLayout = new GroupLayout(contentPanel);
						contentPanel.setLayout(contentPanelLayout);
						contentPanelLayout.setHorizontalGroup(
							contentPanelLayout.createParallelGroup()
								.addGroup(contentPanelLayout.createSequentialGroup()
									.addGroup(contentPanelLayout.createParallelGroup()
										.addComponent(label1)
										.addGroup(contentPanelLayout.createSequentialGroup()
											.addComponent(label2)
											.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
											.addComponent(fishSelected, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
										.addGroup(contentPanelLayout.createSequentialGroup()
											.addComponent(label3)
											.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
											.addComponent(checkBox1)))
									.addContainerGap(36, Short.MAX_VALUE))
						);
						contentPanelLayout.setVerticalGroup(
							contentPanelLayout.createParallelGroup()
								.addGroup(contentPanelLayout.createSequentialGroup()
									.addComponent(label1)
									.addGap(40, 40, 40)
									.addGroup(contentPanelLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
										.addComponent(label2)
										.addComponent(fishSelected, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addGap(38, 38, 38)
									.addGroup(contentPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
										.addComponent(label3)
										.addComponent(checkBox1))
									.addContainerGap(77, Short.MAX_VALUE))
						);
					}
					dialogPane.add(contentPanel, BorderLayout.WEST);

					//======== buttonBar ========
					{
						buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
						buttonBar.setLayout(new GridBagLayout());
						((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 80};
						((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0};

						//---- okButton ----
						okButton.setText("OK");
						okButton.addActionListener(new ActionListener() {
							@Override
							public void actionPerformed(ActionEvent e) {
								okButtonActionPerformed(e);
							}
						});
						buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.BOTH,
							new Insets(0, 0, 0, 0), 0, 0));
					}
					dialogPane.add(buttonBar, BorderLayout.SOUTH);
				}
				contentPane.add(dialogPane, BorderLayout.CENTER);
				pack();
				setLocationRelativeTo(getOwner());
				// JFormDesigner - End of component initialization  //GEN-END:initComponents
			}

			// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
			// Generated using JFormDesigner Evaluation license - Robert G
			private JPanel dialogPane;
			private JPanel contentPanel;
			private JLabel label1;
			private JLabel label2;
			private JComboBox fishSelected;
			private JLabel label3;
			private JCheckBox checkBox1;
			private JPanel buttonBar;
			private JButton okButton;
			// JFormDesigner - End of variables declaration  //GEN-END:variables
		}
}