Java Code Examples for net.runelite.api.Actor#getHealthScale()

The following examples show how to use net.runelite.api.Actor#getHealthScale() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpponentInfoPlugin.java    From plugins with GNU General Public License v3.0 6 votes vote down vote up
private String getHpString(Actor actor, boolean withColorTag)
{
	int maxHp = getMaxHp(actor);
	int health = actor.getHealthScale();
	int ratio = actor.getHealthRatio();

	final String result;
	if (maxHp != -1)
	{
		final int exactHealth = OpponentInfoOverlay.getExactHp(ratio, health, maxHp);
		result = "(" + exactHealth + "/" + maxHp + ")";
	}
	else
	{
		result = "(" + (100 * ratio) / health + "%)";
	}

	return withColorTag ? ColorUtil.colorStartTag(0xff0000) + result : result;
}
 
Example 2
Source File: OpponentInfoOverlay.java    From plugins with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Dimension render(Graphics2D graphics)
{
	final Actor opponent = opponentInfoPlugin.getLastOpponent();

	if (opponent == null)
	{
		opponentName = null;
		return null;
	}

	if (opponent.getName() != null && opponent.getHealthScale() > 0)
	{
		lastRatio = opponent.getHealthRatio();
		lastHealthScale = opponent.getHealthScale();
		opponentName = Text.removeTags(opponent.getName());

		lastMaxHealth = opponentInfoPlugin.getMaxHp(opponent);
	}

	if (opponentName == null)
	{
		return null;
	}

	final FontMetrics fontMetrics = graphics.getFontMetrics();

	// Opponent name
	int panelWidth = Math.max(ComponentConstants.STANDARD_WIDTH, fontMetrics.stringWidth(opponentName) + ComponentConstants.STANDARD_BORDER + ComponentConstants.STANDARD_BORDER);
	panelComponent.setPreferredSize(new Dimension(panelWidth, 0));
	panelComponent.getChildren().add(TitleComponent.builder()
		.text(opponentName)
		.build());

	// Health bar
	if (lastRatio >= 0 && lastHealthScale > 0)
	{
		final ProgressBarComponent progressBarComponent = new ProgressBarComponent();
		progressBarComponent.setBackgroundColor(HP_RED);
		progressBarComponent.setForegroundColor(HP_GREEN);

		final HitpointsDisplayStyle displayStyle = opponentInfoConfig.hitpointsDisplayStyle();

		if ((displayStyle == HitpointsDisplayStyle.HITPOINTS || displayStyle == HitpointsDisplayStyle.BOTH)
			&& lastMaxHealth != -1)
		{
			int health = getExactHp(lastRatio, lastHealthScale, lastMaxHealth);

			// Show both the hitpoint and percentage values if enabled in the config
			final ProgressBarComponent.LabelDisplayMode progressBarDisplayMode = displayStyle == HitpointsDisplayStyle.BOTH ?
				ProgressBarComponent.LabelDisplayMode.BOTH : ProgressBarComponent.LabelDisplayMode.FULL;

			progressBarComponent.setLabelDisplayMode(progressBarDisplayMode);
			progressBarComponent.setMaximum(lastMaxHealth);
			progressBarComponent.setValue(health);
		}
		else
		{
			float floatRatio = (float) lastRatio / (float) lastHealthScale;
			progressBarComponent.setValue(floatRatio * 100d);
		}

		panelComponent.getChildren().add(progressBarComponent);
	}

	// Opponents opponent
	if (opponentsOpponentName != null)
	{
		panelWidth = Math.max(panelWidth, fontMetrics.stringWidth(opponentsOpponentName) + ComponentConstants.STANDARD_BORDER + ComponentConstants.STANDARD_BORDER);
		panelComponent.setPreferredSize(new Dimension(panelWidth, 0));
		panelComponent.getChildren().add(TitleComponent.builder()
			.text(opponentsOpponentName)
			.build());
	}

	return super.render(graphics);
}
 
Example 3
Source File: OpponentInfoPlugin.java    From plugins with GNU General Public License v3.0 4 votes vote down vote up
private boolean modify(MenuEntry entry)
{
	if (isNotAttackEntry(entry))
	{
		return false;
	}

	boolean changed = false;

	int index = entry.getIdentifier();
	Actor actor = getActorFromIndex(index);

	if (actor == null)
	{
		return false;
	}

	if (actor instanceof Player)
	{
		index -= 32768;
	}

	String target = entry.getTarget();

	if (showAttacking &&
		client.getLocalPlayer().getRSInteracting() == index)
	{
		target = attackingColTag + target.substring(COLOR_TAG_LENGTH);
		changed = true;
	}

	if (showAttackers &&
		actor.getRSInteracting() - 32768 == client.getLocalPlayerIndex())
	{
		target = '*' + target;
		changed = true;
	}

	if (showHitpoints &&
		actor.getHealthScale() > 0)
	{
		int lvlIndex = target.lastIndexOf("(level-");
		if (lvlIndex != -1)
		{
			String levelReplacement = getHpString(actor, true);

			target = target.substring(0, lvlIndex) + levelReplacement;
			changed = true;
		}
	}

	if (changed)
	{
		entry.setTarget(target);
		return true;
	}

	return false;
}
 
Example 4
Source File: OpponentInfoPlugin.java    From plugins with GNU General Public License v3.0 4 votes vote down vote up
private boolean fixup(MenuEntry entry)
{
	if (isNotAttackEntry(entry))
	{
		return false;
	}

	int index = entry.getIdentifier();

	Actor actor = getActorFromIndex(index);

	if (actor == null)
	{
		return false;
	}

	if (actor instanceof Player)
	{
		index -= 32768;
	}

	String target = entry.getTarget();

	boolean hasAggro = actor.getRSInteracting() - 32768 == client.getLocalPlayerIndex();
	boolean hadAggro = target.charAt(0) == '*';
	boolean isTarget = client.getLocalPlayer().getRSInteracting() == index;
	boolean hasHp = showHitpoints && actor instanceof NPC && actor.getHealthScale() > 0;

	boolean aggroChanged = showAttackers && hasAggro != hadAggro;
	boolean targetChanged = showAttacking && isTarget != target.startsWith(attackingColTag, aggroChanged ? 1 : 0);
	boolean hpChanged = showHitpoints && hasHp == target.contains("(level-");

	if (!aggroChanged &&
		!targetChanged &&
		!hasHp &&
		!hpChanged)
	{
		return false;
	}

	if (targetChanged)
	{
		boolean player = actor instanceof Player;
		final int start = hadAggro ? 1 + COLOR_TAG_LENGTH : COLOR_TAG_LENGTH;
		target =
			(hasAggro ? '*' : "") +
				(isTarget ? attackingColTag :
					player ? ColorUtil.colorStartTag(0xffffff) : ColorUtil.colorStartTag(0xffff00)) +
				target.substring(start);
	}
	else if (aggroChanged)
	{
		if (hasAggro)
		{
			target = '*' + target;
		}
		else
		{
			target = target.substring(1);
		}
	}

	if (hpChanged || hasHp)
	{
		final int braceIdx;

		if (!hasHp)
		{
			braceIdx = target.lastIndexOf("<col=ff0000>(");
			if (braceIdx != -1)
			{
				target = target.substring(0, braceIdx - 1) + "(level-" + actor.getCombatLevel() + ")";
			}
		}
		else if ((braceIdx = target.lastIndexOf('(')) != -1)
		{
			final String hpString = getHpString(actor, hpChanged);

			target = target.substring(0, braceIdx) + hpString;
		}
	}

	entry.setTarget(target);
	return true;
}