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.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* 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.Graphics2D;
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.awt.Rectangle;
|
import java.awt.Rectangle;
|
||||||
import java.time.Duration;
|
|
||||||
import java.time.Instant;
|
|
||||||
import java.util.Map;
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import net.runelite.api.Actor;
|
import net.runelite.api.Actor;
|
||||||
import net.runelite.api.Client;
|
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_GREEN = new Color(0, 146, 54, 230);
|
||||||
private static final Color HP_RED = new Color(102, 15, 16, 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 Client client;
|
||||||
private final OpponentInfoPlugin opponentInfoPlugin;
|
private final OpponentInfoPlugin opponentInfoPlugin;
|
||||||
private final HiscoreManager hiscoreManager;
|
private final HiscoreManager hiscoreManager;
|
||||||
|
|
||||||
private final NPC[] clientNpcs;
|
|
||||||
private final PanelComponent panelComponent = new PanelComponent();
|
private final PanelComponent panelComponent = new PanelComponent();
|
||||||
private final Map<String, Integer> oppInfoHealth = OpponentInfoPlugin.loadNpcHealth();
|
|
||||||
|
|
||||||
private Integer lastMaxHealth;
|
private Integer lastMaxHealth;
|
||||||
private float lastRatio = 0;
|
private float lastRatio = 0;
|
||||||
private Instant lastTime = Instant.now();
|
|
||||||
private String opponentName;
|
private String opponentName;
|
||||||
private String opponentsOpponentName;
|
private String opponentsOpponentName;
|
||||||
private NPC lastOpponent;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private OpponentInfoOverlay(Client client, OpponentInfoPlugin opponentInfoPlugin, HiscoreManager hiscoreManager)
|
private OpponentInfoOverlay(Client client, OpponentInfoPlugin opponentInfoPlugin, HiscoreManager hiscoreManager)
|
||||||
@@ -78,7 +71,6 @@ class OpponentInfoOverlay extends Overlay
|
|||||||
this.opponentInfoPlugin = opponentInfoPlugin;
|
this.opponentInfoPlugin = opponentInfoPlugin;
|
||||||
this.hiscoreManager = hiscoreManager;
|
this.hiscoreManager = hiscoreManager;
|
||||||
|
|
||||||
this.clientNpcs = client.getCachedNPCs();
|
|
||||||
setPosition(OverlayPosition.TOP_LEFT);
|
setPosition(OverlayPosition.TOP_LEFT);
|
||||||
setPriority(OverlayPriority.HIGH);
|
setPriority(OverlayPriority.HIGH);
|
||||||
|
|
||||||
@@ -86,58 +78,33 @@ class OpponentInfoOverlay extends Overlay
|
|||||||
panelComponent.setGap(new Point(0, 2));
|
panelComponent.setGap(new Point(0, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Actor getOpponent()
|
|
||||||
{
|
|
||||||
Player player = client.getLocalPlayer();
|
|
||||||
if (player == null)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return player.getInteracting();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dimension render(Graphics2D graphics)
|
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 (opponent == null)
|
||||||
{
|
{
|
||||||
if (lastOpponent != null && clientNpcs[lastOpponent.getIndex()] != lastOpponent)
|
opponentName = null;
|
||||||
{
|
return null;
|
||||||
// lastOpponent is no longer valid
|
|
||||||
lastOpponent = null;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
opponent = lastOpponent;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Update last opponent
|
|
||||||
lastOpponent = opponent instanceof NPC ? (NPC) opponent : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opponent != null && opponent.getHealth() > 0)
|
if (opponent.getName() != null && opponent.getHealth() > 0)
|
||||||
{
|
{
|
||||||
lastTime = Instant.now();
|
|
||||||
lastRatio = (float) opponent.getHealthRatio() / (float) opponent.getHealth();
|
lastRatio = (float) opponent.getHealthRatio() / (float) opponent.getHealth();
|
||||||
opponentName = Text.removeTags(opponent.getName());
|
opponentName = Text.removeTags(opponent.getName());
|
||||||
|
|
||||||
lastMaxHealth = null;
|
lastMaxHealth = null;
|
||||||
if (opponent instanceof NPC)
|
if (opponent instanceof NPC)
|
||||||
{
|
{
|
||||||
lastMaxHealth = oppInfoHealth.get(opponentName + "_" + opponent.getCombatLevel());
|
lastMaxHealth = opponentInfoPlugin.getOppInfoHealth().get(opponentName + "_" + opponent.getCombatLevel());
|
||||||
}
|
}
|
||||||
else if (opponent instanceof Player)
|
else if (opponent instanceof Player)
|
||||||
{
|
{
|
||||||
HiscoreResult hiscoreResult = hiscoreManager.lookupAsync(opponentName, opponentInfoPlugin.getHiscoreEndpoint());
|
final HiscoreResult hiscoreResult = hiscoreManager.lookupAsync(opponentName, opponentInfoPlugin.getHiscoreEndpoint());
|
||||||
if (hiscoreResult != null)
|
if (hiscoreResult != null)
|
||||||
{
|
{
|
||||||
int hp = hiscoreResult.getHitpoints().getLevel();
|
final int hp = hiscoreResult.getHitpoints().getLevel();
|
||||||
if (hp > 0)
|
if (hp > 0)
|
||||||
{
|
{
|
||||||
lastMaxHealth = hp;
|
lastMaxHealth = hp;
|
||||||
@@ -145,7 +112,7 @@ class OpponentInfoOverlay extends Overlay
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Actor opponentsOpponent = opponent.getInteracting();
|
final Actor opponentsOpponent = opponent.getInteracting();
|
||||||
if (opponentsOpponent != null
|
if (opponentsOpponent != null
|
||||||
&& (opponentsOpponent != client.getLocalPlayer() || client.getVar(Varbits.MULTICOMBAT_AREA) == 1))
|
&& (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();
|
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.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* 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.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import lombok.AccessLevel;
|
import lombok.AccessLevel;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import net.runelite.api.Actor;
|
||||||
import net.runelite.api.Client;
|
import net.runelite.api.Client;
|
||||||
import net.runelite.api.GameState;
|
import net.runelite.api.GameState;
|
||||||
import net.runelite.api.WorldType;
|
import net.runelite.api.WorldType;
|
||||||
import net.runelite.api.events.GameStateChanged;
|
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.Plugin;
|
||||||
import net.runelite.client.plugins.PluginDescriptor;
|
import net.runelite.client.plugins.PluginDescriptor;
|
||||||
import net.runelite.client.ui.overlay.OverlayManager;
|
import net.runelite.client.ui.overlay.OverlayManager;
|
||||||
@@ -49,6 +55,8 @@ import net.runelite.http.api.hiscore.HiscoreEndpoint;
|
|||||||
)
|
)
|
||||||
public class OpponentInfoPlugin extends Plugin
|
public class OpponentInfoPlugin extends Plugin
|
||||||
{
|
{
|
||||||
|
private static final Duration WAIT = Duration.ofSeconds(5);
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private Client client;
|
private Client client;
|
||||||
|
|
||||||
@@ -61,6 +69,14 @@ public class OpponentInfoPlugin extends Plugin
|
|||||||
@Getter(AccessLevel.PACKAGE)
|
@Getter(AccessLevel.PACKAGE)
|
||||||
private HiscoreEndpoint hiscoreEndpoint = HiscoreEndpoint.NORMAL;
|
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
|
@Override
|
||||||
protected void startUp() throws Exception
|
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();
|
Gson gson = new Gson();
|
||||||
Type type = new TypeToken<Map<String, Integer>>()
|
Type type = new TypeToken<Map<String, Integer>>()
|
||||||
|
|||||||
Reference in New Issue
Block a user