This is my first ever OSBot script (and post), and botting script in general.
Mainly looking for feedback and ways to improve, thanks in advance!
Naaiz's 1-60 WC
The purpose of the script is to train accounts straight from tutorial island, so around lumbridge area, to be ready to cut Yews for that easy F2P cash.
import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.api.ui.Skill;
import java.awt.*;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
@ScriptManifest(name = "Auto60WC v0.1", author = "Naaiz", version = 0.1, info = "", logo = "")
public class main extends Script {
private long startTime;
private long startExp;
private long startLvl;
private long timeElapsed;
private boolean started = false;
private JFrame gui;
// Default string values
private String selectedTree = "Lumbridge Goblins";
private String selectedOak = "Lumbridge General Store";
private String selectedWillow = "Lumbridge Pond";
// Image for progress
private final Image tracker = tracker("http://i.imgur.com/B4QjxbI.png");
private Image tracker(String url) {
try {
return ImageIO.read(new URL(url));
}
catch(IOException e) {
return null;
}
}
Color black = new Color(0, 0, 0, 255);
Color white = new Color(255,55,255,255);
private void createGUI(){
// Declare two constants for width and height of the GUI
final int GUI_WIDTH = 275, GUI_HEIGHT = 275;
// Get the size of the screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// Calculating x and y coordinates
final int gX = (int) (screenSize.getWidth() / 2) - (GUI_WIDTH / 2);
final int gY = (int) (screenSize.getHeight() / 2) - (GUI_HEIGHT / 2);
// Create a new JFrame with the title "GUI"
gui = new JFrame("Naaiz's 1-60 WC");
// Set the x coordinate, y coordinate, width and height of the GUI
gui.setBounds(gX, gY, GUI_WIDTH, GUI_HEIGHT);
// Disable resizing
gui.setResizable(false);
// Create a sub container JPanel
JPanel panel = new JPanel();
// Add it to the GUI
gui.add(panel);
// Create a label
JLabel treelabel = new JLabel("Tree location:");
JLabel oaklabel = new JLabel("Oak location:");
JLabel willowlabel = new JLabel("Willow location:");
// Set text colour to white
treelabel.setForeground(Color.white);
oaklabel.setForeground(Color.white);
willowlabel.setForeground(Color.white);
// Add it to the JPanel
panel.add(treelabel);
JComboBox<String> selectedTreeLocation = new JComboBox<>(new String[]{"Lumbridge Goblins", "North of Draynor"});
// Add an action listener, to listen for user's selections, assign to a variable called selectedTree on selection.
selectedTreeLocation.addActionListener(e -> selectedTree = selectedTreeLocation.getSelectedItem().toString());
panel.add(selectedTreeLocation);
panel.add(oaklabel);
JComboBox<String> selectedOakLocation = new JComboBox<>(new String[]{"Lumbridge General Store"});
selectedOakLocation.addActionListener(e -> selectedOak = selectedOakLocation.getSelectedItem().toString());
panel.add(selectedOakLocation);
panel.add(willowlabel);
JComboBox<String> selectedWillowLocation = new JComboBox<>(new String[]{"Lumbridge Pond", "Draynor Village"});
selectedWillowLocation.addActionListener(e -> selectedWillow = selectedWillowLocation.getSelectedItem().toString());
panel.add(selectedWillowLocation);
// Function for startbutton
JButton startButton = new JButton("Start");
startButton.setPreferredSize(new Dimension(125, 25));
startButton.addActionListener(e -> {
started = true;
gui.setVisible(false);
});
panel.add(startButton);
// Make the GUI visible
gui.setVisible(true);
}
@[member='Override']
public void onStart() {
//Creating GUI
createGUI();
startTime = System.currentTimeMillis();
startExp = skills.getExperience(Skill.WOODCUTTING);
startLvl = skills.getStatic(Skill.WOODCUTTING);
}
private enum State {
WAIT, EMPTY, CUTTREE, CUTOAK, CUTWILLOW, LOGOUT
};
private State getState() {
if(!myPlayer().isAnimating() && skills.getStatic(Skill.WOODCUTTING) < 15 && !inventory.isFull())
return State.CUTTREE;
if(!myPlayer().isAnimating() && skills.getStatic(Skill.WOODCUTTING) < 30 && !inventory.isFull())
return State.CUTOAK;
if(!myPlayer().isAnimating() && skills.getStatic(Skill.WOODCUTTING) < 60 && !inventory.isFull())
return State.CUTWILLOW;
if(inventory.isFull())
return State.EMPTY;
if(skills.getStatic(Skill.WOODCUTTING) == 60)
return State.LOGOUT;
return State.WAIT;
}
@[member='Override']
public void onExit() {
if(gui != null) { // If the JFrame has been created
gui.setVisible(false); // Hide it
gui.dispose(); // Dispose
}
}
private void treeCut(RS2Object tree,Area area,int level, int maxLevel)throws InterruptedException {
if (tree != null && !myPlayer().isMoving() && !myPlayer().isAnimating() && area.contains(myPlayer()) && tree.isVisible()) {
log(tree + " exists, player isnt doing anything and " + tree + " is visible.");
camera.toEntity(tree);
log("Chopping " + tree + "...");
tree.interact("Chop down");
}
else {
if (!myPlayer().isMoving() && !myPlayer().isAnimating() && level < maxLevel){
log("Walking to random position in " + area);
getWalking().webWalk(area.getRandomPosition());
if(tree != null) {
camera.toEntity(tree);
sleep(random(200,500));
}
}
}
}
@[member='Override']
public int onLoop() throws InterruptedException {
if(started){
RS2Object tree = getObjects().closest("Tree");
RS2Object oak = getObjects().closest("Oak");
RS2Object willow = getObjects().closest("Willow");
Area trees = new Area (3200, 3237, 3171, 3259);
Area oaks = new Area (3208, 3238, 3187, 3251);
Area willows = new Area (3169, 3263, 3160, 3274);
int currentLevel = skills.getStatic(Skill.WOODCUTTING);
if(selectedTree == "Lumbridge Goblins") {
trees = new Area (3200, 3237, 3171, 3259); // Area with a bunch of trees northwest of lumbridge
}
if(selectedTree == "North of Draynor") {
trees = new Area (3086, 3275, 3075, 3265); // Area with a bunch of trees north of draynor
}
if(selectedOak == "Lumbridge General Store") {
oaks = new Area (3208, 3238, 3187, 3251); // Oaks near Lumbridge General Store
}
if(selectedWillow == "Lumbridge Pond") {
willows = new Area (3169, 3263, 3160, 3274); // Willows at the pond north of Lumbridge
}
if(selectedWillow == "Draynor Village") {
willows = new Area (3091, 3225, 3081, 3239); // Willows at Draynor Village
}
switch (getState()) {
case WAIT:
log("Case = WAIT");
sleep(random(1000,2000));
break;
case EMPTY:
log("Case = EMPTY: Emptying inventory...");
inventory.dropAll("Logs", "Oak logs", "Willow logs");
case CUTTREE:
log("Case: CUTTREE");
treeCut(tree, trees, currentLevel, 15);
sleep(random(500,1000));
break;
case CUTOAK:
log("Case: CUTOAK");
treeCut(oak, oaks, currentLevel, 30);
sleep(random(500,1000));
break;
case CUTWILLOW:
log("Case: CUTWILLOW");
treeCut(willow, willows, currentLevel, 60);
sleep(random(500,1000));
break;
case LOGOUT:
stop();
}
}
return random(500,750); //The amount of time in milliseconds before the loop starts over
}
@[member='Override']
public void onPaint(Graphics2D gr) {
if(started){
gr.drawImage(tracker, 7, 211, null);
timeElapsed = System.currentTimeMillis() - startTime;
gr.setFont(new Font("Arial", Font.BOLD, 16));
long second = (timeElapsed / 1000) % 60;
long minute = (timeElapsed / (1000 * 60)) % 60;
long hour = (timeElapsed / (1000 * 60 * 60)) % 24;
gr.setColor(black);
gr.drawString(String.format("%02d:%02d:%02d", hour, minute, second), 337, 374);
gr.drawString("" + (skills.getExperience(Skill.WOODCUTTING) - startExp), 335, 401);
gr.drawString("" + (skills.getStatic(Skill.WOODCUTTING) - startLvl), 335, 429);
int x = getMouse().getPosition().x;
int y = getMouse().getPosition().y;
gr.drawLine(0, y, 765, y);
gr.drawLine(x, 0, x, 503);
}
}
}