Jump to content

Stuck in p2p world hop loop


uyfgfarOS

Recommended Posts

Hi guys, creating a script to buy grapes & hop. All working fine except when it hops it will be stuck in the hopping method. I've tried using counters to counteract it being stuck inside the method but I can't seem to get it out.

package testBuyer;

import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

import testBuyer.main.State;

import java.awt.*;

import javax.swing.text.html.parser.Entity;

@ScriptManifest(info = "GRAPE BUYER", version = 1.0, logo = "", author = "MATT", name = "Test2")
public class main extends Script {
	int grapeCount = 1;
	
	enum State {
		OPENCHEST, BUYGRAPES, BANK, HOP;
	}

	public State state;
		
	public void onStart(){
		
	if(getInventory().isFull())
	{
		log("FULL - BANKING");
			state = State.BANK;
		
	}
	
	if (getInventory().onlyContains("Coins"))
	{
		log("ONLY COINS IN INVENT");
		state = State.OPENCHEST;
		log("OPEN CHEST");
	}
	
	 if(getStore().isOpen())
	 {
		 state = State.BUYGRAPES;
	 }
	 
	 if (grapeCount == 0)
	 {
		 log("out of grapes");
		 state = State.HOP;
	 }
	 
	 if (getInventory().contains("Coins") && getInventory().contains("Grapes") && !getInventory().isFull())
	 {
		 log("Invent has coins & grapes, but isn't full.");
		 state = State.OPENCHEST;
	 }
	}
	
	public int onLoop() throws InterruptedException{
		switch (state){
		case OPENCHEST:
			return openChest();
		case BUYGRAPES:
			return buyGrapes();
		case BANK:
			return bank();
		case HOP:
			return hopping();
	
		}
	return random(100, 220);
	}

	int openChest() throws InterruptedException {		
		objects.closest("Chest").interact("Buy-food");
		sleep(900);	
		if (getInventory().contains("Coins") && !getInventory().isFull())
		{			
				log("Changing state to buying");
				state = State.BUYGRAPES;		
		}
		return 0;
	}

	int buyGrapes() throws InterruptedException
	{
		if (store.isOpen()) {
			log("store is open");
			sleep(300);
			RS2Widget grapeWidge = getWidgets().get(300, 16, 5);
			if (grapeWidge != null)
			{
				grapeCount = grapeWidge.getItemAmount();
				if (grapeCount > 0)
				{
					log("Shop in stock, buying");
					grapeWidge.interact("Buy 10");
					sleep(400);	
				}	
				else
				{						
					state = State.HOP;
				}
			}								
	
		}
		return 0;
		
			
	}
	
	int hopping() throws InterruptedException
	{
		log("hopping");		
		getWorlds().hopToP2PWorld();	
		grapeCount = 1;
		return 0;
	}

	int bank() throws InterruptedException{
		getBank().depositAllExcept("Coins");
		sleep(300);
		return 0;	
	}
	
	
}

Here's what the log shows, initially when entering the hopping method it will show the log "hopping" twice, instantly. Followed by loads. 

 

[INFO][Bot #1][01/10 11:26:05 AM]: hopping
[INFO][Bot #1][01/10 11:26:05 AM]: hopping
[INFO][Bot #1][01/10 11:26:05 AM]: hopping
[INFO][Bot #1][01/10 11:26:05 AM]: hopping
[INFO][Bot #1][01/10 11:26:05 AM]: hopping
[INFO][Bot #1][01/10 11:26:05 AM]: hopping

Thanks a lot guys.

Edited by uyfgfarOS
  • Like 1
Link to comment
Share on other sites

6 minutes ago, Nbacon said:

Hello Matt,

You never change state in hopping or bank... so that is why.

 

 

 

 

Hi mate, thanks for the reply. In my mind I thought the loop would take care of changing states.

I have added a state change into both hopping & banking now. But a little unsure. My hopping method looks like this now:

 

int hopping() throws InterruptedException
	{
		int hopCount = 0;
		log("hopping");	
		
		while (hopCount == 0)
		{		
		getWorlds().hopToP2PWorld();
		hopCount = 1;
		}
		state = State.OPENCHEST;
		
		return 0;
	}

I can appreciate I probably don't need the while loop in there, I was just trying something out as I changed state to OPENCHEST, hoping it would hop and then open chest, but it opens the world viewer and then clicks the chest, without hopping. Apologies if it's dumb on my part I've been out of coding for about 3 years :)

Link to comment
Share on other sites

3 minutes ago, uyfgfarOS said:

I can appreciate I probably don't need the while loop in there, I was just trying something out as I changed state to OPENCHEST, hoping it would hop and then open chest, but it opens the world viewer and then clicks the chest, without hopping. Apologies if it's dumb on my part I've been out of coding for about 3 years :)

no no no no no no......

while (hopCount == 0)
		{		
		getWorlds().hopToP2PWorld();
		hopCount = 1;
		}

this is just an if statment that is (logic like this can lead to forever loops if your not carefull)

if (true){

do something.....

}

and thats just 

do something.....

ps

 I think this will work better  (put this is loop) and make loop return some number like 1500 

Quote

if (inventory.isFull()) {
            if (bank.isOpen()) {
                bank.depositAllExcept("Coins");
            } else {
                bank.open();
            }

        } else {
            if (bank.isOpen()) {
                bank.close();
            } else {
                if (getStore().isOpen()) {
                    if (getStore().getAmount("Grapes") == 0) {
                        getStore().close();
                        Thread.sleep(random(1500, 3350));//this is bad form
                        getWorlds().hopToP2PWorld();

                    } else {
                        getStore().buy("Grapes", 1000);
                    }
                } else {
                    RS2Object chest = objects.closest("Chest");
                    if (chest != null) {
                        chest.interact("Buy-food");
                    }
                        

                }

            }
        }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

7 minutes ago, Nbacon said:

no no no no no no......


while (hopCount == 0)
		{		
		getWorlds().hopToP2PWorld();
		hopCount = 1;
		}

this is just an if statment that is (logic like this can lead to forever loops if your not carefull)


if (true){

do something.....

}

and thats just 


do something.....

ps

 I think this will work better  (put this is loop) and make loop return some number like 1500 

 

 

 

 

 

 

 

 

 

 

 

 

 

Cheers for your patience. I can see this is a lot simpler now, I think the whole switch case thing was confusing me. Only thing I had to change from your code was the fact that the Culinaromancer chest doesn't like the buy option, so I have to use widgets.

Also once my inventory is full it's sat spamming on the grapes (0) to buy them. That's something I'll try figure out.

Thankyou!

Link to comment
Share on other sites

My tip if your going to do switch statment thing draw it on paper and make sure all A->B->something->Z  loop back to A or in better words all paths have a cycle to a start state.

 

 

oh I see an error in my code from what you said.

if (inventory.isFull()) {
            if (bank.isOpen()) {
                bank.depositAllExcept("Coins");
            } else {
                bank.open();
            }

        } else {

to

   if (inventory.isFull()) {

            if (getStore().isOpen()) {
                getStore().close();
            }else{
                if (bank.isOpen()) {
                    bank.depositAllExcept("Coins");
                } else {
                    bank.open();
                }
            }
        } else {

 

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...