1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Resource Health Manager - Never deal with health methods again!

Discussion in 'Tutorials & Resources' started by Viewer, Mar 15, 2015.

  1. Viewer

    Viewer Discretion is advised

    Joined:
    Jan 2, 2014
    Messages:
    306
    Likes Received:
    77
    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:
    Code (Javascript):
    1. private final HealthManager healthManager = new HealthManager();
    And then it is ready to use:
    Code (Javascript):
    1.  
    2. if (healthManager.getHealthPercent() < 30) {
    3.     if (healthManager.hasFood()) {
    4.          healthManager.heal();
    5.       }
    6. }
    7.  
    HealthManager.java
    Code (Javascript):
    1. public class HealthManager {
    2.  
    3.     private GameMode current = null;
    4.  
    5.     private SpriteItem food = null;
    6.  
    7.     public HealthManager() {
    8.         if (Environment.isRS3()) {
    9.             if (CombatMode.LEGACY.isCurrent()) {
    10.                 current = GameMode.RS3_LEGACY;
    11.             } else {
    12.                 current = GameMode.RS3_EOC;
    13.             }
    14.         } else {
    15.             current = GameMode.OSRS;
    16.         }
    17.     }
    18.  
    19.     private enum GameMode {
    20.         RS3_EOC,
    21.         RS3_LEGACY,
    22.         OSRS
    23.     }
    24.  
    25.     public int getCurrentHealth() {
    26.         InterfaceComponent currentHealth = null;
    27.         switch (current) {
    28.             case RS3_EOC:
    29.                 currentHealth = Interfaces.getAt(1430, 4, 7);
    30.                 break;
    31.             case RS3_LEGACY:
    32.                 currentHealth = Interfaces.getAt(1504, 3, 7);
    33.                 break;
    34.             case OSRS:
    35.                 return Skill.CONSTITUTION.getCurrentLevel();
    36.         }
    37.         if (currentHealth != null && currentHealth.isVisible()) {
    38.             return Integer.parseInt(current.equals(GameMode.RS3_EOC) ? currentHealth.getText().split("/")[0] : currentHealth.getText());
    39.         }
    40.         return 0;
    41.     }
    42.  
    43.     public int getTotalHealth() {
    44.         switch (current) {
    45.             case RS3_EOC:
    46.                 final InterfaceComponent totalHealth = Interfaces.getAt(1430, 4, 7);
    47.                 if (totalHealth != null && totalHealth.isVisible()) {
    48.                     return Integer.parseInt(totalHealth.getText().split("/")[1]);
    49.                 }
    50.                 break;
    51.             case RS3_LEGACY:
    52.                 return Skill.CONSTITUTION.getBaseLevel() * 10;
    53.             case OSRS:
    54.                 return Skill.CONSTITUTION.getBaseLevel();
    55.         }
    56.         return 0;
    57.     }
    58.  
    59.     public int getHealthPercent() {
    60.         final double currentHealth = getCurrentHealth(), totalHealth = getTotalHealth();
    61.         return totalHealth > 0 ? (int) ((currentHealth / totalHealth) * 100) : 0;
    62.     }
    63.  
    64.     public boolean hasFood() {
    65.         food = Inventory.getItems(new Filter<SpriteItem>() {
    66.             @Override
    67.             public boolean accepts(SpriteItem spriteItem) {
    68.                 final ItemDefinition definition = spriteItem.getDefinition();
    69.                 if (definition != null) {
    70.                     final String name = definition.getName().toLowerCase();
    71.                     final List<String> actions = definition.getInventoryActions();
    72.                     if (!actions.isEmpty()) {
    73.                         return (actions.contains("Eat") && !name.contains("burnt")) || name.contains("saradomin brew");
    74.                     }
    75.                 }
    76.                 return false;
    77.             }
    78.         }).first();
    79.         return food != null;
    80.     }
    81.  
    82.     public boolean heal() {
    83.         final SpriteItem theFood = food;
    84.         if (theFood != null) {
    85.             if (theFood.click()) {
    86.                 return Execution.delayUntil(new Callable<Boolean>() {
    87.                     @Override
    88.                     public Boolean call() throws Exception {
    89.                         return !theFood.isValid();
    90.                     }
    91.                 }, 1500);
    92.             }
    93.         }
    94.         return false;
    95.     }
    96.  
    97.  
    98. }
     
    LucasSousa likes this.
  2. SlashnHax

    Joined:
    Dec 10, 2014
    Messages:
    3,210
    Likes Received:
    1,042

Share This Page

Loading...