Skip to content

Instantly share code, notes, and snippets.

@CalebWhiting
Last active November 24, 2016 22:45
Show Gist options
  • Save CalebWhiting/e1d4d5706d8cc4b4177e to your computer and use it in GitHub Desktop.
Save CalebWhiting/e1d4d5706d8cc4b4177e to your computer and use it in GitHub Desktop.
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.Game;
import org.powerbot.game.api.methods.Tabs;
import org.powerbot.game.api.methods.Widgets;
import org.powerbot.game.api.methods.input.Mouse;
import org.powerbot.game.api.methods.interactive.NPCs;
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.Magic;
import org.powerbot.game.api.methods.tab.Skills;
import org.powerbot.game.api.methods.widget.Camera;
import org.powerbot.game.api.util.Random;
import org.powerbot.game.api.util.Timer;
import org.powerbot.game.api.wrappers.Locatable;
import org.powerbot.game.api.wrappers.node.Item;
import org.powerbot.game.api.wrappers.widget.Widget;
import org.powerbot.game.api.wrappers.widget.WidgetChild;
import org.powerbot.game.bot.event.listener.PaintListener;
import java.awt.*;
import static org.powerbot.game.api.methods.Tabs.*;
import static org.powerbot.game.api.methods.input.Mouse.getLocation;
import static org.powerbot.game.api.util.Random.nextInt;
import static org.powerbot.game.api.util.Time.format;
import static org.powerbot.game.api.util.Time.sleep;
/**
* @author Caleb Date: 15/04/12 Time: 17:44
*/
@Manifest (name = "Alchemy", authors = "Caleb")
public class Alchemy extends ActiveScript implements PaintListener {
/**
* *******************************
*/
public static final int ITEM = 63;
/**
* *******************************
*/
private int startXP = 0;
private long startTime = 0;
@Override protected void setup() {
startXP = Skills.getExperience(Skills.MAGIC);
startTime = System.currentTimeMillis();
provide(new Antiban());
provide(new Alch());
}
@Override public void onRepaint(final Graphics graphics) {
final Graphics2D g = (Graphics2D) graphics;
final Point mouse = Mouse.getLocation();
final int x = mouse.x, y = mouse.y;
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(4));
g.drawLine(x - 8, y, x + 8, y);
g.drawLine(x, y - 8, x, y + 8);
g.setColor(Mouse.isPressed() ? Color.RED : Color.GREEN);
g.setStroke(new BasicStroke(2));
g.drawLine(x - 8, y, x + 8, y);
g.drawLine(x, y - 8, x, y + 8);
g.setColor(Color.GREEN);
int tx = 50, ty = 50;
for (final String s : getPaintStrings()) {
g.drawString(s, tx, ty);
ty += 20;
}
}
public String[] getPaintStrings() {
return new String[]{
"Alchemy - 1.01", format(System.currentTimeMillis() - startTime), "EXP: " + (Skills.getExperience(Skills.MAGIC) - startXP),
};
}
public class Alch extends Strategy implements Task {
public void run() {
switch (getCurrent()) {
case MAGIC:
Mage.clickSpell(Mage.SPELL_HIGH_ALCHEMY);
final Timer timer = new Timer(nextInt(100, 500));
while (timer.isRunning() && getCurrent() != INVENTORY) {
sleep(nextInt(50, 200));
}
break;
case INVENTORY:
if (Magic.isSpellSelected()) {
final Item i = Inventory.getItem(ITEM);
if (i != null) {
if (i.getWidgetChild().contains(getLocation())) {
Mouse.click(true);
} else {
i.getWidgetChild().click(true);
}
final Timer timer2 = new Timer(nextInt(500, 1500));
while (timer2.isRunning() && getCurrent() != MAGIC) {
sleep(nextInt(50, 200));
}
}
} else {
final Timer timer2 = new Timer(nextInt(2000, 2500));
while (timer2.isRunning() && getCurrent() != MAGIC) {
sleep(nextInt(50, 200));
}
if (getCurrent() != MAGIC) {
MAGIC.open();
sleep(nextInt(50, 200));
}
}
}
if (getCurrent() != MAGIC && getCurrent() != INVENTORY) {
MAGIC.open();
sleep(nextInt(50, 200));
}
}
public boolean validate() {
return Game.isLoggedIn();
}
}
public static class Mage {
/**
* Magic spell books.
*/
public static enum Book {
MODERN(192), ANCIENT(193), LUNAR(430);
private int id;
Book(int id) {
this.id = id;
}
public int getInterfaceID() {
return id;
}
}
public static final int SPELL_HIGH_ALCHEMY = 59;
public static boolean clickSpell(int spell) {
if (getCurrent() != MAGIC) {
Tabs.MAGIC.open();
for (int i = 0; i < 100; i++) {
sleep(20);
if (getCurrent() == MAGIC) {
break;
}
}
sleep(Random.nextInt(150, 250));
}
if (getCurrent() == Tabs.MAGIC) {
Widget inter = getWidget();
if (inter != null) {
WidgetChild comp = inter.getChild(spell);
if (comp.validate()) {
return comp.getBoundingRectangle().contains(Mouse.getLocation()) ? Mouse.click(Mouse.getLocation(), true) : comp.click(true);
}
}
}
return false;
}
/**
* Gets the open magic book interface.
*
* @return The current magic Widget.
*/
public static Widget getWidget() {
Widget inter = Widgets.get(Book.MODERN.getInterfaceID());
if (! inter.validate()) {
inter = Widgets.get(Book.ANCIENT.getInterfaceID());
if (! inter.validate()) {
inter = Widgets.get(Book.LUNAR.getInterfaceID());
if (! inter.validate()) {
return null;
}
}
}
return inter;
}
}
public static class Antiban extends Strategy implements Task {
final Timer timer = new Timer(Random.nextInt(2 * 60000, 4 * 60000));
@Override public void run() {
switch (nextInt(0, 3)) {
case 0:
Camera.setAngle(Random.nextInt(0, 360));
break;
case 1:
final Tabs[] tabs = Tabs.values();
tabs[nextInt(0, tabs.length)].open();
break;
case 2:
Locatable[] l = null;
switch (Random.nextInt(0, 3)) {
case 0:
l = NPCs.getLoaded();
break;
case 1:
l = Players.getLoaded();
break;
case 2:
l = SceneEntities.getLoaded();
break;
}
if (l != null && l.length > 0) {
Camera.turnTo(l[nextInt(0, l.length)]);
}
break;
}
sleep(nextInt(100, 500));
timer.setEndIn(nextInt(2 * 60000, 4 * 60000));
}
public boolean validate() {
return ! timer.isRunning() && Game.isLoggedIn();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment