Jump to content

Scripting Help


Toxic

Recommended Posts

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);
	}
		
	
}

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Player me = myPlayer();

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

 

  • Like 1
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...