Created
August 17, 2012 18:25
-
-
Save CalebWhiting/3381329 to your computer and use it in GitHub Desktop.
DIYMiner.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* DIYMiner.java | |
* | |
* @author Caleb/JDK_7 | |
* | |
* Feed free to use, modify and learn from this code (That is what it is made for), | |
* all that I ask is that you don't redistribute any of my code without giving appropriate | |
* credits. | |
* | |
* Thank you, Caleb. | |
* | |
* Side note: This isn't in the Ultra name since its for learning purposes and I believe it is not the | |
* same quality as you should expect in Ultra scripts. | |
* | |
* As I have said before, this is for learning purposes, so if you are actually looking for a good mining script | |
* then I would suggest something else such as AMiner, kMiner etc. | |
* | |
* INSTRUCTIONS FOR USE: | |
* | |
* Change the "ROCK_IDS" variable to the rock ID's that you wish to mine. | |
* Change the "ITEMS_TO_DROP" variable to the item ID's that you wish to drop (Ore, gems etc) | |
* | |
* As far as I'm aware there are no good object ID database's, although for item ID's I would | |
* suggest itemdb.biz if you don't want to get them manually. | |
* | |
* Learn & Enjoy! | |
*/ | |
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.Walking; | |
import org.powerbot.game.api.methods.input.Mouse; | |
import org.powerbot.game.api.methods.interactive.Players; | |
import org.powerbot.game.api.methods.node.SceneEntities; | |
import org.powerbot.game.api.methods.tab.Inventory; | |
import org.powerbot.game.api.methods.tab.Skills; | |
import org.powerbot.game.api.methods.widget.Camera; | |
import org.powerbot.game.api.util.Filter; | |
import org.powerbot.game.api.util.Random; | |
import org.powerbot.game.api.util.Time; | |
import org.powerbot.game.api.util.Timer; | |
import org.powerbot.game.api.wrappers.node.Item; | |
import org.powerbot.game.api.wrappers.node.SceneObject; | |
import org.powerbot.game.bot.event.MessageEvent; | |
import org.powerbot.game.bot.event.listener.MessageListener; | |
import org.powerbot.game.bot.event.listener.PaintListener; | |
import java.awt.*; | |
import java.text.DecimalFormat; | |
import java.util.ArrayList; | |
@Manifest(name = "D.I.Y Miner", authors = {"Powerbot Community"}, version = 0.1, description = "Mines ores") | |
public class DIYMiner extends ActiveScript implements PaintListener, MessageListener { | |
private static final int[] ROCK_IDS = {}; | |
// each npc, object, item etc has its own individual ID | |
private static final int[] ITEMS_TO_DROP = {}; | |
// the items to drop when the inventory is full (ores, gems, mystery box etc) | |
private static DecimalFormat intFormat; | |
// this will be used to correctly format int values in the paint (Example: 100000 = 100,000) | |
private long startTime; | |
// this will be used in the paint to display runtime | |
private static int startXp; | |
// this will be used to calculate the amount of experience gained in the paint | |
private static Manifest manifest; | |
// this can be used for getting things such as script name, authors, version etc | |
// see onRepaint(Graphics) for explanation | |
private static int oresMined = 0; | |
// this will be used in the paint to display how many ores have been mined | |
public static Action current = null; | |
// this will be used to display "status" in the paint | |
public int getTotalExperience() { | |
int xp = 0; | |
for (int x = 0; x <= 24; x++) { | |
// adds the total amount of experience in 'x' skill to the total amount | |
// of overall experience | |
xp += Skills.getExperience(x); | |
} | |
return xp; | |
} | |
@Override | |
protected void setup() { | |
manifest = getClass().getAnnotation(Manifest.class); | |
// we are setting the manifest variable | |
startXp = getTotalExperience(); | |
// we are setting the startXp variable | |
intFormat = new DecimalFormat("#,###"); | |
// we are setting the intFormat variable | |
startTime = System.currentTimeMillis(); | |
// we are setting the startTime variable | |
provide(new Drop()); | |
// this adds the Drop action to the scripts actions | |
// without this our Drop class will never be ran. | |
provide(new Mine()); | |
// this adds the Mine action to the scripts actions | |
// without this our Mine class will never be ran. | |
} | |
@Override | |
public void onRepaint(final Graphics graphics) { | |
// this method is inherited from the PaintListener interface, this will draw to the | |
// canvas | |
final ArrayList<String> strings = new ArrayList<String>(); | |
strings.add(manifest.name() + " [v" + manifest.version() + "]"); | |
strings.add(manifest.description()); | |
// so far we have added the name, version and description to the array of strings to paint | |
final StringBuilder builder = new StringBuilder("Authors: "); | |
for (final String author : manifest.authors()) { | |
// StringBuilders are a good alternative to doing stuff like... | |
// string = string + "bla"; etc | |
builder.append(author + " & "); | |
} | |
final String authors = builder.substring(0, builder.length() - 3); // trim the " & " at the end | |
strings.add(authors); | |
// added authors to the paint | |
strings.add("Runtime: " + Time.format(System.currentTimeMillis() - startTime)); | |
// added runtime to the paint | |
strings.add("Status: " + (current == null ? "Idle" : current.getClass().getSimpleName())); | |
// added status to the paint | |
strings.add("Experience: " + intFormat.format(getTotalExperience() - startXp)); | |
// added experience to the paint | |
strings.add("Ores mined: " + intFormat.format(oresMined)); | |
// added ores mined to the paint | |
graphics.setFont(new Font("Tahoma", Font.BOLD, 9)); | |
graphics.setColor(Color.GREEN); | |
// we have set the font to Tahoma (Bold) with a size of 9 | |
// and the color to green (r: 0, g: 255, b: 0) | |
if (strings.size() > 0) { | |
int y = 70; | |
for (final String string : strings) { | |
graphics.drawString(string, 10, y += 15); | |
} | |
} | |
// mouse paint | |
final boolean pressed = System.currentTimeMillis() - Mouse.getPressTime() < 1500; | |
final Point mouse = Mouse.getLocation(); | |
// if the mouse was pressed under 1500 ms ago then set the color to red; otherwise green | |
graphics.setColor(pressed ? Color.RED : Color.GREEN); | |
graphics.fillOval(mouse.x - 3, mouse.y - 3, 6, 6); | |
// mouse border | |
graphics.setColor(Color.BLACK); | |
graphics.drawOval(mouse.x - 3, mouse.y - 3, 6, 6); | |
} | |
@Override | |
public void messageReceived(MessageEvent messageEvent) { | |
final String message = messageEvent.getMessage(); | |
if (message.contains("You manage to mine")) { | |
// if a server message is recived and it contains "You manage to mine" then | |
// add 1 to the oresMined variable | |
oresMined += 1; | |
} | |
} | |
private class Mine extends Action { | |
public SceneObject getRock() { | |
// gets the closest rock | |
return SceneEntities.getNearest(ROCK_IDS); | |
} | |
@Override | |
public void execute() throws Exception { | |
// this method is inherited from the Action superclass | |
// this will be where our mining action happens | |
final SceneObject rock = getRock(); | |
// we make this final to tell java that this is not going to change, | |
// without final it would get the rock every time we used the rock variable | |
if (rock != null) { | |
// if rock is null, and we try to do anything with it, then we will get a | |
// NullPointerException (NPE) | |
if (rock.isOnScreen() && rock.interact("Mine")) { | |
final Timer timer = new Timer(Random.nextInt(4000, 5000)); | |
// wait for the player to start mining | |
while (timer.isRunning() && Players.getLocal().getAnimation() == -1) ; | |
if (Players.getLocal().getAnimation() != -1) { | |
// reset the timer to avoid waiting for the player to finish mining when they aren't | |
timer.reset(); | |
} | |
final int start = Inventory.getCount(); | |
while (timer.isRunning() && Inventory.getCount() == start && rock.validate()) { | |
// wait for the timer to run out, or to gain an item or for the rock to not be valid | |
if (Players.getLocal().getAnimation() != -1) { | |
// if mining, then reset the timer. | |
timer.reset(); | |
} | |
} | |
} else { | |
// The rock is not on screen, or we failed to interact with the rock | |
Camera.turnTo(rock); | |
// try turning the camera to the rock first, just incase it was the interaction that failed | |
// rather than it not being on screen | |
Time.sleep(50, 150); | |
if (!rock.isOnScreen()) { | |
Walking.walk(rock); | |
final Timer timer = new Timer(Random.nextInt(1000, 2000)); | |
while (timer.isRunning() && !Players.getLocal().isMoving()) ; | |
// it can take a while for the player to start moving, without this it could "spam walk" | |
if (!Players.getLocal().isMoving()) { | |
// check we are moving before we wait for it to be on screen to | |
// avoid it waiting for no reason | |
return; | |
} | |
timer.setEndIn(Random.nextInt(3000, 5000)); | |
// reset the timer to avoid making a whole new Timer | |
while (timer.isRunning() && !rock.isOnScreen() && Players.getLocal().isMoving()) { | |
// This will loop until the timer has ran out or the rock is on the screen | |
Time.sleep(10, 50); | |
} | |
} | |
} | |
} | |
} | |
@Override | |
public boolean isValid() throws Exception { | |
// this method is inherited from the Action superclass | |
// this will return if execute() should be called | |
return !Inventory.isFull(); | |
} | |
} | |
private class Drop extends Action { | |
public Item[] getItems(final int... ids) { | |
// Inventory.getItems(Filter<Item>) explanation... | |
// all of the items in the inventory are checked | |
// and if filter.accept(Item) returns true, then the item is added | |
// to the array of items to return | |
return Inventory.getItems(new Filter<Item>() { | |
@Override | |
public boolean accept(final Item item) { | |
for (final int i : ids) { | |
if (item.getId() == i) { | |
return true; | |
} | |
} | |
return false; | |
} | |
}); | |
} | |
@Override | |
public void execute() throws Exception { | |
// this method is inherited from the Action superclass | |
// this will be where our drop action happens | |
final Item[] items = getItems(ITEMS_TO_DROP); | |
if (items.length > 0) { | |
// if the inventory contains any of the items to drop... | |
for (final Item i : items) { | |
if (i.getWidgetChild().interact("Drop")) { | |
Time.sleep(10, 150); | |
} | |
} | |
} | |
} | |
public boolean isValid() throws Exception { | |
// this method is inherited from the Action superclass | |
// this will return if execute() should be called | |
return Inventory.isFull(); | |
} | |
} | |
private abstract class Action extends Strategy implements Task { | |
// this is a abstract class i use, just because i prefer to, in my IDE it doesn't prompt me to implement | |
// validate, and i can also use it to get the current action in the paint, it also helps me to debug | |
// by throwing errors (not sure if RSBot does now, but it didn't a while ago) | |
public abstract void execute() throws Exception; | |
public abstract boolean isValid() throws Exception; | |
public void run() { | |
current = this; | |
try { | |
execute(); | |
} catch (final Exception e) { | |
e.printStackTrace(); | |
} | |
current = null; | |
} | |
public boolean validate() { | |
try { | |
return isValid(); | |
} catch (final Exception e) { | |
e.printStackTrace(); | |
return false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment