From 1f13c27a6a35d1bdb358164fdabda6dbd6ec0a20 Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Wed, 24 Oct 2018 13:45:58 +0200 Subject: [PATCH] Create NPC manager that can return HP for NPC - Create NPC manager and move logic from opponent info there for getting NPC HP based on combat level and name - Make opponent info use NPC manager instead of its own implementation Signed-off-by: Tomas Slusny --- .../net/runelite/client/game/NPCManager.java | 65 +++++++++++++++++++ .../opponentinfo/OpponentInfoOverlay.java | 13 +++- .../opponentinfo/OpponentInfoPlugin.java | 20 ------ 3 files changed, 75 insertions(+), 23 deletions(-) create mode 100644 runelite-client/src/main/java/net/runelite/client/game/NPCManager.java diff --git a/runelite-client/src/main/java/net/runelite/client/game/NPCManager.java b/runelite-client/src/main/java/net/runelite/client/game/NPCManager.java new file mode 100644 index 0000000000..f34d2c13ee --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/game/NPCManager.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2018, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.game; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.reflect.Type; +import java.util.Map; +import javax.annotation.Nullable; +import javax.inject.Inject; +import javax.inject.Singleton; + +@Singleton +public class NPCManager +{ + private final Map healthMap; + + @Inject + private NPCManager() + { + final Gson gson = new Gson(); + final Type typeToken = new TypeToken>() + { + }.getType(); + + final InputStream healthFile = getClass().getResourceAsStream("/npc_health.json"); + healthMap = gson.fromJson(new InputStreamReader(healthFile), typeToken); + } + + /** + * Returns health for target NPC based on it's combat level and name + * @param name npc name + * @param combatLevel npc combat level + * @return health or null if HP is unknown + */ + @Nullable + public Integer getHealth(final String name, final int combatLevel) + { + return healthMap.get(name + "_" + combatLevel); + } +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/OpponentInfoOverlay.java b/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/OpponentInfoOverlay.java index 38ac7ac9f0..6c8cd97b69 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/OpponentInfoOverlay.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/OpponentInfoOverlay.java @@ -38,6 +38,7 @@ import net.runelite.api.NPC; import net.runelite.api.Player; import net.runelite.api.Varbits; import net.runelite.client.game.HiscoreManager; +import net.runelite.client.game.NPCManager; import net.runelite.client.ui.overlay.Overlay; import net.runelite.client.ui.overlay.OverlayPosition; import net.runelite.client.ui.overlay.OverlayPriority; @@ -57,6 +58,7 @@ class OpponentInfoOverlay extends Overlay private final OpponentInfoPlugin opponentInfoPlugin; private final OpponentInfoConfig opponentInfoConfig; private final HiscoreManager hiscoreManager; + private final NPCManager npcManager; private final PanelComponent panelComponent = new PanelComponent(); @@ -67,13 +69,18 @@ class OpponentInfoOverlay extends Overlay private String opponentsOpponentName; @Inject - private OpponentInfoOverlay(Client client, OpponentInfoPlugin opponentInfoPlugin, - OpponentInfoConfig opponentInfoConfig, HiscoreManager hiscoreManager) + private OpponentInfoOverlay( + Client client, + OpponentInfoPlugin opponentInfoPlugin, + OpponentInfoConfig opponentInfoConfig, + HiscoreManager hiscoreManager, + NPCManager npcManager) { this.client = client; this.opponentInfoPlugin = opponentInfoPlugin; this.opponentInfoConfig = opponentInfoConfig; this.hiscoreManager = hiscoreManager; + this.npcManager = npcManager; setPosition(OverlayPosition.TOP_LEFT); setPriority(OverlayPriority.HIGH); @@ -102,7 +109,7 @@ class OpponentInfoOverlay extends Overlay lastMaxHealth = null; if (opponent instanceof NPC) { - lastMaxHealth = opponentInfoPlugin.getOppInfoHealth().get(opponentName + "_" + opponent.getCombatLevel()); + lastMaxHealth = npcManager.getHealth(opponentName, opponent.getCombatLevel()); } else if (opponent instanceof Player) { diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/OpponentInfoPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/OpponentInfoPlugin.java index a8e5694a6d..1f38dc136d 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/OpponentInfoPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/OpponentInfoPlugin.java @@ -26,16 +26,10 @@ package net.runelite.client.plugins.opponentinfo; import com.google.common.eventbus.Subscribe; -import com.google.common.reflect.TypeToken; -import com.google.gson.Gson; import com.google.inject.Provides; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.lang.reflect.Type; import java.time.Duration; import java.time.Instant; import java.util.EnumSet; -import java.util.Map; import javax.inject.Inject; import lombok.AccessLevel; import lombok.Getter; @@ -84,9 +78,6 @@ public class OpponentInfoPlugin extends Plugin private Instant lastTime; - @Getter(AccessLevel.PACKAGE) - private final Map oppInfoHealth = loadNpcHealth(); - @Provides OpponentInfoConfig provideConfig(ConfigManager configManager) { @@ -164,15 +155,4 @@ public class OpponentInfoPlugin extends Plugin } } } - - private Map loadNpcHealth() - { - Gson gson = new Gson(); - Type type = new TypeToken>() - { - }.getType(); - - InputStream healthFile = OpponentInfoPlugin.class.getResourceAsStream("/npc_health.json"); - return gson.fromJson(new InputStreamReader(healthFile), type); - } }