I've changed the try/catch to throws InterruptedException,
also changed the equipment to inventory but still when it's looking for which state it should use in the method: getState()
it always returns the one outside the actual ones i need. thats why it gives me the null error.
new code:
package jugfiller;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.Player;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.api.ui.Message;
import org.osbot.rs07.api.ui.Message.MessageType;
import org.osbot.rs07.script.MethodProvider;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.Area;
@ScriptManifest(author = "JellePlomp", info = "Fills jugs with water", logo = "", name = "jugFiller", version = 1)
public class JugFiller extends Script
{
Position[] PATH_TO_BANK = { new Position(3281,3180,0),
new Position(3270,3167,0) };
Position[] PATH_TO_FOUNTAIN = { new Position(3281,3180,0),
new Position(3292,3175,0) };
int fountainTimes = 0;
public Area AlKharidBank = new Area(3269, 3170, 3272, 3162);
public Area AlKharidFountain = new Area(3290,3170,3295,3175);
public String state = "";
long startTime;
public enum State
{
BANK_JUGS, //als hij geen jugs meer heeft maar WEL water, en in bank is
FILL_JUGS, //als hij empty jugs heeft en in de area fountain is
CRASH;
}
public void onStop() {}
public void onStart()
{
state = "Starting up...wait a moment";
startTime = System.currentTimeMillis();
}
public State getState()
{
if(this.inventory.contains(new String[] {"Jugs"}))
{
return State.FILL_JUGS;
}
if(!this.inventory.contains(new String[] {"Jugs"}) && this.inventory.contains(new String[] {"jug of water"}))
{
return State.BANK_JUGS;
}
return State.CRASH;
}
public int onLoop() throws InterruptedException {
switch(getState())
{
case BANK_JUGS:
bank();
break;
case FILL_JUGS:
fill();
break;
case CRASH:
log("crashs here");
break;
default:
break;
}
return random(200, 300);
}
public void bank() throws InterruptedException {
if (AlKharidBank.contains(myPlayer())) {
this.state = "BANKING_JUGS";
if (!this.bank.isOpen()) {
this.bank.open();
withdrawJugs();
} else
withdrawJugs();
} else if (AlKharidFountain.contains(myPlayer())) {
this.state = "WALKING_TO_BANK";
this.safeWalkTo(this.PATH_TO_BANK);
EXECUTE_ANTIBAN();
}
}
public void fill() throws InterruptedException {
if (AlKharidFountain.contains(myPlayer())) {
this.state = "FILLING_JUGS";
RS2Object fountain = (RS2Object)this.objects.closest(new String[] { "Fountain" });
if (fountain != null) {
getInventory().getItem(new String[] { "Jug" }).interact(new String[] { "Use" });
fountain.interact(" -> ");
MethodProvider.sleep(random(18000, 20000));
}
} else if (AlKharidBank.contains(myPlayer())) {
this.state = "WALKING_TO_FOUNTAIN";
this.safeWalkTo(this.PATH_TO_FOUNTAIN);
EXECUTE_ANTIBAN();
}
}
private void safeWalkTo(Position[] p) throws InterruptedException {
this.getLocalWalker().walkPath(p);
Position lastPos = p[p.length - 1];
while (true) {
double lastDistance = distanceSquared(this.myPosition(), lastPos);
if (lastDistance < 4.5d) {
break;
}
sleep(3000);
if (Math.abs(distanceSquared(this.myPosition(), lastPos) - lastDistance) < 1.1d) {
ArrayList<Position> shortenedPath = new ArrayList<Position>();
boolean startAdding = false;
for (Position pos : p) {
if (distanceSquared(this.myPosition(), pos) < 100d) {
startAdding = true;
}
if (startAdding) {
shortenedPath.add(pos);
}
}
this.getLocalWalker().walkPath(shortenedPath);
}
}
}
private double distanceSquared(Position p1, Position p2) {
return Math.pow(p1.getX() - p2.getX(), 2d) + Math.pow(p1.getY() - p2.getY(), 2d);
}
public void withdrawJugs() throws InterruptedException {
this.bank.depositAll();
this.bank.withdrawAll("Jug");
this.bank.close();
}
public void EXECUTE_ANTIBAN() throws InterruptedException {
Random r1 = new Random();
float chance = r1.nextFloat();
if (chance <= 0.4F) {
antiban();
}
}
public void antiban() throws InterruptedException {
int select = random(3, 6);
switch (select) {
case 3:
int pitch = random(22, 67);
getCamera().movePitch(pitch);
break;
case 4:
int yaw = random(1, 360);
getCamera().moveYaw(yaw);
break;
case 5:
List<Player> p = getPlayers().getAll();
if (p != null) {
int randomNum = Script.random(0, p.size() - 1);
Player randomPlayer = (Player) p.get(randomNum);
if (randomPlayer != null) {
randomPlayer.hover();
getMouse().click(true);
sleep(random(200, 500));
}
}
break;
case 6:
List<Player> pl = getPlayers().getAll();
if (pl != null) {
int randomNum = Script.random(0, pl.size() - 1);
Player randomPlayer = (Player) pl.get(randomNum);
if (randomPlayer != null) {
randomPlayer.hover();
}
}
break;
}
}
public void onPaint(Graphics2D g) {
long elapsed = System.currentTimeMillis() - startTime;
g.setColor(Color.YELLOW);
g.setFont(new Font("Arial", Font.PLAIN, 10));
g.drawString("Visited fountain: "+fountainTimes+" times", 320, 290);
g.drawString("run time: "+format(elapsed)+" times", 320, 315);
g.drawString("current state: "+state+"", 320, 330);
}
public void onMessage(Message c) {
if (c.getType() == MessageType.GAME) {
String m = c.getMessage().toLowerCase();
try {
if(m.contains("fill")) {
fountainTimes++;
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
public String format(long time) {
StringBuilder string = new StringBuilder();
long totalSeconds = time / 1000L;
long totalMinutes = totalSeconds / 60L;
long totalHours = totalMinutes / 60L;
int seconds = (int) totalSeconds % 60;
int minutes = (int) totalMinutes % 60;
int hours = (int) totalHours % 24;
if (hours > 0) {
string.append(hours + "h ");
}
if (minutes > 0) {
string.append(minutes + "m ");
}
string.append(seconds + "s");
return string.toString();
}
}
btw thank you for helping (: