Refactor PrayerAlert

This commit is contained in:
Scott Burns
2019-05-16 01:06:40 +02:00
parent 26f24ae54d
commit f30e496ee0
3 changed files with 200 additions and 184 deletions

View File

@@ -23,24 +23,25 @@ import net.runelite.client.config.ConfigItem;
@ConfigGroup("prayeralert") @ConfigGroup("prayeralert")
public interface PrayerAlertConfig extends Config public interface PrayerAlertConfig extends Config
{ {
@ConfigItem( @ConfigItem(
position = 1, position = 1,
keyName = "alwaysShowAlert", keyName = "alwaysShowAlert",
name = "Always show prayer alert", name = "Always show prayer alert",
description = "Show the alert, even without prayer restore in inventory" description = "Show the alert, even without prayer restore in inventory"
) )
default boolean alwaysShowAlert() default boolean alwaysShowAlert()
{ {
return false; return false;
} }
@ConfigItem(
position = 2, @ConfigItem(
keyName = "oldRenderMode", position = 2,
name = "Render using old method", keyName = "oldRenderMode",
description = "Render the prayer alert using the old method" name = "Render using old method",
) description = "Render the prayer alert using the old method"
default boolean oldRenderMode() )
{ default boolean oldRenderMode()
return false; {
} return false;
}
} }

View File

