From ac8f728d47a4653fc363db95572970b28ce03529 Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Sun, 17 Feb 2019 20:31:29 +0100 Subject: [PATCH 1/2] Remove dodgy charges variable from item charges plugin Just use config class directly, this variable makes no sense. Signed-off-by: Tomas Slusny --- .../plugins/itemcharges/ItemChargeOverlay.java | 2 +- .../plugins/itemcharges/ItemChargePlugin.java | 17 +++++------------ .../itemcharges/ItemChargePluginTest.java | 16 +++++++++++----- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeOverlay.java b/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeOverlay.java index 7730d46ffd..c8a07716bf 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeOverlay.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeOverlay.java @@ -83,7 +83,7 @@ class ItemChargeOverlay extends Overlay continue; } - charges = itemChargePlugin.getDodgyCharges(); + charges = config.dodgyNecklace(); } else { diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargePlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargePlugin.java index c545e9a5bb..f232b64bfd 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargePlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargePlugin.java @@ -28,8 +28,6 @@ import com.google.inject.Provides; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.inject.Inject; -import lombok.AccessLevel; -import lombok.Getter; import net.runelite.api.ChatMessageType; import net.runelite.api.events.ChatMessage; import net.runelite.client.Notifier; @@ -68,9 +66,6 @@ public class ItemChargePlugin extends Plugin @Inject private ItemChargeConfig config; - @Getter(AccessLevel.PACKAGE) - private int dodgyCharges; - @Provides ItemChargeConfig getConfig(ConfigManager configManager) { @@ -81,7 +76,6 @@ public class ItemChargePlugin extends Plugin protected void startUp() { overlayManager.add(overlay); - dodgyCharges = config.dodgyNecklace(); } @Override @@ -110,22 +104,21 @@ public class ItemChargePlugin extends Plugin notifier.notify("Your dodgy necklace has crumbled to dust."); } - setDodgyCharges(MAX_DODGY_CHARGES); + updateDodgyNecklaceCharges(MAX_DODGY_CHARGES); } else if (dodgyCheckMatcher.find()) { - setDodgyCharges(Integer.parseInt(dodgyCheckMatcher.group(1))); + updateDodgyNecklaceCharges(Integer.parseInt(dodgyCheckMatcher.group(1))); } else if (dodgyProtectMatcher.find()) { - setDodgyCharges(Integer.parseInt(dodgyProtectMatcher.group(1))); + updateDodgyNecklaceCharges(Integer.parseInt(dodgyProtectMatcher.group(1))); } } } - private void setDodgyCharges(int dodgyCharges) + private void updateDodgyNecklaceCharges(final int value) { - this.dodgyCharges = dodgyCharges; - config.dodgyNecklace(dodgyCharges); + config.dodgyNecklace(value); } } \ No newline at end of file diff --git a/runelite-client/src/test/java/net/runelite/client/plugins/itemcharges/ItemChargePluginTest.java b/runelite-client/src/test/java/net/runelite/client/plugins/itemcharges/ItemChargePluginTest.java index b8fc81a16a..ea6438031a 100644 --- a/runelite-client/src/test/java/net/runelite/client/plugins/itemcharges/ItemChargePluginTest.java +++ b/runelite-client/src/test/java/net/runelite/client/plugins/itemcharges/ItemChargePluginTest.java @@ -33,11 +33,13 @@ import net.runelite.api.Client; import net.runelite.api.events.ChatMessage; import net.runelite.client.Notifier; import net.runelite.client.ui.overlay.OverlayManager; -import static org.junit.Assert.assertEquals; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import static org.mockito.Matchers.eq; import org.mockito.Mock; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.verify; import org.mockito.runners.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) @@ -78,18 +80,22 @@ public class ItemChargePluginTest { ChatMessage chatMessage = new ChatMessage(null, ChatMessageType.SERVER, "", CHECK, "", 0); itemChargePlugin.onChatMessage(chatMessage); - assertEquals(10, itemChargePlugin.getDodgyCharges()); + verify(config).dodgyNecklace(eq(10)); + reset(config); chatMessage = new ChatMessage(null, ChatMessageType.SERVER, "", PROTECT, "", 0); itemChargePlugin.onChatMessage(chatMessage); - assertEquals(9, itemChargePlugin.getDodgyCharges()); + verify(config).dodgyNecklace(eq(9)); + reset(config); chatMessage = new ChatMessage(null, ChatMessageType.SERVER, "", PROTECT_1, "", 0); itemChargePlugin.onChatMessage(chatMessage); - assertEquals(1, itemChargePlugin.getDodgyCharges()); + verify(config).dodgyNecklace(eq(1)); + reset(config); chatMessage = new ChatMessage(null, ChatMessageType.SERVER, "", BREAK, "", 0); itemChargePlugin.onChatMessage(chatMessage); - assertEquals(10, itemChargePlugin.getDodgyCharges()); + verify(config).dodgyNecklace(eq(10)); + reset(config); } } \ No newline at end of file From 1a3815865189853b9b2dcc1dcf2ed522e7da14ec Mon Sep 17 00:00:00 2001 From: Hydrox6 Date: Tue, 18 Dec 2018 11:24:10 +0000 Subject: [PATCH 2/2] Add infoboxes to item charges plugin Closes #1318 Co-authored-by: Tomas Slusny Signed-off-by: Tomas Slusny --- .../plugins/itemcharges/ItemChargeConfig.java | 11 ++ .../itemcharges/ItemChargeInfobox.java | 60 ++++++ .../itemcharges/ItemChargeOverlay.java | 17 +- .../plugins/itemcharges/ItemChargePlugin.java | 173 ++++++++++++++++++ .../plugins/itemcharges/ItemChargeType.java | 3 +- .../plugins/itemcharges/ItemWithSlot.java | 47 +++++ .../itemcharges/ItemChargePluginTest.java | 10 + 7 files changed, 304 insertions(+), 17 deletions(-) create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeInfobox.java create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemWithSlot.java diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeConfig.java b/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeConfig.java index 40488c5137..28c503b20a 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeConfig.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeConfig.java @@ -203,4 +203,15 @@ public interface ItemChargeConfig extends Config { return false; } + + @ConfigItem( + keyName = "showInfoboxes", + name = "Show Infoboxes", + description = "Configures whether to show an infobox equipped charge items", + position = 15 + ) + default boolean showInfoboxes() + { + return false; + } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeInfobox.java b/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeInfobox.java new file mode 100644 index 0000000000..7ee70d44b5 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeInfobox.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2018, Hydrox6 + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.plugins.itemcharges; + +import java.awt.Color; +import java.awt.image.BufferedImage; +import lombok.Getter; +import net.runelite.api.EquipmentInventorySlot; +import net.runelite.client.ui.overlay.infobox.Counter; + +@Getter +class ItemChargeInfobox extends Counter +{ + private final ItemChargePlugin plugin; + private final ItemWithSlot item; + private final EquipmentInventorySlot slot; + + ItemChargeInfobox( + ItemChargePlugin plugin, + BufferedImage image, + String name, + int charges, + ItemWithSlot item, + EquipmentInventorySlot slot) + { + super(image, plugin, charges); + setTooltip(name); + this.plugin = plugin; + this.item = item; + this.slot = slot; + } + + @Override + public Color getTextColor() + { + return getPlugin().getColor(getCount()); + } +} \ No newline at end of file diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeOverlay.java b/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeOverlay.java index c8a07716bf..27bb35b21d 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeOverlay.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeOverlay.java @@ -24,7 +24,6 @@ */ package net.runelite.client.plugins.itemcharges; -import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Point; @@ -112,7 +111,7 @@ class ItemChargeOverlay extends Overlay final TextComponent textComponent = new TextComponent(); textComponent.setPosition(new Point(bounds.x, bounds.y + 16)); textComponent.setText(charges < 0 ? "?" : String.valueOf(charges)); - textComponent.setColor(getColor(charges)); + textComponent.setColor(itemChargePlugin.getColor(charges)); textComponent.render(graphics); } return null; @@ -137,20 +136,6 @@ class ItemChargeOverlay extends Overlay return jewellery; } - private Color getColor(int charges) - { - Color color = Color.WHITE; - if (charges <= config.veryLowWarning()) - { - color = config.veryLowWarningColor(); - } - else if (charges <= config.lowWarning()) - { - color = config.lowWarningolor(); - } - return color; - } - private boolean displayOverlay() { return config.showTeleportCharges() || config.showDodgyCount() || config.showFungicideCharges() diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargePlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargePlugin.java index f232b64bfd..e295efd9e5 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargePlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargePlugin.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2017, Seth + * Copyright (c) 2018, Hydrox6 * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -25,17 +26,29 @@ package net.runelite.client.plugins.itemcharges; import com.google.inject.Provides; +import java.awt.Color; +import java.awt.image.BufferedImage; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.inject.Inject; import net.runelite.api.ChatMessageType; +import net.runelite.api.Client; +import net.runelite.api.EquipmentInventorySlot; +import net.runelite.api.InventoryID; +import net.runelite.api.Item; +import net.runelite.api.ItemContainer; +import net.runelite.api.ItemID; import net.runelite.api.events.ChatMessage; +import net.runelite.api.events.ConfigChanged; +import net.runelite.api.events.ItemContainerChanged; import net.runelite.client.Notifier; import net.runelite.client.config.ConfigManager; import net.runelite.client.eventbus.Subscribe; +import net.runelite.client.game.ItemManager; import net.runelite.client.plugins.Plugin; import net.runelite.client.plugins.PluginDescriptor; import net.runelite.client.ui.overlay.OverlayManager; +import net.runelite.client.ui.overlay.infobox.InfoBoxManager; @PluginDescriptor( name = "Item Charges", @@ -54,12 +67,21 @@ public class ItemChargePlugin extends Plugin private static final int MAX_DODGY_CHARGES = 10; + @Inject + private Client client; + @Inject private OverlayManager overlayManager; @Inject private ItemChargeOverlay overlay; + @Inject + private ItemManager itemManager; + + @Inject + private InfoBoxManager infoBoxManager; + @Inject private Notifier notifier; @@ -82,6 +104,37 @@ public class ItemChargePlugin extends Plugin protected void shutDown() throws Exception { overlayManager.remove(overlay); + infoBoxManager.removeIf(ItemChargeInfobox.class::isInstance); + } + + @Subscribe + public void onConfigChanged(ConfigChanged event) + { + if (!event.getGroup().equals("itemCharge")) + { + return; + } + + if (!config.showInfoboxes()) + { + infoBoxManager.removeIf(ItemChargeInfobox.class::isInstance); + return; + } + + if (!config.showTeleportCharges()) + { + removeInfobox(ItemWithSlot.TELEPORT); + } + + if (!config.showAbyssalBraceletCharges()) + { + removeInfobox(ItemWithSlot.ABYSSAL_BRACELET); + } + + if (!config.showDodgyCount()) + { + removeInfobox(ItemWithSlot.DODGY_NECKLACE); + } } @Subscribe @@ -117,8 +170,128 @@ public class ItemChargePlugin extends Plugin } } + @Subscribe + public void onItemContainerChanged(ItemContainerChanged event) + { + if (event.getItemContainer() != client.getItemContainer(InventoryID.EQUIPMENT) || !config.showInfoboxes()) + { + return; + } + + final Item[] items = event.getItemContainer().getItems(); + + if (config.showTeleportCharges()) + { + updateJewelleryInfobox(ItemWithSlot.TELEPORT, items); + } + + if (config.showDodgyCount()) + { + updateJewelleryInfobox(ItemWithSlot.DODGY_NECKLACE, items); + } + + if (config.showAbyssalBraceletCharges()) + { + updateJewelleryInfobox(ItemWithSlot.ABYSSAL_BRACELET, items); + } + } + private void updateDodgyNecklaceCharges(final int value) { config.dodgyNecklace(value); + + if (config.showInfoboxes() && config.showDodgyCount()) + { + final ItemContainer itemContainer = client.getItemContainer(InventoryID.EQUIPMENT); + + if (itemContainer == null) + { + return; + } + + updateJewelleryInfobox(ItemWithSlot.DODGY_NECKLACE, itemContainer.getItems()); + } + } + + private void updateJewelleryInfobox(ItemWithSlot item, Item[] items) + { + for (final EquipmentInventorySlot equipmentInventorySlot : item.getSlots()) + { + updateJewelleryInfobox(item, items, equipmentInventorySlot); + } + } + + private void updateJewelleryInfobox(ItemWithSlot type, Item[] items, EquipmentInventorySlot slot) + { + removeInfobox(type, slot); + + if (slot.getSlotIdx() >= items.length) + { + return; + } + + final int id = items[slot.getSlotIdx()].getId(); + if (id < 0) + { + return; + } + + final ItemWithCharge itemWithCharge = ItemWithCharge.findItem(id); + int charges = -1; + + if (itemWithCharge == null) + { + if (id == ItemID.DODGY_NECKLACE && type == ItemWithSlot.DODGY_NECKLACE) + { + charges = config.dodgyNecklace(); + } + } + else if (itemWithCharge.getType() == type.getType()) + { + charges = itemWithCharge.getCharges(); + } + + if (charges <= 0) + { + return; + } + + final String name = itemManager.getItemComposition(id).getName(); + final BufferedImage image = itemManager.getImage(id); + final ItemChargeInfobox infobox = new ItemChargeInfobox(this, image, name, charges, type, slot); + infoBoxManager.addInfoBox(infobox); + } + + private void removeInfobox(final ItemWithSlot item) + { + infoBoxManager.removeIf(t -> t instanceof ItemChargeInfobox && ((ItemChargeInfobox) t).getItem() == item); + } + + private void removeInfobox(final ItemWithSlot item, final EquipmentInventorySlot slot) + { + infoBoxManager.removeIf(t -> + { + if (!(t instanceof ItemChargeInfobox)) + { + return false; + } + + final ItemChargeInfobox i = (ItemChargeInfobox)t; + return i.getItem() == item && i.getSlot() == slot; + }); + } + + Color getColor(int charges) + { + Color color = Color.WHITE; + if (charges <= config.veryLowWarning()) + { + color = config.veryLowWarningColor(); + } + else if (charges <= config.lowWarning()) + { + color = config.lowWarningolor(); + } + return color; } } \ No newline at end of file diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeType.java b/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeType.java index 2f25c12164..cbe5d20ff3 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeType.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemChargeType.java @@ -32,5 +32,6 @@ enum ItemChargeType IMPBOX, TELEPORT, WATERCAN, - WATERSKIN + WATERSKIN, + DODGY_NECKLACE } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemWithSlot.java b/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemWithSlot.java new file mode 100644 index 0000000000..3fbd2bac66 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/itemcharges/ItemWithSlot.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2019, Tomas Slusny + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.plugins.itemcharges; + +import com.google.common.collect.Sets; +import java.util.Set; +import lombok.Getter; +import net.runelite.api.EquipmentInventorySlot; + +@Getter +enum ItemWithSlot +{ + ABYSSAL_BRACELET(ItemChargeType.ABYSSAL_BRACELET, EquipmentInventorySlot.GLOVES), + DODGY_NECKLACE(ItemChargeType.DODGY_NECKLACE, EquipmentInventorySlot.AMULET), + TELEPORT(ItemChargeType.TELEPORT, EquipmentInventorySlot.WEAPON, EquipmentInventorySlot.AMULET, EquipmentInventorySlot.GLOVES, EquipmentInventorySlot.RING); + + private final ItemChargeType type; + private final Set slots; + + ItemWithSlot(final ItemChargeType type, final EquipmentInventorySlot... slots) + { + this.type = type; + this.slots = Sets.newHashSet(slots); + } +} diff --git a/runelite-client/src/test/java/net/runelite/client/plugins/itemcharges/ItemChargePluginTest.java b/runelite-client/src/test/java/net/runelite/client/plugins/itemcharges/ItemChargePluginTest.java index ea6438031a..165c7d50a7 100644 --- a/runelite-client/src/test/java/net/runelite/client/plugins/itemcharges/ItemChargePluginTest.java +++ b/runelite-client/src/test/java/net/runelite/client/plugins/itemcharges/ItemChargePluginTest.java @@ -28,10 +28,12 @@ import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.testing.fieldbinder.Bind; import com.google.inject.testing.fieldbinder.BoundFieldModule; +import java.util.concurrent.ScheduledExecutorService; import net.runelite.api.ChatMessageType; import net.runelite.api.Client; import net.runelite.api.events.ChatMessage; import net.runelite.client.Notifier; +import net.runelite.client.config.RuneLiteConfig; import net.runelite.client.ui.overlay.OverlayManager; import org.junit.Before; import org.junit.Test; @@ -54,6 +56,14 @@ public class ItemChargePluginTest @Bind private Client client; + @Mock + @Bind + private ScheduledExecutorService scheduledExecutorService; + + @Mock + @Bind + private RuneLiteConfig runeLiteConfig; + @Mock @Bind private OverlayManager overlayManager;