Jump to content

Check whats wrong with my script


futurepasts

Recommended Posts

Hello maybe anyone could check my script it doesn't do things as i want.

package Main;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.api.Bank;
import org.osbot.rs07.api.Inventory;

import java.util.concurrent.TimeUnit; 
 


import java.awt.*;
 
@ScriptManifest(author = "test", info = "test", name = "test", version = 1.0, logo = "")
public class main extends Script {
 
    @Override
    public void onStart() {
        timeBegan = System.currentTimeMillis();
    }
    
    private enum State {
		USE, BANK, WAIT, WALK_TO_PUMP, FILL, TAKE;
	};
	
	
	
	private long timeBegan;
	private long timeRan;

	
	Position[] pathtopump = {new Position(2945,3368,0), new Position(2945,3370,0), new Position(2945,3372,0), new Position(2947,3374,0), new Position(2949,3376,0), new Position(2949,3378,0), new Position(2950,3380,0), new Position(2949,3382,0)};
	
	
	Position[] pathtobank = {new Position(2949,3382,0), new Position(2950,3380,0), new Position(2948,3376,0), new Position(2946,3372,0), new Position(2946,3369,0)};

	private State getState() {
		Entity pump = objects.closest("Waterpump");
		if(inventory.getAmount("Bucket of water") == 27)
			return State.BANK;
		if (pump != null)
			return State.USE;
		if (bank.contains("Bucket"))
				return State.TAKE;
		if (inventory.contains("Bucket"))
			return State.WALK_TO_PUMP;
		if (!inventory.contains("Bucket"))
			return State.BANK;
		return State.WAIT;
	}
    
    @Override
    public int onLoop() throws InterruptedException {
    	switch (getState()) {
    	case TAKE:
    		bank.open();
    		if (bank.contains("Bucket"))
    				bank.withdraw("Bucket", 27);
    		sleep(random(200, 420));
		case USE:
			RS2Object well = objects.closest("Waterpump");
			getInventory().interact("Use", "Bucket");
		    if (well != null) {
		    	well.interact("Use");
			sleep(random(7200, 7511));
			if(inventory.getAmount("Bucket of water") == 27)
			break;
			 }
		case BANK:
			localWalker.walkPath(pathtobank);
			log("Walking to bank");
			sleep(random(5000, 5700));
		    	bank.open();
		            while (bank.isOpen())
		            	sleep(random(200, 420));
		            bank.depositAll();
		    break;    
		case WALK_TO_PUMP:
			localWalker.walkPath(pathtopump);
			break;
		case WAIT:
			sleep(random(5000, 5700));
			break;
		}
    	return random(200, 300);
    }
 
    @Override
    public void onExit() {
        log("Script stopped!");
    }
 
    @Override
    public void onPaint(Graphics2D g) {
    	timeRan = System.currentTimeMillis() - this.timeBegan;
    	g.drawString(ft(timeRan), 50, 50); 
    }
    // How long the script has been running!
    private String ft(long duration) 
	{
		String res = "Time ran:";
		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;
	} 
}

I was thinking about areas but it seems it doesn't work for me too :/

 

 

            What my script does wrong is that on start it ignore

  CASE:Take and instantly goes to Waterpump(if i have buckets in inventory it goes to Waterpump and fill water then goes back to bank open it and the IDLE)

 

Also when filling buckets(after filling about 5-6 it tries to use bucket again but clicks on bucket of water and then fails and idle again)

 

When filling i tried to add but, it keeps doing same shit.