@@ -16,10 +16,16 @@
package net.runelite.client.plugins.prayeralert; package net.runelite.client.plugins.prayeralert;
import java.awt.*; import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import javax.inject.Inject; import javax.inject.Inject;
import net.runelite.api.Client;
import net.runelite.api.*; import net.runelite.api.InventoryID;
import net.runelite.api.Item;
import net.runelite.api.ItemContainer;
import net.runelite.api.ItemID;
import net.runelite.client.game.ItemManager; import net.runelite.client.game.ItemManager;
import net.runelite.client.plugins.itemstats.stats.Stat; import net.runelite.client.plugins.itemstats.stats.Stat;
import net.runelite.client.plugins.itemstats.stats.Stats; import net.runelite.client.plugins.itemstats.stats.Stats;
@@ -33,157 +39,169 @@ import net.runelite.client.ui.overlay.components.TitleComponent;
class PrayerAlertOverlay extends Overlay class PrayerAlertOverlay extends Overlay
{ {
private final Client client; private final Client client;
private final PrayerAlertConfig config; private final PrayerAlertConfig config;
private final PanelComponent panelComponent = new PanelComponent(); private final PanelComponent panelComponent = new PanelComponent();
private final ItemManager itemManager; private final ItemManager itemManager;
private final Stat prayer = Stats.PRAYER; private final Stat prayer = Stats.PRAYER;
@Inject @Inject
private PrayerAlertOverlay(Client client, PrayerAlertConfig config, ItemManager itemManager) private PrayerAlertOverlay(Client client, PrayerAlertConfig config, ItemManager itemManager)
{ {
setPosition(OverlayPosition.TOP_RIGHT); setPosition(OverlayPosition.TOP_RIGHT);
setLayer(OverlayLayer.ABOVE_WIDGETS); setLayer(OverlayLayer.ABOVE_WIDGETS);
this.client = client; this.client = client;
this.config = config; this.config = config;
this.itemManager = itemManager; this.itemManager = itemManager;
} }
@Override @Override
public Dimension render(Graphics2D graphics) public Dimension render(Graphics2D graphics)
{ {
panelComponent.getChildren().clear(); panelComponent.getChildren().clear();
int prayerLevel = getPrayerLevel(); int prayerLevel = getPrayerLevel();
int prayerPoints = getPrayerPoints(); int prayerPoints = getPrayerPoints();
if (config.oldRenderMode()){ if (config.oldRenderMode())
if (config.alwaysShowAlert()){ {
boolean drink = drinkPrayerPotion(prayerLevel, prayerPoints); if (config.alwaysShowAlert())
if (drink) { {
oldPrayerRestorePanel(graphics); boolean drink = drinkPrayerPotion(prayerLevel, prayerPoints);
} if (drink)
} {
else { oldPrayerRestorePanel(graphics);
boolean drink = drinkPrayerPotion(prayerLevel, prayerPoints); }
boolean hasPrayerPotion = checkInventoryForPotion(); }
if (drink && hasPrayerPotion) { else
oldPrayerRestorePanel(graphics); {
} boolean drink = drinkPrayerPotion(prayerLevel, prayerPoints);
} boolean hasPrayerPotion = checkInventoryForPotion();
} if (drink && hasPrayerPotion)
else{ {
if (config.alwaysShowAlert()){ oldPrayerRestorePanel(graphics);
boolean drink = drinkPrayerPotion(prayerLevel, prayerPoints); }
if (drink) { }
prayerRestorePanel(panelComponent, graphics); }
} else
} {
else { if (config.alwaysShowAlert())
boolean drink = drinkPrayerPotion(prayerLevel, prayerPoints); {
boolean hasPrayerPotion = checkInventoryForPotion(); boolean drink = drinkPrayerPotion(prayerLevel, prayerPoints);
if (drink && hasPrayerPotion) { if (drink)
prayerRestorePanel(panelComponent, graphics); {
} prayerRestorePanel(panelComponent, graphics);
} }
} }
return panelComponent.render(graphics); else
} {
boolean drink = drinkPrayerPotion(prayerLevel, prayerPoints);
boolean hasPrayerPotion = checkInventoryForPotion();
if (drink && hasPrayerPotion)
{
prayerRestorePanel(panelComponent, graphics);
}
}
}
return panelComponent.render(graphics);
}
private int getPrayerLevel() private int getPrayerLevel()
{ {
return prayer.getMaximum(client); return prayer.getMaximum(client);
} }
private int getPrayerPoints() private int getPrayerPoints()
{ {
return prayer.getValue(client); return prayer.getValue(client);
} }
private boolean drinkPrayerPotion(int prayerLevel, int prayerPoints) private boolean drinkPrayerPotion(int prayerLevel, int prayerPoints)
{ {
boolean drink = false; boolean drink = false;
int prayerPotionRestoreValue = 7; int prayerPotionRestoreValue = 7;
double quarterOfPrayerLevel = (0.25) * (double) prayerLevel; double quarterOfPrayerLevel = (0.25) * (double) prayerLevel;
prayerPotionRestoreValue = prayerPotionRestoreValue + (int) quarterOfPrayerLevel; prayerPotionRestoreValue = prayerPotionRestoreValue + (int) quarterOfPrayerLevel;
if (prayerPoints < (prayerLevel - prayerPotionRestoreValue)) if (prayerPoints < (prayerLevel - prayerPotionRestoreValue))
{ {
drink = true; drink = true;
} }
return drink; return drink;
} }
private boolean checkInventoryForPotion() private boolean checkInventoryForPotion()
{ {
ItemContainer inventory = client.getItemContainer(InventoryID.INVENTORY); ItemContainer inventory = client.getItemContainer(InventoryID.INVENTORY);
Item[] inventoryItems; Item[] inventoryItems;
boolean hasPrayerPotion = false; boolean hasPrayerPotion = false;
int[] potionID = {ItemID.PRAYER_POTION1, ItemID.PRAYER_POTION2, ItemID.PRAYER_POTION3, ItemID.PRAYER_POTION4, ItemID.PRAYER_POTION1_20396, ItemID.PRAYER_POTION2_20395, int[] potionID = {ItemID.PRAYER_POTION1, ItemID.PRAYER_POTION2, ItemID.PRAYER_POTION3, ItemID.PRAYER_POTION4, ItemID.PRAYER_POTION1_20396, ItemID.PRAYER_POTION2_20395,
ItemID.PRAYER_POTION3_20394, ItemID.PRAYER_POTION4_20393, ItemID.PRAYER_MIX1, ItemID.PRAYER_MIX2, ItemID.SUPER_RESTORE1, ItemID.SUPER_RESTORE2, ItemID.PRAYER_POTION3_20394, ItemID.PRAYER_POTION4_20393, ItemID.PRAYER_MIX1, ItemID.PRAYER_MIX2, ItemID.SUPER_RESTORE1, ItemID.SUPER_RESTORE2,
ItemID.SUPER_RESTORE3, ItemID.SUPER_RESTORE4, ItemID.SUPER_RESTORE_MIX1, ItemID.SUPER_RESTORE_MIX2}; ItemID.SUPER_RESTORE3, ItemID.SUPER_RESTORE4, ItemID.SUPER_RESTORE_MIX1, ItemID.SUPER_RESTORE_MIX2};
if (inventory != null) if (inventory != null)
{ {
inventoryItems = inventory.getItems(); inventoryItems = inventory.getItems();
for (Item item : inventoryItems) for (Item item : inventoryItems)
{ {
for (int prayerPotionId : potionID) for (int prayerPotionId : potionID)
{ {
if (item.getId() == prayerPotionId) if (item.getId() == prayerPotionId)
{ {
hasPrayerPotion = true; hasPrayerPotion = true;
} }
} }
} }
} }
return hasPrayerPotion; return hasPrayerPotion;
} }
private void prayerRestorePanel(PanelComponent panelComponent, Graphics2D graphics){ private void prayerRestorePanel(PanelComponent panelComponent, Graphics2D graphics)
panelComponent.getChildren().add(new ImageComponent(itemManager.getImage(ItemID.PRAYER_POTION4))); {
panelComponent.getChildren().add(TitleComponent.builder() panelComponent.getChildren().add(new ImageComponent(itemManager.getImage(ItemID.PRAYER_POTION4)));
.text("Drink") panelComponent.getChildren().add(TitleComponent.builder()
.color(Color.RED) .text("Drink")
.build()); .color(Color.RED)
panelComponent.setPreferredSize(new Dimension( .build());
graphics.getFontMetrics().stringWidth("Drink") + 12,0)); panelComponent.setPreferredSize(new Dimension(
} graphics.getFontMetrics().stringWidth("Drink") + 12, 0));
}
private void oldPrayerRestorePanel(Graphics2D graphics){ private void oldPrayerRestorePanel(Graphics2D graphics)
graphics.translate(-100, 15); {
graphics.setColor(new Color(0.2f, 0.2f, 0.2f, 0.5f)); graphics.translate(-100, 15);
graphics.fillRect(0, 0, 100, 45); graphics.setColor(new Color(0.2f, 0.2f, 0.2f, 0.5f));
graphics.fillRect(0, 0, 100, 45);
graphics.drawImage(itemManager.getImage(ItemID.PRAYER_POTION4), null, 14, 7); graphics.drawImage(itemManager.getImage(ItemID.PRAYER_POTION4), null, 14, 7);
Font dropShadow1 = FontManager.getRunescapeFont(); Font dropShadow1 = FontManager.getRunescapeFont();
dropShadow1 = dropShadow1.deriveFont(Font.PLAIN); dropShadow1 = dropShadow1.deriveFont(Font.PLAIN);
graphics.setFont(dropShadow1); graphics.setFont(dropShadow1);
graphics.setColor(new Color(0f, 0f, 0f, 0.6f)); graphics.setColor(new Color(0f, 0f, 0f, 0.6f));
graphics.drawString("Drink", 56, 20); graphics.drawString("Drink", 56, 20);
Font drinkFont1 = FontManager.getRunescapeFont(); Font drinkFont1 = FontManager.getRunescapeFont();
drinkFont1 = drinkFont1.deriveFont(Font.PLAIN); drinkFont1 = drinkFont1.deriveFont(Font.PLAIN);
graphics.setFont(drinkFont1); graphics.setFont(drinkFont1);
graphics.setColor(new Color(1.0f, 0.03529412f, 0.0f)); graphics.setColor(new Color(1.0f, 0.03529412f, 0.0f));
graphics.translate(-0.8, -0.8); graphics.translate(-0.8, -0.8);
graphics.drawString("Drink", 56, 20); graphics.drawString("Drink", 56, 20);
Font dropShadow2 = FontManager.getRunescapeFont(); Font dropShadow2 = FontManager.getRunescapeFont();
dropShadow2 = dropShadow2.deriveFont(Font.PLAIN); dropShadow2 = dropShadow2.deriveFont(Font.PLAIN);
graphics.setFont(dropShadow2); graphics.setFont(dropShadow2);
graphics.setColor(new Color(0f, 0f, 0f, 0.6f)); graphics.setColor(new Color(0f, 0f, 0f, 0.6f));
graphics.drawString("Potion", 53, 40); graphics.drawString("Potion", 53, 40);
Font drinkFont2 = FontManager.getRunescapeFont(); Font drinkFont2 = FontManager.getRunescapeFont();
drinkFont2 = drinkFont2.deriveFont(Font.PLAIN); drinkFont2 = drinkFont2.deriveFont(Font.PLAIN);
graphics.setFont(drinkFont2); graphics.setFont(drinkFont2);
graphics.setColor(new Color(1.0f, 0.03529412f, 0.0f)); graphics.setColor(new Color(1.0f, 0.03529412f, 0.0f));
graphics.translate(-0.8, -0.8); graphics.translate(-0.8, -0.8);
graphics.drawString("Potion", 53, 40); graphics.drawString("Potion", 53, 40);
} }
} }

View File

@@ -18,45 +18,42 @@ package net.runelite.client.plugins.prayeralert;
import com.google.inject.Provides; import com.google.inject.Provides;
import javax.inject.Inject; import javax.inject.Inject;
import net.runelite.client.config.ConfigManager;
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.config.ConfigManager;
import net.runelite.client.plugins.PluginType; import net.runelite.client.plugins.PluginType;
import net.runelite.client.ui.overlay.OverlayManager; import net.runelite.client.ui.overlay.OverlayManager;
@PluginDescriptor( @PluginDescriptor(
name = "Prayer Alerter", name = "Prayer Alerter",
description = "Alert the player when prayer is low", description = "Alert the player when prayer is low",
tags = {"prayer", "overlay"}, tags = {"prayer", "overlay"},
type = PluginType.UTILITY, type = PluginType.UTILITY,
enabledByDefault = false enabledByDefault = false
) )
public class PrayerAlertPlugin extends Plugin public class PrayerAlertPlugin extends Plugin
{ {
@Inject @Inject
private OverlayManager overlayManager; private OverlayManager overlayManager;
@Inject @Inject
private PrayerAlertOverlay overlay; private PrayerAlertOverlay overlay;
@Inject @Provides
private PrayerAlertConfig config; PrayerAlertConfig provideConfig(ConfigManager configManager)
{
return configManager.getConfig(PrayerAlertConfig.class);
}
@Provides @Override
PrayerAlertConfig provideConfig(ConfigManager configManager) protected void startUp() throws Exception
{ {
return configManager.getConfig(PrayerAlertConfig.class); overlayManager.add(overlay);
} }
@Override @Override
protected void startUp() throws Exception protected void shutDown() throws Exception
{ {
overlayManager.add(overlay); overlayManager.remove(overlay);
} }
@Override
protected void shutDown() throws Exception
{
overlayManager.remove(overlay);
}
} }