opponent info: move interacting logic to plugin and use interacting changed event
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2017, Adam <Adam@sigterm.info>
|
||||
* Copyright (c) 2016-2018, Adam <Adam@sigterm.info>
|
||||
* Copyright (c) 2018, Jordan Atwood <jordan.atwood423@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -30,9 +31,6 @@ import java.awt.FontMetrics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Point;
|
||||
import java.awt.Rectangle;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import javax.inject.Inject;
|
||||
import net.runelite.api.Actor;
|
||||
import net.runelite.api.Client;
|
||||
@@ -54,22 +52,17 @@ class OpponentInfoOverlay extends Overlay
|
||||
{
|
||||
private static final Color HP_GREEN = new Color(0, 146, 54, 230);
|
||||
private static final Color HP_RED = new Color(102, 15, 16, 230);
|
||||
private static final Duration WAIT = Duration.ofSeconds(3);
|
||||
|
||||
private final Client client;
|
||||
private final OpponentInfoPlugin opponentInfoPlugin;
|
||||
private final HiscoreManager hiscoreManager;
|
||||
|
||||
private final NPC[] clientNpcs;
|
||||
private final PanelComponent panelComponent = new PanelComponent();
|
||||
private final Map<String, Integer> oppInfoHealth = OpponentInfoPlugin.loadNpcHealth();
|
||||
|
||||
private Integer lastMaxHealth;
|
||||
private float lastRatio = 0;
|
||||
private Instant lastTime = Instant.now();
|
||||
private String opponentName;
|
||||
private String opponentsOpponentName;
|
||||
private NPC lastOpponent;
|
||||
|
||||
@Inject
|
||||
private OpponentInfoOverlay(Client client, OpponentInfoPlugin opponentInfoPlugin, HiscoreManager hiscoreManager)
|
||||
@@ -78,7 +71,6 @@ class OpponentInfoOverlay extends Overlay
|
||||
this.opponentInfoPlugin = opponentInfoPlugin;
|
||||
this.hiscoreManager = hiscoreManager;
|
||||
|
||||
this.clientNpcs = client.getCachedNPCs();
|
||||
setPosition(OverlayPosition.TOP_LEFT);
|
||||
setPriority(OverlayPriority.HIGH);
|
||||
|
||||
@@ -86,58 +78,33 @@ class OpponentInfoOverlay extends Overlay
|
||||
panelComponent.setGap(new Point(0, 2));
|
||||
}
|
||||
|
||||
private Actor getOpponent()
|
||||
{
|
||||
Player player = client.getLocalPlayer();
|
||||
if (player == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return player.getInteracting();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dimension render(Graphics2D graphics)
|
||||
{
|
||||
Actor opponent = getOpponent();
|
||||
final Actor opponent = opponentInfoPlugin.getLastOpponent();
|
||||
|
||||
// If opponent is null, try to use last opponent
|
||||
if (opponent == null)
|
||||
{
|
||||
if (lastOpponent != null && clientNpcs[lastOpponent.getIndex()] != lastOpponent)
|
||||
{
|
||||
// lastOpponent is no longer valid
|
||||
lastOpponent = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
opponent = lastOpponent;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Update last opponent
|
||||
lastOpponent = opponent instanceof NPC ? (NPC) opponent : null;
|
||||
opponentName = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (opponent != null && opponent.getHealth() > 0)
|
||||
if (opponent.getName() != null && opponent.getHealth() > 0)
|
||||
{
|
||||
lastTime = Instant.now();
|
||||
lastRatio = (float) opponent.getHealthRatio() / (float) opponent.getHealth();
|
||||
opponentName = Text.removeTags(opponent.getName());
|
||||
|
||||
lastMaxHealth = null;
|
||||
if (opponent instanceof NPC)
|
||||
{
|
||||
lastMaxHealth = oppInfoHealth.get(opponentName + "_" + opponent.getCombatLevel());
|
||||
lastMaxHealth = opponentInfoPlugin.getOppInfoHealth().get(opponentName + "_" + opponent.getCombatLevel());
|
||||
}
|
||||
else if (opponent instanceof Player)
|
||||
{
|
||||
HiscoreResult hiscoreResult = hiscoreManager.lookupAsync(opponentName, opponentInfoPlugin.getHiscoreEndpoint());
|
||||
final HiscoreResult hiscoreResult = hiscoreManager.lookupAsync(opponentName, opponentInfoPlugin.getHiscoreEndpoint());
|
||||
if (hiscoreResult != null)
|
||||
{
|
||||
int hp = hiscoreResult.getHitpoints().getLevel();
|
||||
final int hp = hiscoreResult.getHitpoints().getLevel();
|
||||
if (hp > 0)
|
||||
{
|
||||
lastMaxHealth = hp;
|
||||
@@ -145,7 +112,7 @@ class OpponentInfoOverlay extends Overlay
|
||||
}
|
||||
}
|
||||
|
||||
Actor opponentsOpponent = opponent.getInteracting();
|
||||
final Actor opponentsOpponent = opponent.getInteracting();
|
||||
if (opponentsOpponent != null
|
||||
&& (opponentsOpponent != client.getLocalPlayer() || client.getVar(Varbits.MULTICOMBAT_AREA) == 1))
|
||||
{
|
||||
@@ -157,9 +124,9 @@ class OpponentInfoOverlay extends Overlay
|
||||
}
|
||||
}
|
||||
|
||||
if (Duration.between(Instant.now(), lastTime).abs().compareTo(WAIT) > 0)
|
||||
if (opponentName == null)
|
||||
{
|
||||
return null; //don't draw anything.
|
||||
return null;
|
||||
}
|
||||
|
||||
final FontMetrics fontMetrics = graphics.getFontMetrics();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2017, Adam <Adam@sigterm.info>
|
||||
* Copyright (c) 2016-2018, Adam <Adam@sigterm.info>
|
||||
* Copyright (c) 2018, Jordan Atwood <jordan.atwood423@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -30,15 +31,20 @@ import com.google.gson.Gson;
|
||||
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;
|
||||
import net.runelite.api.Actor;
|
||||
import net.runelite.api.Client;
|
||||
import net.runelite.api.GameState;
|
||||
import net.runelite.api.WorldType;
|
||||
import net.runelite.api.events.GameStateChanged;
|
||||
import net.runelite.api.events.GameTick;
|
||||
import net.runelite.api.events.InteractingChanged;
|
||||
import net.runelite.client.plugins.Plugin;
|
||||
import net.runelite.client.plugins.PluginDescriptor;
|
||||
import net.runelite.client.ui.overlay.OverlayManager;
|
||||
@@ -49,6 +55,8 @@ import net.runelite.http.api.hiscore.HiscoreEndpoint;
|
||||
)
|
||||
public class OpponentInfoPlugin extends Plugin
|
||||
{
|
||||
private static final Duration WAIT = Duration.ofSeconds(5);
|
||||
|
||||
@Inject
|
||||
private Client client;
|
||||
|
||||
@@ -61,6 +69,14 @@ public class OpponentInfoPlugin extends Plugin
|
||||
@Getter(AccessLevel.PACKAGE)
|
||||
private HiscoreEndpoint hiscoreEndpoint = HiscoreEndpoint.NORMAL;
|
||||
|
||||
@Getter(AccessLevel.PACKAGE)
|
||||
private Actor lastOpponent;
|
||||
|
||||
private Instant lastTime = null;
|
||||
|
||||
@Getter(AccessLevel.PACKAGE)
|
||||
private final Map<String, Integer> oppInfoHealth = loadNpcHealth();
|
||||
|
||||
@Override
|
||||
protected void startUp() throws Exception
|
||||
{
|
||||
@@ -96,7 +112,38 @@ public class OpponentInfoPlugin extends Plugin
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, Integer> loadNpcHealth()
|
||||
@Subscribe
|
||||
public void onInteractingChanged(InteractingChanged event)
|
||||
{
|
||||
if (event.getSource() != client.getLocalPlayer())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Actor opponent = event.getTarget();
|
||||
|
||||
if (opponent == null)
|
||||
{
|
||||
lastTime = Instant.now();
|
||||
return;
|
||||
}
|
||||
|
||||
lastOpponent = opponent;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onGameTick(GameTick gameTick)
|
||||
{
|
||||
if (lastOpponent != null && client.getLocalPlayer().getInteracting() == null)
|
||||
{
|
||||
if (Duration.between(lastTime, Instant.now()).compareTo(WAIT) > 0)
|
||||
{
|
||||
lastOpponent = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Integer> loadNpcHealth()
|
||||
{
|
||||
Gson gson = new Gson();
|
||||
Type type = new TypeToken<Map<String, Integer>>()
|
||||
|
||||
Reference in New Issue
Block a user