if(!myPlayer().isAnimating()) {
Link to comment
Share on other sites

Because you aren't checking if the bank is actually open yet.

 

You're just telling it to open the bank, then it will immediately execute the code after that.

Put a condition sleep in after bank.open()

 

new ConditionalSleep(5000){
@Override
public boolean condition() {
return bank.isOpen();
}
}.sleep
Edited by Tom
  • Like 2
Link to comment
Share on other sites

Because you aren't checking if the bank is actually open yet.

 

You're just telling it to open the bank, then it will immediately execute the code after that.

Put a condition sleep in after bank.open()

 

 

new ConditionalSleep(5000){
@Override
public boolean condition() {
return bank.isOpen();
}
}.sleep

I'd be surprised if this fixes it, since this is the exact conditional sleep bank.open uses.

In general, I'd recommend executing one action in the onLoop and use conditional statements to determine which one, and use the return values :p

if(bank.open()) {

 ...

}

Link to comment
Share on other sites

I'd be surprised if this fixes it, since this is the exact conditional sleep bank.open uses.

In general, I'd recommend executing one action in the onLoop and use conditional statements to determine which one, and use the return values tongue.png

if(bank.open()) {

 ...

}

 

 

It was just one of the first things I noticed that could potentially cause problems, I myself didn't know bank.open used a conditional sleep, I thought it was just the interaction then continues.

Link to comment
Share on other sites

Well i will try that but question why In CASE:USE   it interacts with waterpump and after like 6-7 or sometimes less buckets it tries to interact again and then it clicks on bucket of water after it idles and script stops.

 

Because you are sleeping for 7 or so seconds, and after that 7 seconds of sleeping, it will try to interact again unless you add some conditions.

 

What I would suggest is called an animation timer, ill post the snippet.

	long lastAnimation = 0;

	public void updateTimes() {
		if (inventory.isItemSelected()) {
			inventory.deselectItem();
		}
		if (myPlayer().isAnimating()) {
			lastAnimation = System.currentTimeMillis();
		}
	}

	public boolean canAnimate() {
		return System.currentTimeMillis() > (lastAnimation + 2000);
	}

Then you want to have another Case state being animate, preferably the first state so it gets checked first which just checks if the player is animating, and runs updateTimes() if he is.

if(myPlayer().isAnimating() && !myPlayer().isMoving()){
           updateTimes();
}

Also, you should add a break; at the end of your Case:TAKE state.

Link to comment
Share on other sites

Now it fills buckets great but when it comes to bank or when i start the script and my player is in the bank it just stands

package main;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.api.Bank;
import org.osbot.rs07.api.Inventory;

import java.util.Arrays;
import java.util.concurrent.TimeUnit; 
 


import java.awt.*;
 
@ScriptManifest(author = "test", info = "test", name = "test", version = 1.0, logo = "")
public class main extends Script {
 
    @Override
    public void onStart() {
        timeBegan = System.currentTimeMillis();
    }
    
    private enum State {
		USE, BANK, WAIT, WALK_TO_PUMP, FILL, TAKE, ANIMATE;
	};
	
	
	
	private long timeBegan;
	private long timeRan;

	
	Position[] pathtopump = {new Position(3185,3444,0), new Position(3183,3443,0), new Position(3183,3438,0), new Position(3183,3436,0), new Position(3183,3432,0), new Position(3187,3432,0), new Position(3189,3432,0), new Position(3193,3432,0), new Position(3195,3432,0), new Position(3198,3429,0), new Position(3200,3429,0), new Position(3203,3429,0), new Position(3206,3429,0), new Position(3208,3430,0)};
	
	
	Position[] pathtobank = {new Position(3209,3430,0), new Position(3207,3430,0), new Position(3205,3430,0), new Position(3201,3430,0), new Position(3199,3430,0), new Position(3195,3430,0), new Position(3193,3430,0), new Position(3191,3430,0), new Position(3187,3430,0), new Position(3184,3431,0), new Position(3183,3433,0), new Position(3183,3436,0), new Position(3182,3439,0), new Position(3185,3440,0)};

	private State getState() {
		Entity pump = objects.closest("Fountain");
		if(inventory.getAmount("Bucket of water") == 27)
			return State.BANK;
		if (pump != null)
			return State.USE;
		if (bank.contains("Bucket"))
				return State.TAKE;
		if (inventory.contains("Bucket"))
			return State.WALK_TO_PUMP;
		if (!inventory.contains("Bucket"))
			return State.BANK;
		if(myPlayer().isAnimating() && !myPlayer().isMoving()){
	           updateTimes();
	           return State.ANIMATE;
	}
		return State.WAIT;
	}
    
	long lastAnimation = 0;

	public void updateTimes() {
		if (inventory.isItemSelected()) {
			inventory.deselectItem();
		}
		if (myPlayer().isAnimating()) {
			lastAnimation = System.currentTimeMillis();
		}
	}

	public boolean canAnimate() {
		return System.currentTimeMillis() > (lastAnimation + 2000);
	}
	
	
    @Override
    public int onLoop() throws InterruptedException {
    	switch (getState()) {
    	case TAKE:
    		bank.open();
    		if (bank.contains("Bucket"))
    				bank.withdraw("Bucket", 27);
    		sleep(random(200, 420));
    		break;
		case USE:
			if(canAnimate()){
				RS2Object well = objects.closest("Fountain");
				getInventory().interact("Use", "Bucket");
			    if (well != null) {
			    	well.interact("Use");
			    	sleep(random(8000, 9000));
			break;
			    }
			 }
		case BANK:
			localWalker.walkPath(pathtobank);
			log("Walking to bank");
			sleep(random(5000, 5700));
			RS2Object bank1 = objects.closest("Bank booth");
			if (bank1 != null) {
				if(Arrays.asList(bank1.getDefinition().getActions()).contains("Bank")){
		    	bank.open();
		            while (bank.isOpen())
		            	sleep(random(200, 420));
		            bank.depositAll();
			}
			}
		    break;    
		case WALK_TO_PUMP:
			localWalker.walkPath(pathtopump);
			break;
		case WAIT:
			sleep(random(5000, 5700));
			break;
		}
    	return random(200, 300);
    }
 
    @Override
    public void onExit() {
        log("Script stopped!");
    }
 
    @Override
    public void onPaint(Graphics2D g) {
    	timeRan = System.currentTimeMillis() - this.timeBegan;
    	g.drawString(ft(timeRan), 50, 50); 
    }
    // How long the script has been running!
    private String ft(long duration) 
	{
		String res = "Time ran:";
		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;
	} 
}
Edited by futurepasts
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...