Jump to content

Script wont check getHealthPercent correctly


investmentideas

Recommended Posts

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

this is the log

[INFO][Bot #2][03/30 06:00:50 PM]: Checking Health
[INFO][Bot #2][03/30 06:00:50 PM]: Checking Health
[INFO][Bot #2][03/30 06:00:51 PM]: Checking Health
[INFO][Bot #2][03/30 06:00:51 PM]: Checking Health

my health was 45% when the script was running, so it is not recognising my health is < 99

Link to comment
Share on other sites

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

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

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