Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Script wont check getHealthPercent correctly

Featured Replies

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

  • Author

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

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.

  • Author
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();
            }
        }
    }
}
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

Couldn't you just say 

if (myPlayer().getHealthPercent() < 50 && getInventory().contains("Shark")) { Health % below 50 & Inventory contains sharks
        log("I got an owie, let's eat!");
        getInventory().interact("Eat", "Shark"); Eats a shark
    }
}

 

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.