SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 121 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  /*
  2.  * Copyright (c) 2016-2018, Adam <Adam@sigterm.info>
  3.  * Copyright (c) 2018, Jordan Atwood <jordan.atwood423@gmail.com>
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright notice, this
  10.  *    list of conditions and the following disclaimer.
  11.  * 2. Redistributions in binary form must reproduce the above copyright notice,
  12.  *    this list of conditions and the following disclaimer in the documentation
  13.  *    and/or other materials provided with the distribution.
  14.  *
  15.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18.  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25.  */
  26. package net.runelite.client.plugins.Learning3;
  27.  
  28. import net.runelite.api.*;
  29. import net.runelite.client.game.HiscoreManager;
  30. import net.runelite.client.game.NPCManager;
  31. import net.runelite.client.ui.overlay.Overlay;
  32. import net.runelite.client.ui.overlay.OverlayMenuEntry;
  33. import net.runelite.client.ui.overlay.OverlayPosition;
  34. import net.runelite.client.ui.overlay.OverlayPriority;
  35. import net.runelite.client.ui.overlay.components.ComponentConstants;
  36. import net.runelite.client.ui.overlay.components.PanelComponent;
  37. import net.runelite.client.ui.overlay.components.ProgressBarComponent;
  38. import net.runelite.client.ui.overlay.components.TitleComponent;
  39. import net.runelite.client.util.Text;
  40. import net.runelite.http.api.hiscore.HiscoreResult;
  41.  
  42. import javax.inject.Inject;
  43. import java.awt.Point;
  44. import java.awt.*;
  45.  
  46. import static net.runelite.api.MenuAction.RUNELITE_OVERLAY_CONFIG;
  47. import static net.runelite.client.ui.overlay.OverlayManager.OPTION_CONFIGURE;
  48.  
  49. class OpponentInfoOverlay extends Overlay
  50. {
  51.     private static final Color HP_GREEN = new Color(0, 146, 54, 230);
  52.     private static final Color HP_RED = new Color(102, 15, 16, 230);
  53.  
  54.     private final Client client;
  55.     private final OpponentInfoPlugin opponentInfoPlugin;
  56.     private final OpponentInfoConfig opponentInfoConfig;
  57.     private final HiscoreManager hiscoreManager;
  58.     private final NPCManager npcManager;
  59.     public float hpp;
  60.     private final PanelComponent panelComponent = new PanelComponent();
  61.  
  62.     private Integer lastMaxHealth;
  63.     private int lastRatio = 0;
  64.     private int lastHealthScale = 0;
  65.     private String opponentName;
  66.     private String opponentsOpponentName;
  67.  
  68.     @Inject
  69.     private OpponentInfoOverlay(
  70.         Client client,
  71.         OpponentInfoPlugin opponentInfoPlugin,
  72.         OpponentInfoConfig opponentInfoConfig,
  73.         HiscoreManager hiscoreManager,
  74.         NPCManager npcManager)
  75.     {
  76.         super(opponentInfoPlugin);
  77.         this.client = client;
  78.         this.opponentInfoPlugin = opponentInfoPlugin;
  79.         this.opponentInfoConfig = opponentInfoConfig;
  80.         this.hiscoreManager = hiscoreManager;
  81.         this.npcManager = npcManager;
  82.  
  83.         setPosition(OverlayPosition.TOP_LEFT);
  84.         setPriority(OverlayPriority.HIGH);
  85.  
  86.         panelComponent.setBorder(new Rectangle(2, 2, 2, 2));
  87.         panelComponent.setGap(new Point(0, 2));
  88.         getMenuEntries().add(new OverlayMenuEntry(RUNELITE_OVERLAY_CONFIG, OPTION_CONFIGURE, "Opponent info overlay"));
  89.     }
  90.  
  91.     @Override
  92.     public Dimension render(Graphics2D graphics)
  93.     {
  94.         final Actor opponent = opponentInfoPlugin.getLastOpponent();
  95.  
  96.         if (opponent == null)
  97.         {
  98.             opponentName = null;
  99.             return null;
  100.         }
  101.  
  102.         if (opponent.getName() != null && opponent.getHealth() > 0)
  103.             {
  104.  
  105.             lastRatio = opponent.getHealthRatio();
  106.             lastHealthScale = opponent.getHealth();
  107.             opponentName = Text.removeTags(opponent.getName());
  108.  
  109.             lastMaxHealth = null;
  110.             if (opponent instanceof NPC)
  111.             {
  112.                 lastMaxHealth = npcManager.getHealth(opponentName, opponent.getCombatLevel());
  113.             }
  114.             else if (opponent instanceof Player)
  115.             {
  116.                 final HiscoreResult hiscoreResult = hiscoreManager.lookupAsync(opponentName, opponentInfoPlugin.getHiscoreEndpoint());
  117.                 if (hiscoreResult != null)
  118.                 {
  119.                     final int hp = hiscoreResult.getHitpoints().getLevel();
  120.                     if (hp > 0)
  121.                     {
  122.                         lastMaxHealth = hp;
  123.                     }
  124.  
  125.                 }
  126.             }
  127.  
  128.             final Actor opponentsOpponent = opponent.getInteracting();
  129.             if (opponentsOpponent != null
  130.                 && (opponentsOpponent != client.getLocalPlayer() || client.getVar(Varbits.MULTICOMBAT_AREA) == 1))
  131.             {
  132.                 opponentsOpponentName = Text.removeTags(opponentsOpponent.getName());
  133.             }
  134.             else
  135.             {
  136.                 opponentsOpponentName = null;
  137.             }
  138.         }
  139.  
  140.         if (opponentName == null)
  141.         {
  142.             return null;
  143.         }
  144.  
  145.         final FontMetrics fontMetrics = graphics.getFontMetrics();
  146.  
  147.         panelComponent.getChildren().clear();
  148.  
  149.         // Opponent name
  150.         int textWidth = Math.max(ComponentConstants.STANDARD_WIDTH, fontMetrics.stringWidth(opponentName));
  151.         panelComponent.setPreferredSize(new Dimension(textWidth, 0));
  152.         panelComponent.getChildren().add(TitleComponent.builder()
  153.             .text(opponentName)
  154.             .build());
  155.  
  156.         // Health bar
  157.  
  158.         if (lastRatio >= 0 && lastHealthScale > 0)
  159.         {
  160.             final ProgressBarComponent progressBarComponent = new ProgressBarComponent();
  161.             progressBarComponent.setBackgroundColor(HP_RED);
  162.             progressBarComponent.setForegroundColor(HP_GREEN);
  163.  
  164.             if (lastMaxHealth != null && !opponentInfoConfig.showPercent())
  165.             {
  166.                 // This is the reverse of the calculation of healthRatio done by the server
  167.                 // which is: healthRatio = 1 + (healthScale - 1) * health / maxHealth (if health > 0, 0 otherwise)
  168.                 // It's able to recover the exact health if maxHealth <= healthScale.
  169.                 int health = 0;
  170.                 if (lastRatio > 0)
  171.                 {
  172.                     int minHealth = 1;
  173.                     int maxHealth;
  174.                     if (lastHealthScale > 1)
  175.                     {
  176.                         if (lastRatio > 1)
  177.                         {
  178.                             // This doesn't apply if healthRatio = 1, because of the special case in the server calculation that
  179.                             // health = 0 forces healthRatio = 0 instead of the expected healthRatio = 1
  180.                             minHealth = (lastMaxHealth * (lastRatio - 1) + lastHealthScale - 2) / (lastHealthScale - 1);
  181.                         }
  182.                         maxHealth = (lastMaxHealth * lastRatio - 1) / (lastHealthScale - 1);
  183.                         if (maxHealth > lastMaxHealth)
  184.                         {
  185.                             maxHealth = lastMaxHealth;
  186.  
  187.                         }
  188.                     }
  189.                     else
  190.                     {
  191.                         // If healthScale is 1, healthRatio will always be 1 unless health = 0
  192.                         // so we know nothing about the upper limit except that it can't be higher than maxHealth
  193.                         maxHealth = lastMaxHealth;
  194.                     }
  195.                     // Take the average of min and max possible healts
  196.                     health = (minHealth + maxHealth + 1) / 2;
  197.                 }
  198.  
  199.                 progressBarComponent.setLabelDisplayMode(ProgressBarComponent.LabelDisplayMode.FULL);
  200.                 progressBarComponent.setMaximum(lastMaxHealth);
  201.                 progressBarComponent.setValue(health);
  202.  
  203.  
  204.             }
  205.             else
  206.             {
  207.                 float floatRatio = (float) lastRatio / (float) lastHealthScale;
  208.                 progressBarComponent.setValue(floatRatio * 100d);
  209.                 if(floatRatio * 100d == 0){
  210.                     graphics.setColor(Color.blue);
  211.                     graphics.fillRect(6, 290, 5, 5);
  212.                 }
  213.             }
  214.  
  215.             panelComponent.getChildren().add(progressBarComponent);
  216.         }
  217.  
  218.  
  219.         return panelComponent.render(graphics);
  220.     }
  221. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top