Jump to content
Sophisticated

Need a way to give my methods 'time' to finish before attempting to go on with the rest of the code.

Recommended Posts

Hi guys, I'm trying to make a simple script that makes unfinished potions.  The script isn't finished yet, but at the moment I'm working on 3 different ways of withdrawing items from the bank. The aim is to withdraw 14 items, but with 3 different options. The most common way for me would be by withdrawing 14 items, followed by grabing 10 items and clicking 4 times on the same item (to make 14). And the least common way of withdrawing would be by using the Withdraw-X option.

Now I noticed a few times my script keeps on running before "finishing" what I want it to finish first. I'm trying the part of the code that would withdraw using the withdraw-X option, but alot of the times the script isn't even able to withdraw the items and just continues with the next task. Now I've read about conditionalWaiting and I'm not sure if this is the right option to handle it and I'm not sure how to implement it.

So my question to you guys is: How exactly can I make my code 'pause'/wait till a task is finished, before attempting to go on with the next task? (code is below)

I also have an off-topic question about the human mouse movement and Banking.withdraw(); I've bought VIP extended, whenever I run a script, am I automatically using the human mouse movements? And let's say when I'm using Banking.withdraw() and clicking on a withdraw option, is the movement also random/human like?

I'm hoping someone can help me out.

 

The part I'm talking about sits in withdrawIngredients();

package scripts;

import org.tribot.api.DynamicClicking;
import org.tribot.api.General;
import org.tribot.api.input.Keyboard;
import org.tribot.api2007.Banking;
import org.tribot.api2007.Interfaces;
import org.tribot.api2007.Inventory;
import org.tribot.api2007.types.RSInterface;
import org.tribot.api2007.types.RSItem;
import org.tribot.api2007.types.RSNPC;
import org.tribot.script.Script;
import org.tribot.script.ScriptManifest;

@ScriptManifest(authors = {
		"Sophisticated" }, category = "Herblore", name = "Sophisticated's Potion Maker", version = 1.00, description = "Makes dem pots XD.", gameMode = 1)
public class Herblore extends Script
{

	// Withdraws the ingredients
	// TODO: Random kliks op de knoppen
	private void withdraw()
	{
		if (!Banking.isBankScreenOpen())
		{
			int random = General.random(0, 30);
			if (random != 0)
			{
				println("Standard openBank()");
				Banking.openBank();
			}
			else
			{
				println("Right-click open bank!");
				rightClickOpenBank();
			}
			sleep(830, 1120);
		}
		else if (Banking.isBankScreenOpen() && !Inventory.isFull())
		{
			RSItem[] herb = Banking.find(257);
			RSItem[] waterVial = Banking.find(227);

			RSItem[] herbInventory = Inventory.find(257);
			RSItem[] waterVialInventory = Inventory.find(227);

			if (herb[0].getStack() >= 14 && herbInventory.length == 0)
			{
				withdrawIngredients(257, herb[0]);
				sleep(300, 450);
			}
			if (waterVial[0].getStack() >= 14 && waterVialInventory.length == 0)
			{
				withdrawIngredients(227, waterVial[0]);
				sleep(1322, 1450);
			}
		}
	}

	int withdrawCalled = 0;

	private void withdrawIngredients(int itemID, RSItem item)
	{
		withdrawCalled++;
		println("Withdraw Ingredients is called: " + withdrawCalled + " times.");

		// int random = General.random(0, 30);
		int random = 20;
		if (random != 1 && random != 2 && random != 3 && random != 20 && random != 21)
		{
			// Standard withdraw
			Banking.withdraw(14, itemID);
		}
		else if (random == 1 || random == 2 || random == 3)
		{
			// Withdraws 10 + (4x1)
			Banking.withdraw(10, itemID);
			sleep(80, 280);
			Banking.withdrawItem(item, 1);
			sleep(20, 120);
			Banking.withdrawItem(item, 1);
			sleep(20, 120);
			Banking.withdrawItem(item, 1);
			sleep(20, 120);
			Banking.withdrawItem(item, 1);
			sleep(20, 120);
		}
		else if (random == 20 || random == 21)
		{
			// Withdraws 14 using "Make X"
			item.click("Withdraw-X");
			RSInterface target = Interfaces.get(162, 32);
			sleep(250, 400);
			if (target != null && !target.isHidden())
			{
				Keyboard.typeSend("14");
				sleep(200, 320);
				Keyboard.pressEnter();
				sleep(200, 320);
			}
			else
			{
				println("TARGET NULL or HIDDEN!");
			}
		}
	}

	// Opens the bank using right click.
	private void rightClickOpenBank()
	{
		int random = General.random(0, 22);
		final RSNPC[] bankers = org.tribot.api2007.NPCs.findNearest("Banker");
		if (bankers.length > 1)
		{
			if (bankers[0].isOnScreen() && random != 0)
			{
				// Not sure if dynamic clicking is the right way to handles this
				// since the banker isn't moving.
				DynamicClicking.clickRSNPC(bankers[0], "Bank Banker");
			}
			else if (bankers[0].isOnScreen() && random == 0)
			{
				DynamicClicking.clickRSNPC(bankers[0], "Talk-to Banker");
				sleep(80, 200);
				DynamicClicking.clickRSNPC(bankers[0], "Bank Banker");
				sleep(320, 610);
			}
		}
	}

	// Returns true if we got the ingredients.
	private boolean gotIngredients()
	{
		RSItem[] herb = Inventory.find(257);
		RSItem[] waterVial = Inventory.find(227);
		if (herb.length == 14 && waterVial.length == 14)
		{
			return true;
		}
		return false;
	}

