From f30e496ee065ddd839056b61a9c6396247933c69 Mon Sep 17 00:00:00 2001 From: Scott Burns Date: Thu, 16 May 2019 01:06:40 +0200 Subject: [PATCH] Refactor PrayerAlert --- .../prayeralert/PrayerAlertConfig.java | 41 +-- .../prayeralert/PrayerAlertOverlay.java | 290 ++++++++++-------- .../prayeralert/PrayerAlertPlugin.java | 53 ++-- 3 files changed, 200 insertions(+), 184 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/prayeralert/PrayerAlertConfig.java b/runelite-client/src/main/java/net/runelite/client/plugins/prayeralert/PrayerAlertConfig.java index b68ef2ba6e..1ee5230fbc 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/prayeralert/PrayerAlertConfig.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/prayeralert/PrayerAlertConfig.java @@ -23,24 +23,25 @@ import net.runelite.client.config.ConfigItem; @ConfigGroup("prayeralert") public interface PrayerAlertConfig extends Config { - @ConfigItem( - position = 1, - keyName = "alwaysShowAlert", - name = "Always show prayer alert", - description = "Show the alert, even without prayer restore in inventory" - ) - default boolean alwaysShowAlert() - { - return false; - } - @ConfigItem( - position = 2, - keyName = "oldRenderMode", - name = "Render using old method", - description = "Render the prayer alert using the old method" - ) - default boolean oldRenderMode() - { - return false; - } + @ConfigItem( + position = 1, + keyName = "alwaysShowAlert", + name = "Always show prayer alert", + description = "Show the alert, even without prayer restore in inventory" + ) + default boolean alwaysShowAlert() + { + return false; + } + + @ConfigItem( + position = 2, + keyName = "oldRenderMode", + name = "Render using old method", + description = "Render the prayer alert using the old method" + ) + default boolean oldRenderMode() + { + return false; + } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/prayeralert/PrayerAlertOverlay.java b/runelite-client/src/main/java/net/runelite/client/plugins/prayeralert/PrayerAlertOverlay.java index 71814074c2..617cd3e611 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/prayeralert/PrayerAlertOverlay.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/prayeralert/PrayerAlertOverlay.java @@ -16,10 +16,16 @@ 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 net.runelite.api.*; +import net.runelite.api.Client; +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.plugins.itemstats.stats.Stat; import net.runelite.client.plugins.itemstats.stats.Stats; @@ -33,157 +39,169 @@ import net.runelite.client.ui.overlay.components.TitleComponent; class PrayerAlertOverlay extends Overlay { - private final Client client; - private final PrayerAlertConfig config; - private final PanelComponent panelComponent = new PanelComponent(); - private final ItemManager itemManager; + private final Client client; + private final PrayerAlertConfig config; + private final PanelComponent panelComponent = new PanelComponent(); + private final ItemManager itemManager; - private final Stat prayer = Stats.PRAYER; + private final Stat prayer = Stats.PRAYER; - @Inject - private PrayerAlertOverlay(Client client, PrayerAlertConfig config, ItemManager itemManager) - { - setPosition(OverlayPosition.TOP_RIGHT); - setLayer(OverlayLayer.ABOVE_WIDGETS); - this.client = client; - this.config = config; - this.itemManager = itemManager; - } + @Inject + private PrayerAlertOverlay(Client client, PrayerAlertConfig config, ItemManager itemManager) + { + setPosition(OverlayPosition.TOP_RIGHT); + setLayer(OverlayLayer.ABOVE_WIDGETS); + this.client = client; + this.config = config; + this.itemManager = itemManager; + } - @Override - public Dimension render(Graphics2D graphics) - { - panelComponent.getChildren().clear(); - int prayerLevel = getPrayerLevel(); - int prayerPoints = getPrayerPoints(); - if (config.oldRenderMode()){ - if (config.alwaysShowAlert()){ - boolean drink = drinkPrayerPotion(prayerLevel, prayerPoints); - if (drink) { - oldPrayerRestorePanel(graphics); - } - } - else { - boolean drink = drinkPrayerPotion(prayerLevel, prayerPoints); - boolean hasPrayerPotion = checkInventoryForPotion(); - if (drink && hasPrayerPotion) { - oldPrayerRestorePanel(graphics); - } - } - } - else{ - if (config.alwaysShowAlert()){ - boolean drink = drinkPrayerPotion(prayerLevel, prayerPoints); - if (drink) { - prayerRestorePanel(panelComponent, graphics); - } - } - else { - boolean drink = drinkPrayerPotion(prayerLevel, prayerPoints); - boolean hasPrayerPotion = checkInventoryForPotion(); - if (drink && hasPrayerPotion) { - prayerRestorePanel(panelComponent, graphics); - } - } - } - return panelComponent.render(graphics); - } + @Override + public Dimension render(Graphics2D graphics) + { + panelComponent.getChildren().clear(); + int prayerLevel = getPrayerLevel(); + int prayerPoints = getPrayerPoints(); + if (config.oldRenderMode()) + { + if (config.alwaysShowAlert()) + { + boolean drink = drinkPrayerPotion(prayerLevel, prayerPoints); + if (drink) + { + oldPrayerRestorePanel(graphics); + } + } + else + { + boolean drink = drinkPrayerPotion(prayerLevel, prayerPoints); + boolean hasPrayerPotion = checkInventoryForPotion(); + if (drink && hasPrayerPotion) + { + oldPrayerRestorePanel(graphics); + } + } + } + else + { + if (config.alwaysShowAlert()) + { + boolean drink = drinkPrayerPotion(prayerLevel, prayerPoints); + if (drink) + { + prayerRestorePanel(panelComponent, graphics); + } + } + else + { + boolean drink = drinkPrayerPotion(prayerLevel, prayerPoints); + boolean hasPrayerPotion = checkInventoryForPotion(); + if (drink && hasPrayerPotion) + { + prayerRestorePanel(panelComponent, graphics); + } + } + } + return panelComponent.render(graphics); + } - private int getPrayerLevel() - { - return prayer.getMaximum(client); - } + private int getPrayerLevel() + { + return prayer.getMaximum(client); + } - private int getPrayerPoints() - { - return prayer.getValue(client); - } + private int getPrayerPoints() + { + return prayer.getValue(client); + } - private boolean drinkPrayerPotion(int prayerLevel, int prayerPoints) - { - boolean drink = false; - int prayerPotionRestoreValue = 7; - double quarterOfPrayerLevel = (0.25) * (double) prayerLevel; + private boolean drinkPrayerPotion(int prayerLevel, int prayerPoints) + { + boolean drink = false; + int prayerPotionRestoreValue = 7; + double quarterOfPrayerLevel = (0.25) * (double) prayerLevel; - prayerPotionRestoreValue = prayerPotionRestoreValue + (int) quarterOfPrayerLevel; + prayerPotionRestoreValue = prayerPotionRestoreValue + (int) quarterOfPrayerLevel; - if (prayerPoints < (prayerLevel - prayerPotionRestoreValue)) - { - drink = true; - } + if (prayerPoints < (prayerLevel - prayerPotionRestoreValue)) + { + drink = true; + } - return drink; - } + return drink; + } - private boolean checkInventoryForPotion() - { - ItemContainer inventory = client.getItemContainer(InventoryID.INVENTORY); - Item[] inventoryItems; - boolean hasPrayerPotion = false; + private boolean checkInventoryForPotion() + { + ItemContainer inventory = client.getItemContainer(InventoryID.INVENTORY); + Item[] inventoryItems; + 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, - 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}; + 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.SUPER_RESTORE3, ItemID.SUPER_RESTORE4, ItemID.SUPER_RESTORE_MIX1, ItemID.SUPER_RESTORE_MIX2}; - if (inventory != null) - { - inventoryItems = inventory.getItems(); - for (Item item : inventoryItems) - { - for (int prayerPotionId : potionID) - { - if (item.getId() == prayerPotionId) - { - hasPrayerPotion = true; - } - } - } - } + if (inventory != null) + { + inventoryItems = inventory.getItems(); + for (Item item : inventoryItems) + { + for (int prayerPotionId : potionID) + { + if (item.getId() == prayerPotionId) + { + hasPrayerPotion = true; + } + } + } + } - return hasPrayerPotion; - } + return hasPrayerPotion; + } - private void prayerRestorePanel(PanelComponent panelComponent, Graphics2D graphics){ - panelComponent.getChildren().add(new ImageComponent(itemManager.getImage(ItemID.PRAYER_POTION4))); - panelComponent.getChildren().add(TitleComponent.builder() - .text("Drink") - .color(Color.RED) - .build()); - panelComponent.setPreferredSize(new Dimension( - graphics.getFontMetrics().stringWidth("Drink") + 12,0)); - } + private void prayerRestorePanel(PanelComponent panelComponent, Graphics2D graphics) + { + panelComponent.getChildren().add(new ImageComponent(itemManager.getImage(ItemID.PRAYER_POTION4))); + panelComponent.getChildren().add(TitleComponent.builder() + .text("Drink") + .color(Color.RED) + .build()); + panelComponent.setPreferredSize(new Dimension( + graphics.getFontMetrics().stringWidth("Drink") + 12, 0)); + } - private void oldPrayerRestorePanel(Graphics2D graphics){ - graphics.translate(-100, 15); - graphics.setColor(new Color(0.2f, 0.2f, 0.2f, 0.5f)); - graphics.fillRect(0, 0, 100, 45); + private void oldPrayerRestorePanel(Graphics2D graphics) + { + graphics.translate(-100, 15); + 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(); - dropShadow1 = dropShadow1.deriveFont(Font.PLAIN); - graphics.setFont(dropShadow1); - graphics.setColor(new Color(0f, 0f, 0f, 0.6f)); - graphics.drawString("Drink", 56, 20); + Font dropShadow1 = FontManager.getRunescapeFont(); + dropShadow1 = dropShadow1.deriveFont(Font.PLAIN); + graphics.setFont(dropShadow1); + graphics.setColor(new Color(0f, 0f, 0f, 0.6f)); + graphics.drawString("Drink", 56, 20); - Font drinkFont1 = FontManager.getRunescapeFont(); - drinkFont1 = drinkFont1.deriveFont(Font.PLAIN); - graphics.setFont(drinkFont1); - graphics.setColor(new Color(1.0f, 0.03529412f, 0.0f)); - graphics.translate(-0.8, -0.8); - graphics.drawString("Drink", 56, 20); + Font drinkFont1 = FontManager.getRunescapeFont(); + drinkFont1 = drinkFont1.deriveFont(Font.PLAIN); + graphics.setFont(drinkFont1); + graphics.setColor(new Color(1.0f, 0.03529412f, 0.0f)); + graphics.translate(-0.8, -0.8); + graphics.drawString("Drink", 56, 20); - Font dropShadow2 = FontManager.getRunescapeFont(); - dropShadow2 = dropShadow2.deriveFont(Font.PLAIN); - graphics.setFont(dropShadow2); - graphics.setColor(new Color(0f, 0f, 0f, 0.6f)); - graphics.drawString("Potion", 53, 40); + Font dropShadow2 = FontManager.getRunescapeFont(); + dropShadow2 = dropShadow2.deriveFont(Font.PLAIN); + graphics.setFont(dropShadow2); + graphics.setColor(new Color(0f, 0f, 0f, 0.6f)); + graphics.drawString("Potion", 53, 40); - Font drinkFont2 = FontManager.getRunescapeFont(); - drinkFont2 = drinkFont2.deriveFont(Font.PLAIN); - graphics.setFont(drinkFont2); - graphics.setColor(new Color(1.0f, 0.03529412f, 0.0f)); - graphics.translate(-0.8, -0.8); - graphics.drawString("Potion", 53, 40); - } + Font drinkFont2 = FontManager.getRunescapeFont(); + drinkFont2 = drinkFont2.deriveFont(Font.PLAIN); + graphics.setFont(drinkFont2); + graphics.setColor(new Color(1.0f, 0.03529412f, 0.0f)); + graphics.translate(-0.8, -0.8); + graphics.drawString("Potion", 53, 40); + } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/prayeralert/PrayerAlertPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/prayeralert/PrayerAlertPlugin.java index efb60f8cd8..da1fc659ab 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/prayeralert/PrayerAlertPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/prayeralert/PrayerAlertPlugin.java @@ -18,45 +18,42 @@ package net.runelite.client.plugins.prayeralert; import com.google.inject.Provides; import javax.inject.Inject; +import net.runelite.client.config.ConfigManager; import net.runelite.client.plugins.Plugin; import net.runelite.client.plugins.PluginDescriptor; -import net.runelite.client.config.ConfigManager; import net.runelite.client.plugins.PluginType; import net.runelite.client.ui.overlay.OverlayManager; @PluginDescriptor( - name = "Prayer Alerter", - description = "Alert the player when prayer is low", - tags = {"prayer", "overlay"}, - type = PluginType.UTILITY, - enabledByDefault = false + name = "Prayer Alerter", + description = "Alert the player when prayer is low", + tags = {"prayer", "overlay"}, + type = PluginType.UTILITY, + enabledByDefault = false ) public class PrayerAlertPlugin extends Plugin { - @Inject - private OverlayManager overlayManager; + @Inject + private OverlayManager overlayManager; - @Inject - private PrayerAlertOverlay overlay; + @Inject + private PrayerAlertOverlay overlay; - @Inject - private PrayerAlertConfig config; + @Provides + PrayerAlertConfig provideConfig(ConfigManager configManager) + { + return configManager.getConfig(PrayerAlertConfig.class); + } - @Provides - PrayerAlertConfig provideConfig(ConfigManager configManager) - { - return configManager.getConfig(PrayerAlertConfig.class); - } + @Override + protected void startUp() throws Exception + { + overlayManager.add(overlay); + } - @Override - protected void startUp() throws Exception - { - overlayManager.add(overlay); - } - - @Override - protected void shutDown() throws Exception - { - overlayManager.remove(overlay); - } + @Override + protected void shutDown() throws Exception + { + overlayManager.remove(overlay); + } }