Jump to content

Buying items from general store


Recommended Posts

Posted

Alright so I've evolved from cow killing and knife looting, I'm trying to make a shop buying script. I'm at the beginning right now, all I'm trying to do is test the simple function of interacting with the shop owner and clicking "Trade", and then buying 5 shovels from him. But nothing happens.

        NPC shopKeeper = getNpcs().closest("Shop keeper");
        if (shopKeeper != null && shopKeeper.exists()) {
            shopKeeper.interact("Trade");
            getStore().buy("Spade, 5);
        }

What am I doing wrong?

Posted
1 hour ago, FuryShark said:

Make sure the shop is open before you try buying things
getStore().isOpen()

getStore().buy("Spade     your missing a " at the end of spade...

 

Use conditional sleeps so you dont end up spam clicking trade

https://osbot.org/forum/topic/127193-conditional-sleep-with-lambda-expressions/ 

Like this?

        NPC shopKeeper = getNpcs().closest("Shop keeper");
        if (shopKeeper != null && shopKeeper.exists()) {
            shopKeeper.interact("Trade");
            if(getStore().isOpen())
                getStore().buy("Spade", 5);
        }

Still not yielding anything

Posted (edited)
22 minutes ago, bumzag said:

Like this?


        NPC shopKeeper = getNpcs().closest("Shop keeper");
        if (shopKeeper != null && shopKeeper.exists()) {
            shopKeeper.interact("Trade");
            if(getStore().isOpen())
                getStore().buy("Spade", 5);
        }

Still not yielding anything

Works for me, try adding logs to see where your getting stuck.

Also make sure your pressing refresh before trying to run it after you export a new version of a script. 

Edited by FuryShark
Posted (edited)

Yeah dude idk what's going on. It trades with the shop keeper, outputs the log, waits a second, closes the shop and repeats. But it won't buy.

Edit: on second thought, the log in the if(getStore().isOpen()) doesn't output, there fore it doesn't buy either. Also, I did refresh on the script GUI, and I know it did take the rebuild of the jar file because I changed the log string that's outside the if(getStore().isOpen() statement.

Edited by bumzag
  • Sad 1
Posted

You want to do a conditional sleep on the opening of the store:

if(shopKeeper.interact("Trade"){
  sleep.SleepUntil(../*getStore.isOpen()*/..);
  getStore().buy("Spade", 5);
}
 

The issue I believe is that when the "getStore.isOpen()" call is made, the shop has not already opened (perhaps a few ms delay).  You could also try throwing a regular sleep inbetween if you didn't want to do a conditional sleep.

  • Like 1
Posted (edited)
		Position shop = new Position(###,###,###);

		if (s.myPlayer().getPosition().distance(shop) > 10) {
			WebWalkEvent event = new WebWalkEvent(shop);
			s.execute(event);
		}

		NPC ShopKeeper = s.npcs.closest("Shop keeper");

		if (ShopKeeper.exists()) {
			if (ShopKeeper.isVisible()) {
				if (!s.store.isOpen()) {
					ShopKeeper.interact("Trade");
					Sleep.sleepUntil(() -> s.store.isOpen(), 5000);

				}
			}
		}

		if (s.store.isOpen()) {
			if (!s.inventory.contains("Spade")) {
				Sleep.sleepUntil(() -> s.store.buy("Spade", 5), 5000);
			}
		}

 

Edited by Naked
  • Sad 1
  • Heart 1
Posted
26 minutes ago, dreameo said:

You want to do a conditional sleep on the opening of the store:


if(shopKeeper.interact("Trade"){
  sleep.SleepUntil(../*getStore.isOpen()*/..);
  getStore().buy("Spade", 5);
}
 

The issue I believe is that when the "getStore.isOpen()" call is made, the shop has not already opened (perhaps a few ms delay).  You could also try throwing a regular sleep inbetween if you didn't want to do a conditional sleep.

Thanks man, it was simple as a sleep statement. I'll have to work on ConditionalSleeps. Thank you.

 

18 minutes ago, Naked said:

		Position shop = new Position(###,###,###);

		if (s.myPlayer().getPosition().distance(shop) > 10) {
			WebWalkEvent event = new WebWalkEvent(shop);
			s.execute(event);
		}

		NPC ShopKeeper = s.npcs.closest("Shop keeper");

		if (ShopKeeper.exists()) {
			if (ShopKeeper.isVisible()) {
				if (!s.store.isOpen()) {
					ShopKeeper.interact("Trade");
					Sleep.sleepUntil(() -> s.store.isOpen(), 5000);

				}
			}
		}

		if (s.store.isOpen()) {
			if (!s.inventory.contains("Hammer")) {
				Sleep.sleepUntil(() -> s.store.buy("Spade", 5), 5000);
			}
		}

 

Thank you for writing that out. Can you elaborate on stuff like "s.store.isOpen()" and "s.myPlayer().getPosition().distance(shop)" and "s.npcs.closest("Shop keeper")"? I don't understand the "s." portion of it. Where is that coming from?

Posted
21 minutes ago, bumzag said:

Thank you for writing that out. Can you elaborate on stuff like "s.store.isOpen()" and "s.myPlayer().getPosition().distance(shop)" and "s.npcs.closest("Shop keeper")"? I don't understand the "s." portion of it. Where is that coming from?

You can ignore those in your case. I just copy and pasted some code. 


The s is short for script, a variable in my Task class. The class this was from extended the Task class.

TL;DR ignore it

Posted
6 minutes ago, Naked said:

You can ignore those in your case. I just copy and pasted some code. 


The s is short for script, a variable in my Task class. The class this was from extended the Task class.

TL;DR ignore it

Ignore them in the sense that I can erase them out of the code?

Posted (edited)
1 hour ago, bumzag said:

Ignore them in the sense that I can erase them out of the code?

Right so enter the position and this code should work for you. This uses this sleep class: 

 

 

		Position shop = new Position(###,###,###);

		if (myPlayer().getPosition().distance(shop) > 10) {
			WebWalkEvent event = new WebWalkEvent(shop);
			execute(event);
		}

		NPC ShopKeeper = npcs.closest("Shop keeper");

		if (ShopKeeper.exists()) {
			if (ShopKeeper.isVisible()) {
				if (!store.isOpen()) {
					ShopKeeper.interact("Trade");
					Sleep.sleepUntil(() -> store.isOpen(), 5000);

				}
			}
		}

		if (store.isOpen()) {
			if (!inventory.contains("Spade")) {
				Sleep.sleepUntil(() -> store.buy("Spade", 5), 5000);
			}
		}

 

Edited by Naked
  • Like 1
Posted
9 hours ago, Naked said:

Right so enter the position and this code should work for you. This uses this sleep class: 

 

 


		Position shop = new Position(###,###,###);

		if (myPlayer().getPosition().distance(shop) > 10) {
			WebWalkEvent event = new WebWalkEvent(shop);
			execute(event);
		}

		NPC ShopKeeper = npcs.closest("Shop keeper");

		if (ShopKeeper.exists()) {
			if (ShopKeeper.isVisible()) {
				if (!store.isOpen()) {
					ShopKeeper.interact("Trade");
					Sleep.sleepUntil(() -> store.isOpen(), 5000);

				}
			}
		}

		if (store.isOpen()) {
			if (!inventory.contains("Spade")) {
				Sleep.sleepUntil(() -> store.buy("Spade", 5), 5000);
			}
		}

 

Ty for taking the time to answer. Makes a lot more sense now.

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