I have been coding a couple of basic scripts to get me used to writing, and if you're looking for bots, you usually find them spinning flax non-stop. So I figured I'd give writing a Flax-spinner a shot I'm looking for advice on how to improve, so please be critical.
package spinner;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import org.osbot.script.Script;
import org.osbot.script.ScriptManifest;
import org.osbot.script.rs2.map.Position;
import org.osbot.script.rs2.model.Entity;
import org.osbot.script.rs2.model.Player;
import org.osbot.script.rs2.skill.Skill;
import org.osbot.script.rs2.ui.Bank;
import org.osbot.script.rs2.ui.Inventory;
import org.osbot.script.rs2.utility.Area;
@ScriptManifest(author = "Hobroski", info = "Spins flax at Lumbridge", name = "Spinner", version = 1.0D)
public class Spinner extends Script {
final Area SPIN_AREA = new Area(3208, 3212, 3210, 3213);
final Area BANK_AREA = new Area(2307, 3218, 3210, 3220);
final Area NFLAT_AREA = new Area(3205, 3209, 3205, 3209);
final Area DFLAT_AREA = new Area(3206, 3208, 3206, 3208);
final Area ODOOR_AREA = new Area(3207, 3214, 3207, 3214);
final Position NSTAIR_POS = new Position(3205, 3208, 2);
final Position DSTAIR_POS = new Position(3204, 3207, 1);
int NSTAIR_ID = 1316;
int DSTAIR_ID = 2718;
int CDOOR_ID = 20857;
static Timer runtime;
int sEXP;
int sLVL;
int flaxSpun = 0;
boolean init = true;
private int experienceToNextLevel(Skill skill) {
int[] xpForLevels = new int[] { 0, 83, 174, 276, 388, 512, 650, 801,
969, 1154, 1358, 1584, 1833, 2107, 2411, 2746, 3115, 3523,
3973, 4470, 5018, 5624, 6291, 7028, 7842, 8740, 9730, 10824,
12031, 13363, 14833, 16456, 18247, 20224, 22406, 24815, 27473,
30408, 33648, 37224, 41171, 45529, 50339, 55649, 61512, 67983,
75127, 83014, 91721, 101333, 111945, 123660, 136594, 150872,
166636, 184040, 203254, 224466, 247886, 273742, 302288, 333804,
368599, 407015, 449428, 496254, 547953, 605032, 668051, 737627,
814445, 899257, 992895, 1096278, 1210421, 1336443, 1475581,
1629200, 1798808, 1986068, 2192818, 2421087, 2673114, 2951373,
3258594, 3597792, 3972294, 4385776, 4842295, 5346332, 5902831,
6517253, 7195629, 7944614, 8771558, 9684577, 10692629,
11805606, 13034431 };
int xp = client.getSkills().getExperience(skill);
for (int i = 0; i < 99; i++) {
if (xp < xpForLevels[i]) {
return (xpForLevels[i] - xp);
}
}
return (200000000 - xp);
}
public void onStart() {
runtime = new Timer(0);
}
private void init() {
if (client.getLoginState() == 30 && init) {
sEXP = client.getSkills().getExperience(Skill.CRAFTING);
sLVL = client.getSkills().getLevel(Skill.CRAFTING);
try {
client.rotateCameraPitch(random(25, 32));
} catch (InterruptedException e) {
}
init = false;
}
}
public int onLoop() {
Player player = client.getMyPlayer();
Bank bank = client.getBank();
Inventory inven = client.getInventory();
try {
init();
if (inven.contains("Flax") && !player.isMoving()) {
if (BANK_AREA.contains(player)) {
client.moveCameraToPosition(NSTAIR_POS);
if (NSTAIR_POS.isVisible(bot) && BANK_AREA.contains(player)) {
NSTAIR_POS.interact(bot, "Climb-down");
sleep(random(400, 800));
}
}
if (DFLAT_AREA.contains(player) || SPIN_AREA.contains(player)
|| ODOOR_AREA.contains(player) && !player.isMoving()) {
Entity spinningWheel = closestObjectForName("Spinning wheel");
client.moveCameraToEntity(spinningWheel);
if (spinningWheel.isVisible()) {
if (spinningWheel.interact("Spin")) {
sleep(random(2000,3000));
} else {
Entity closedDoor = closestObject(CDOOR_ID);
closedDoor.interact("Open");
}
}
}
if (this.client.getInterface(459).getChild(89).isVisible()) {
sleep(random(900, 2000));
this.selectInterfaceOption(459, 89, "Make X");
sleep(random(900, 2000));
client.typeString("28");
}
} else {
if (SPIN_AREA.contains(player) && !player.isMoving()) {
if (DSTAIR_POS.isVisible(bot)) {
DSTAIR_POS.interact(bot, "Climb-up");
} else {
client.moveCameraToPosition(DSTAIR_POS);
}
}
if (NFLAT_AREA.contains(player) && !player.isMoving()) {
Entity bankBooth = closestObjectForName("Bank booth");
if (bankBooth.isVisible()) {
bankBooth.interact("Bank");
} else {
client.moveCameraToEntity(bankBooth);
}
}
if (BANK_AREA.contains(player) && !player.isMoving()) {
if (!bank.isOpen()) {
Entity bankBooth = closestObjectForName("Bank Booth");
bankBooth.interact("Bank");
sleep(1000);
}
if (inven.isEmpty()) {
bank.withdrawAll(1779);
bank.close();
}
if (inven.contains("Bow string")) {
bank.depositAll();
}
}
}
} catch (Exception e) {
}
return 50;
}
public void onExit() {
}
public void onPaint(Graphics g) {
int gLVL = client.getSkills().getCurrentLevel(Skill.CRAFTING) - sLVL;
int cLVL = client.getSkills().getCurrentLevel(Skill.CRAFTING);
int cEXP = client.getSkills().getExperience(Skill.CRAFTING);
flaxSpun = (cEXP - sEXP) / 15;
Graphics2D gr = (Graphics2D) g;
gr.setColor(Color.WHITE);
gr.setFont(new Font("Arial", Font.PLAIN, 10));
gr.drawString("Time ran : " + Timer.format(runtime.getElapsed()),
25, 50);
gr.drawString("Flax spun : " + flaxSpun, 25, 65);
gr.drawString("Flax spun per hour : " + getPerHour(flaxSpun), 25, 80);
gr.drawString("Profit gained : " + flaxSpun * 45, 25, 95);
gr.drawString("Profit per hour : " + getPerHour(flaxSpun * 45), 25,
110);
gr.drawString("Experience gained : " + (cEXP - sEXP), 25, 125);
gr.drawString("Experience per hour : " + getPerHour(cEXP - sEXP), 25,
140);
gr.drawString(
"Starting Level / Current level : " + sLVL + " / " + cLVL, 25,
155);
gr.drawString("Levels gained : " + gLVL, 25, 170);
gr.drawString("Experience to next level : "
+ experienceToNextLevel(Skill.CRAFTING), 25, 185);
}
public static int getPerHour(int value) {
if (runtime != null && runtime.getElapsed() > 0) {
return (int) (value * 3600000d / runtime.getElapsed());
} else {
return 0;
}
}
public static long getPerHour(long value) {
if (runtime != null && runtime.getElapsed() > 0) {
return (long) (value * 3600000d / runtime.getElapsed());
} else {
return 0;
}
}
}