Jump to content

Script wont check getHealthPercent correctly


Recommended Posts

Posted (edited)
public void eat() throws InterruptedException {
    log("Checking Health");
    if (myPlayer().getHealthPercent() < 99){
        log("Health is not 100%");
        if (getInventory().contains("Shark")){
            log("Eating Shark");
            getInventory().interact("Eat", "Shark");
            new ConditionalSleep(1000, 100) {
                @Override
                public boolean condition() throws InterruptedException {
                    return (myPlayer().getHealthPercent() == 100);
                }
            }.sleep();
        } else {
            if (getInventory().contains("Varrock teleport")){
                log("Teleporting to Varrock");
                getInventory().interact("Break", "Varrock teleport");
                new ConditionalSleep(1000, 100) {
                    @Override
                    public boolean condition() throws InterruptedException {
                        return (!getCombat().isFighting());
                    }
                }.sleep();
            }
        }
    }
}
Edited by investmentideas
Posted
8 minutes ago, Token said:

It only works in combat, the value is cached. If no value has been cached yet, it should return 100. Use skills#getDynamic & skills#getStatic to compute your own health percent.

didnt know, thanks for that! I changed to use static and dynamic, works like a treat, might not be the cleanest but works:

 

private int getHpPercent() {
    int staticHp = getSkills().getStatic(Skill.HITPOINTS);
    int dynamicHp = getSkills().getDynamic(Skill.HITPOINTS);
    int hpPercent = 100 * (dynamicHp / staticHp);
    return hpPercent;
}

public void eat() throws InterruptedException {
    int hpThreshold = 100;
    log("Checking Health");
    if (getHpPercent() < hpThreshold){
        log("Health is not 100%");
        if (getInventory().contains("Shark")){
            log("Eating Shark");
            getInventory().interact("Eat", "Shark");
            new ConditionalSleep(1000, 100) {
                @Override
                public boolean condition() throws InterruptedException {
                    return (myPlayer().getHealthPercent() == 100);
                }
            }.sleep();
        } else {
            if (getInventory().contains("Varrock teleport")){
                log("Teleporting to Varrock");
                getInventory().interact("Break", "Varrock teleport");
                new ConditionalSleep(1000, 100) {
                    @Override
                    public boolean condition() throws InterruptedException {
                        return (!getCombat().isFighting());
                    }
                }.sleep();
            }
        }
    }
}
  • Boge 1
Posted
12 minutes ago, investmentideas said:

didnt know, thanks for that! I changed to use static and dynamic, works like a treat, might not be the cleanest but works:

 


private int getHpPercent() {
    int staticHp = getSkills().getStatic(Skill.HITPOINTS);
    int dynamicHp = getSkills().getDynamic(Skill.HITPOINTS);
    int hpPercent = 100 * (dynamicHp / staticHp);
    return hpPercent;
}

public void eat() throws InterruptedException {
    int hpThreshold = 100;
    log("Checking Health");
    if (getHpPercent() < hpThreshold){
        log("Health is not 100%");
        if (getInventory().contains("Shark")){
            log("Eating Shark");
            getInventory().interact("Eat", "Shark");
            new ConditionalSleep(1000, 100) {
                @Override
                public boolean condition() throws InterruptedException {
                    return (myPlayer().getHealthPercent() == 100);
                }
            }.sleep();
        } else {
            if (getInventory().contains("Varrock teleport")){
                log("Teleporting to Varrock");
                getInventory().interact("Break", "Varrock teleport");
                new ConditionalSleep(1000, 100) {
                    @Override
                    public boolean condition() throws InterruptedException {
                        return (!getCombat().isFighting());
                    }
                }.sleep();
            }
        }
    }
}

Make sure you cast to float/double when doing the division, your method should return only 0 or 100 if I'm not wrong

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