Jump to content

Buying items from general store


bumzag

Recommended Posts

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?

Link to comment
Share on other sites

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/ 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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

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

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

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.

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