Hey fellas - so i'm working on a thieving script for fun (first time scripting) and for some reason my character clicks the master farmer and instantly logs out. Also - could someone explain to me how i would be able to make the script thieve other npc as well? Thanks. -- Here is my code so far:
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;
import javax.swing.*;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.api.ui.Skill;
import java.awt.*;
import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;
@ScriptManifest(author = "Trickked", info = "My first script", name = "Master Farmer Thiever", version = 0, logo = "")
public class Main1 extends Script {
// Holds time start of script and how long it ran
private long timeBegan;
private long timeRan;
// Holds XP Gained from beginning/end
private int beginningXP;
private int currentXP;
private int xpGained;
// Hold Stat level start/current/amount gained
private int currentLevel;
private int beginningLevel;
private int levelsGained;
// Holds percentage till next level and current xp
private double currentLevelXp;
private double nextLevelXp;
private double percentTNL;
// Time until next level
private int xpPerHour;
private double xpTillNextLevel;
private long timeTNL;
private String selectedThiever = "Master Farmer";
final int[] XP_TABLE = { 0, 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, 200000000 };
private JFrame gui;
private boolean started = false;
private void createGUI() {
final int GUI_WIDTH = 400, GUI_HEIGHT = 500;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final int gX = (int) (screenSize.getWidth() / 2) - (GUI_WIDTH / 2);
final int gY = (int) (screenSize.getHeight() / 2) - (GUI_HEIGHT / 2);
gui = new JFrame("GUI");
gui.setBounds(gX, gY, GUI_WIDTH, GUI_HEIGHT);
gui.setResizable(false);
JPanel panel = new JPanel();
gui.add(panel);
JLabel label = new JLabel("Select what you would like to thieve:");
label.setForeground(Color.white);
panel.add(label);
JComboBox<String> thieverSelector = new JComboBox<>(new String[] { "Master Farmer", "Men", "Guard" });
thieverSelector.addActionListener(e -> selectedThiever = thieverSelector.getSelectedItem().toString());
panel.add(thieverSelector);
JButton startButton = new JButton("Start");
startButton.addActionListener(e -> {
started = true;
gui.setVisible(false);
});
panel.add(startButton);
gui.setVisible(true);
}
// Keeps track of the time from start to end
@Override
public void onStart() {
timeBegan = System.currentTimeMillis();
timeRan = System.currentTimeMillis() - this.timeBegan;
beginningXP = skills.getExperience(Skill.THIEVING);
beginningLevel = skills.getStatic(Skill.THIEVING);
timeTNL = 0;
getBank().withdraw("Lobster", 10);
createGUI();
}
// Stores interactions
private enum State {
PICKPOCKET, BANK, WAIT, EAT
};
// Receives interactions made and checks inventory
private State getState() {
int HpPercent = (getSkills().getDynamic(Skill.HITPOINTS) * 100 / getSkills().getStatic(Skill.HITPOINTS));
NPC farmer = getNpcs().closest("Master Farmer");
if (getInventory().isFull() && !getInventory().contains("Lobster"))
return State.BANK;
if (farmer != null && !getInventory().isFull() && HpPercent > 50 && !myPlayer().isAnimating())
return State.PICKPOCKET;
if (HpPercent < 50 && !myPlayer().isAnimating() && getInventory().getItem("Shrimp").interact("Eat")
|| getInventory().getItem("Trout").interact("Eat") || getInventory().getItem("Salmon").interact("Eat")
|| getInventory().getItem("Tuna").interact("Eat") || getInventory().getItem("Lobster").interact("Eat"))
return State.EAT;
return State.WAIT;
}
// Continues script until out of food
@Override
public int onLoop() throws InterruptedException {
if (started) {
switch (getState()) {
case PICKPOCKET:
log("Pickpocket");
NPC farmer = getNpcs().closest("Master Farmer");
if (farmer != null) {
getNpcs().closest("Master Farmer").interact("Pickpocket");
} else {
if (!myPlayer().isAnimating())
;
}
case EAT:
if (getInventory().contains("Shrimp") || getInventory().contains("Trout")
|| getInventory().contains("Tuna") || getInventory().contains("Shrimp")
|| getInventory().contains("Lobster")) {
if (getInventory().getSelectedItemName() == null)
;
{
getInventory().getItem("Lobster").interact("Eat");
}
} else {
getInventory().deselectItem();
}
case BANK:
if (getBank().isOpen()) {
getBank().depositAll();
getBank().withdraw("Lobster", 10);
} else {
RS2Object banker = getObjects().closest("Bank Booth");
if (!myPlayer().isAnimating() && !getInventory().contains("Lobster") && banker != null
&& banker.exists()) {
banker.interact("Bank");
sleep(5000); // Put a conditional sleep here!
} else {
stop(); // Stop if banker could not be found. You will
// probably need to add walking here
}
}
break;
case WAIT:
log("wait");
sleep(random(500, 700));
break;
}
return random(50, 400);
}
return 0;
}
// Sends message once script ends
@Override
public void onExit() {
if (gui != null) {
gui.setVisible(false);
gui.dispose();
}
}
// ONPAINT() METHOD
public void onPaint(Graphics2D g) {
if (started) {
timeRan = System.currentTimeMillis() - this.timeBegan;
g.drawString("Time Ran: " + ft(timeRan), 80, 75);
currentXP = skills.getExperience(Skill.THIEVING);
xpGained = currentXP - beginningXP;
g.drawString("XP Gained: " + xpGained, 80, 114);
currentLevelXp = XP_TABLE[currentLevel];
nextLevelXp = XP_TABLE[currentLevel + 1];
percentTNL = ((currentXP - currentLevelXp) / (nextLevelXp - currentLevelXp) * 100);
DecimalFormat df = new DecimalFormat("#.#");
g.drawString("Percent TNL: " + df.format(percentTNL), 80, 153);
currentLevel = skills.getStatic(Skill.THIEVING);
g.drawString("Beginning Level: " + beginningLevel, 80, 192);
g.drawString("Current Level: " + currentLevel, 80, 231);
levelsGained = currentLevel - beginningLevel;
g.drawString("Levels Gained: " + levelsGained, 80, 270);
xpPerHour = (int) (xpGained / ((System.currentTimeMillis() - this.timeBegan) / 3600000.0D));
nextLevelXp = XP_TABLE[currentLevel + 1];
xpTillNextLevel = nextLevelXp - currentXP;
if (xpGained >= 1) {
timeTNL = (long) ((xpTillNextLevel / xpPerHour) * 3600000);
g.drawString("Time TNL: " + ft(timeTNL), 80, 309);
}
}
}
private String ft(long duration)
{
String res = "";
long days = TimeUnit.MILLISECONDS.toDays(duration);
long hours = TimeUnit.MILLISECONDS.toHours(duration)
- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration)
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration));
long seconds = TimeUnit.MILLISECONDS.toSeconds(duration)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration));
if (days == 0) {
res = (hours + ":" + minutes + ":" + seconds);
} else {
res = (days + ":" + hours + ":" + minutes + ":" + seconds);
}
return res;
}
}