Jump to content

Scripting Help


Recommended Posts

Posted

I ran into a problem trying to make my Jug collector bank and the script is just not working. I tried to format this as neat and tidy as I can with the help of Juggles topics.

This is my first script and if anyone finds errors, things that can fix this could you please explain it too me. Thank You

 

This is the code

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.GroundItem;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import java.awt.*;


@ScriptManifest(name = "Jug Collector", author = "Toxic", version = 1, info = "Collects Jugs", logo = "")

public class Main extends Script {
	
	private State state;
	
//Area to where the jug is
	Area Jug = new Area (3211,3212,0,0);
	
//Area for my bank
	Area Bank = new Area(3208,3221,2,0);
	
	private enum State {
//These must be true and it will go to the CASE
		
	TakeJug,WalkToBank,Bank,WalkToJug
	}
	
	private State getState() {
/*This is where you tell it what case to go to,
		If true it will go to that case*/
		
		
		if (getInventory().isFull() && Bank.contains(myPlayer())) {
			
			return state.Bank;
		}
		
		if (getInventory().isFull() && !Bank.contains(myPlayer())){
			
			return state.WalkToBank;
		}
		
		if (!getInventory().isFull() && !Jug.contains(myPlayer())){
			
			return state.TakeJug;
		}
		
		if (!getInventory().isFull() && !Jug.contains(myPlayer())){
			
			return state.WalkToBank;
		}
		
		return state.Bank;
	}
	
	public void onStart() {
		
	}
	
	@Override
	
	public int onLoop() throws InterruptedException {
		state = getState();
		
		switch (state) {
		
		case Bank:
//If bank isn't open, go open it
			if (!getBank().isOpen()) {
				getBank().open();
			} else {
//If bank is open, deposit everything I have
				getBank().depositAll();
			}
			
		case WalkToBank:
//webWalking to the bank, will handle any obstacle in the way
			getWalking().webWalk(Bank);
			
			break;
			
		case TakeJug:
			
			if (myPlayer().isAnimating()) {
//If my player is taking the jug, do nothing
				
				//DO NOTHING IS HERE
			} else {
				
/*Here you null checking the table. Once it is not equal to null,
	you can take the jug. It will only do this when it is
	not already taking the jug */
				RS2Object jug = getObjects().closest("Jug");
				if (jug != null) {
					jug.interact("Take");
				}
			}
					
					break;
					
		case WalkToJug:
			
//Basic webWalk to the area you created for taking the jug.
			
			getWalking().webWalk(Jug);
			
			break;
	
		}
//This is how long it takes to loop
		
		return random(150, 175);
		
	}
		
	@Override
	
	public void onExit() {
		log("Thanks for running my Jug Stealer");
	}
	
	@Override
	
	public void onPaint(Graphics2D g) {
		g.drawString("Hello Jugs!", 50, 50);
		g.drawString(getState().toString(), 50, 60);
	}
		
	
}

 

Posted (edited)
if (!getInventory().isFull() && !Jug.contains(myPlayer())){
	return state.WalkToBank;
}

That may be an issue.

Can I make a recommendation: stop using States. Read my thread on this topic.

I think you can improve the structure of your logic:

Player me = myPlayer();

if (Bank.contains(me)) {
	if (getInventory().isFull()) {
		bankItems();
	} else {
		goToJug();
	}
} else if (Jug.contains(me)) {
	if (getInventory().isFull()) {
		goToBank();
	} else {
		lootJugs();
	}
}

 

Edited by liverare
Posted
5 minutes ago, liverare said:

if (!getInventory().isFull() && !Jug.contains(myPlayer())){
	return state.WalkToBank;
}

That may be an issue.

Can I make a recommendation: stop using States. Read my thread on this topic.

I think you can improve the structure of your logic:


Player me = myPlayer();

if (Bank.contains(me)) {
	if (getInventory().isFull()) {
		bankItems();
	} else {
		goToJug();
	}
} else if (Jug.contains(me)) {
	if (getInventory().isFull()) {
		goToBank();
	} else {
		lootJugs();
	}
}

Okay, Thank you...I seen Juggles do this so I thought it would be in a better format doing this. I will go ahead and redo this so it goes with the If, if else, else statements. Thanks for linking me and helping me. Hopefully changing this will make the script work.

 

Posted

In your original code, the TakeJug and WalkToBank states have the exact same conditions; so being last, the WalkToBank state will never be used.

Also forgot to 'break;' in the Bank state. Adding a break is important to prevent fallthru to the WalkToBank state.
I'm not a fan of this pattern either, though. So I'd also recommend with something along the lines of what liverare suggested. But remember to still account for the scenario of being in neither of the areas

Posted
On 2/6/2018 at 10:56 AM, FrostBug said:

In your original code, the TakeJug and WalkToBank states have the exact same conditions; so being last, the WalkToBank state will never be used.

Also forgot to 'break;' in the Bank state. Adding a break is important to prevent fallthru to the WalkToBank state.
I'm not a fan of this pattern either, though. So I'd also recommend with something along the lines of what liverare suggested. But remember to still account for the scenario of being in neither of the areas

Got it, so I will go ahead and change to the if, else if and else way but what would you suggest me putting in between it? a if statement stating if its not in neither locations go to "this location" and then start the process?

Posted
4 hours ago, Toxic said:

Got it, so I will go ahead and change to the if, else if and else way but what would you suggest me putting in between it? a if statement stating if its not in neither locations go to "this location" and then start the process?

You could just add an else at the end to go to the jug area again seeing as if it gets stuck and isn't in either area both if statements wouldn't work.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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