	// Makes the unf potions.
	// TODO: random shit etc en maybe cond waiting & random kliks!!!
	public void makePots()
	{
		// 10 op 0 vaakste
		// soms 11 op 1
		// en vaak 13 op 0
		// soms een random van alle herbs (op de 4 eerste na) op één van de eerste 5
		// vials
		// use herb 13 op vial 0 (soms ook een andere op een andere)
		RSItem[] herb = Inventory.find(257);
		herb[0].click(); // need to add randomness
		RSItem[] vial = Inventory.find(227);
		vial[0].hover();
		vial[0].click(); // need to add randomness
		println(herb.length);

		RSInterface target = Interfaces.get(309, 3);

		if (target != null && !target.isHidden())
		{
			println("niet null");
			target.click("Make All");
		}
	}

	// Returns true if the (unf) pots are made.
	public boolean potsAreMade()
	{
		RSItem[] unfPots = Inventory.find(99);
		if (unfPots.length == 14)
		{
			return true;
		}
		return false;
	}

	// Banks the (unf) potions.
	public void bankPots()
	{
		if (!Banking.isBankScreenOpen())
		{
			Banking.openBank();
		}
		else if (Banking.isBankScreenOpen())
		{
			Banking.depositAll();
		}
	}

	@Override
	public void run()
	{
		while (true)
		{
			sleep(50);

			if (!gotIngredients())
			{
				withdraw();
			}
			else if (gotIngredients() && Banking.isBankScreenOpen())
			{
				Banking.close();
			}
			else if (gotIngredients() && !Banking.isBankScreenOpen())
			{
				makePots();
			}
			else if (potsAreMade())
			{
				bankPots();
			}
		}
	}
}

 

Share this post


Link to post
Share on other sites

You are in serious need of replacing them sleeps. Use conditional sleeps instead, for example in your withdraw method where you are trying to open up the bank, replace the sleep(830, 1120) with

Timing.waitCondition(new Condition() {
@Override
public boolean active() {
General.sleep(50, 150);
return Banking.isBankScreenOpen();
}, General.random(3000, 5000));

Also in the same method, where you are looking for the items in the bank, you have to make sure you check the length is greater than 0. Lets say you don't have any herb's in the bank, Banking.find(257) will return an empty array, you then try get the stack of index 0 but it doesn't exist because an empty array was returned, so you end up getting a NPE.

The way you're separating up your withdraw if statements can be simplified by using inequalities.

Again in the withdraw-X part, replace the static sleep with another conditional sleep.

RSInterface target = Interfaces.get(162, 32);

if (Timing.waitCondition(new Condition() {
@Override
public boolean active() {
General.sleep(50, 150);
return target != null && !target.isHidden();
}}, General.random(3000, 5000))) { // type }

In gotIngredients you can just call Inventory.getCount(id) == 14 instead of creating the RSItem.

And yes, whenever the bot moves the mouse from point a to point b with human mouse movement activated the movement will be random/human like, whether you use Mouse.move() or using another API method. You should also try using conditional sleeps instead of static sleeps, as static sleeps are so unreliable and easily form patterns.

Edited by fegmo
  • Like 1

Share this post


Link to post
Share on other sites

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

  • Our picks

    • This release will:

      Fix some ClosedChannelException bug


      Fix bug in RSObject#getAllTiles


      Add game tab support for "Kourend Favour"
        • Like
      • 8 replies
    • This release will:

      Fix Settings UI placement bug


      Fix game object location bug


      Fix small layout bug making the client shift up and down


      Fix client crashing bug where loading the client with a small display area will cause the client to crash


      Fix annoying Linux bug relating to painting events and peers


      Fix settings saving bug where settings are saved to disk more often than they should


      Fix RSInterface#isBeingDrawn bug affecting a limited amount of people


      Drop Java 1.7 bytecode version for 1.8


      Important: Since the downloadable RS client uses Java 7, it will no longer be compatible with Looking Glass. To make up for this, we will add support for using other clients such as RuneLite (at a later date).


      This change was necessary to allow us to use Java 8 syntax. It also paves the way for Java 9/10/11 support.
        • Like
      • 40 replies
    • This update will:

      Fix the RSMenuNode bug which also fixes the bug with bank opening


      Fix the incorrect object positions bug


      Fix and re-enable the LG Objects API Accelerator


      Fix the RSObject#getAllTiles bug
        • Like
      • 22 replies
    • Try our development release by checking "Development Release" on the TRiBot Loader. Note that these new features are currently in beta.

      This release features:

      Re-sizable mode support for both LG and the regular client


      Slightly improved login bot


      Removed final access modifiers from API classes


      Added RSServer hook wrapper to get the client's cached list of server/world info


      [NEW] Bug fix for intelligent banking


      [NEW] Improvement to the stability of LG over time


      [NEW] Vastly improved the reliability and speed of Screen#getColorAt on both LG and the regular client


      [NEW] Fix LG login problems


      [NEW] Fixed re-sizable mode container bug


      [NEW] Fixed re-sizable mode mouse bug


      [NEW] Use of public constants in the Banking API


      [NEW] Use of other various constants such as Projection#NULL_PT and Screen#EMPTY_COLOR



      More features to come very soon!

      Please test it and let us know here if there are any new bugs introduced in this release.
        • Thanks
        • Like
      • 12 replies
    • Try our development release by checking "Development Release" on the TRiBot Loader. Note that these new features are currently in beta.

      This release features:

      Re-sizable mode support for both LG and the regular client


      Slightly improved login bot


      Removed final access modifiers from API classes


      Added RSServer hook wrapper to get the client's cached list of server/world info


      Bug fix for intelligent banking


      Improvement to the stability of LG over time



      More features to come very soon!

      Please test it and let us know here if there are any new bugs introduced in this release.
        • Thanks
        • Like
      • 10 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×