Jump to content

Eat food if < 35% hp


Recommended Posts

Posted

I can't get the script to eat food. Any help? 😛

 

public int onLoop() throws InterruptedException {
                
                if (myPlayer().getHealthPercent() < 35) {
                    eatFood();
                    sleep(random(500, 1000));
                }

            return 0;
        }

 

public void eatFood() throws InterruptedException {
            if (getInventory().contains("Shrimps")) {            
            
            getInventory().getItem("Shrimps").interact("Eat");
                
            }
            else {
                goBank();
                sleep(random(1000, 2000));
                goMonsterLoc();
            }
        }
 

  • 2 weeks later...
Posted (edited)
On 7/19/2019 at 9:54 AM, jajaXd said:

I can't get the script to eat food. Any help? 😛

 

public int onLoop() throws InterruptedException {
                
                if (myPlayer().getHealthPercent() < 35) {
                    eatFood();
                    sleep(random(500, 1000));
                }

            return 0;
        }

 

public void eatFood() throws InterruptedException {
            if (getInventory().contains("Shrimps")) {            
            
            getInventory().getItem("Shrimps").interact("Eat");
                
            }
            else {
                goBank();
                sleep(random(1000, 2000));
                goMonsterLoc();
            }
        }
 


your timer seems a bit silly because eating takes 3 ticks. you're more than welcome to do random (1800, 2400), but random doesn't really matter much anyways
here's two ways you can go about it, but it's virtually the exact same code just done differently. untested, but it should work
you're also better off checking if your player is in an Area instead of checking if the bank booth is null. or you can use map.distance and a null check

as for everyone else, it is infact "Shrimps" in-game. it doesn't take much effort to check it out on the wikia for the correct name, instead of telling op he got it wrong

	Position VarrockWestBank = new Position(3184, 3436, 0);

	int health;

	boolean invHas(String item) { return inventory.contains(item); }
	boolean eat(String name) { return inventory.interact("Eat", name); }
	boolean objExists(String obj) { return objects.closest(obj) != null; }

	String booth = "Bank booth", food = "Shrimps";

	public int onLoop() throws InterruptedException {
		health = myPlayer().getHealthPercent();
		
		if (health <= 35 && invHas(food)) {
			eat(food);
			sleep(1800);
		} else if (health <= 50 && !invHas(food) && !objExists(booth)) {
			walking.webWalk(VarrockWestBank);
		}
		return 200;
	}
	Position VarrockWestBank = new Position(3184, 3436, 0);

	public int onLoop() throws InterruptedException {
		if (myPlayer().getHealthPercent() <= 35 && inventory.contains("Shrimps")) {
			inventory.interact("Eat", "Shrimps");
			sleep(1800);
		} else if (myPlayer().getHealthPercent() <= 50 && !inventory.contains("Shrimps") && objects.closest("Bank booth") == null) {
			walking.webWalk(VarrockWestBank);
		}
		return 200;
	}

 

edit: you can also choose to not declare certain positions like I did, and you can simple do - walking.webWalk(Banks.VARROCK_WEST);

Edited by qmqz

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