Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Resource Health Manager - Never deal with health methods again!

Discretion is advised
Joined
Jan 2, 2014
Messages
306
As the name suggests, this class will make easier for you to get health methods for you bots, such as current life points, HP percentage, etc, also it has a method to eat any item that heals in your inventory, and you can check if you actually have food too. And the best part, it is compatible with any game version.

Example usage:

Declare it in your fields:
JavaScript:
private final HealthManager healthManager = new HealthManager();

And then it is ready to use:
JavaScript:
if (healthManager.getHealthPercent() < 30) {
    if (healthManager.hasFood()) {
         healthManager.heal();
      }
}

HealthManager.java
JavaScript:
public class HealthManager {

    private GameMode current = null;

    private SpriteItem food = null;

    public HealthManager() {
        if (Environment.isRS3()) {
            if (CombatMode.LEGACY.isCurrent()) {
                current = GameMode.RS3_LEGACY;
            } else {
                current = GameMode.RS3_EOC;
            }
        } else {
            current = GameMode.OSRS;
        }
    }

    private enum GameMode {
        RS3_EOC,
        RS3_LEGACY,
        OSRS
    }

    public int getCurrentHealth() {
        InterfaceComponent currentHealth = null;
        switch (current) {
            case RS3_EOC:
                currentHealth = Interfaces.getAt(1430, 4, 7);
                break;
            case RS3_LEGACY:
                currentHealth = Interfaces.getAt(1504, 3, 7);
                break;
            case OSRS:
                return Skill.CONSTITUTION.getCurrentLevel();
        }
        if (currentHealth != null && currentHealth.isVisible()) {
            return Integer.parseInt(current.equals(GameMode.RS3_EOC) ? currentHealth.getText().split("/")[0] : currentHealth.getText());
        }
        return 0;
    }

    public int getTotalHealth() {
        switch (current) {
            case RS3_EOC:
                final InterfaceComponent totalHealth = Interfaces.getAt(1430, 4, 7);
                if (totalHealth != null && totalHealth.isVisible()) {
                    return Integer.parseInt(totalHealth.getText().split("/")[1]);
                }
                break;
            case RS3_LEGACY:
                return Skill.CONSTITUTION.getBaseLevel() * 10;
            case OSRS:
                return Skill.CONSTITUTION.getBaseLevel();
        }
        return 0;
    }

    public int getHealthPercent() {
        final double currentHealth = getCurrentHealth(), totalHealth = getTotalHealth();
        return totalHealth > 0 ? (int) ((currentHealth / totalHealth) * 100) : 0;
    }

    public boolean hasFood() {
        food = Inventory.getItems(new Filter<SpriteItem>() {
            @Override
            public boolean accepts(SpriteItem spriteItem) {
                final ItemDefinition definition = spriteItem.getDefinition();
                if (definition != null) {
                    final String name = definition.getName().toLowerCase();
                    final List<String> actions = definition.getInventoryActions();
                    if (!actions.isEmpty()) {
                        return (actions.contains("Eat") && !name.contains("burnt")) || name.contains("saradomin brew");
                    }
                }
                return false;
            }
        }).first();
        return food != null;
    }

    public boolean heal() {
        final SpriteItem theFood = food;
        if (theFood != null) {
            if (theFood.click()) {
                return Execution.delayUntil(new Callable<Boolean>() {
                    @Override
                    public Boolean call() throws Exception {
                        return !theFood.isValid();
                    }
                }, 1500);
            }
        }
        return false;
    }


}
 
Top