From b0194eeefd07c789f7f027336dd310cc06fd5686 Mon Sep 17 00:00:00 2001 From: Scott Burns Date: Thu, 16 May 2019 00:45:01 +0200 Subject: [PATCH] Refactor Equipment inspector --- .../EquipmentInspectorConfig.java | 62 +-- .../EquipmentInspectorPanel.java | 60 +-- .../EquipmentInspectorPlugin.java | 372 ++++++++++-------- .../plugins/equipmentinspector/ItemPanel.java | 8 +- 4 files changed, 270 insertions(+), 232 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/equipmentinspector/EquipmentInspectorConfig.java b/runelite-client/src/main/java/net/runelite/client/plugins/equipmentinspector/EquipmentInspectorConfig.java index bc9be82d16..c88eec0f71 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/equipmentinspector/EquipmentInspectorConfig.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/equipmentinspector/EquipmentInspectorConfig.java @@ -24,41 +24,43 @@ */ package net.runelite.client.plugins.equipmentinspector; -import java.awt.Color; import net.runelite.client.config.Config; import net.runelite.client.config.ConfigGroup; import net.runelite.client.config.ConfigItem; -import net.runelite.client.plugins.grounditems.config.ItemHighlightMode; -import net.runelite.client.plugins.grounditems.config.MenuHighlightMode; -import net.runelite.client.plugins.grounditems.config.PriceDisplayMode; @ConfigGroup("grounditems") public interface EquipmentInspectorConfig extends Config { - @ConfigItem( - keyName = "ShowValue", - name = "Show the total value of the items", - description = "shows the total value of the items", - position = 1 - ) - default boolean ShowValue() - { - return true; - } - @ConfigItem( - keyName = "protecteditems", - name = "# of protected items", - description = "Limit 4", - position = 2 - ) - default int protecteditems() - { return 1; } - @ConfigItem( - keyName = "ExactValue", - name = "Show exact value", - description = "shows the excact gp value", - position = 3 - ) - default boolean ExactValue() - { return false; } + @ConfigItem( + keyName = "ShowValue", + name = "Show the total value of the items", + description = "shows the total value of the items", + position = 1 + ) + default boolean ShowValue() + { + return true; + } + + @ConfigItem( + keyName = "protecteditems", + name = "# of protected items", + description = "Limit 4", + position = 2 + ) + default int protecteditems() + { + return 1; + } + + @ConfigItem( + keyName = "ExactValue", + name = "Show exact value", + description = "shows the excact gp value", + position = 3 + ) + default boolean ExactValue() + { + return false; + } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/equipmentinspector/EquipmentInspectorPanel.java b/runelite-client/src/main/java/net/runelite/client/plugins/equipmentinspector/EquipmentInspectorPanel.java index d565d9ae31..164ce73b22 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/equipmentinspector/EquipmentInspectorPanel.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/equipmentinspector/EquipmentInspectorPanel.java @@ -24,6 +24,21 @@ */ package net.runelite.client.plugins.equipmentinspector; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.util.HashMap; +import java.util.Map; +import javax.inject.Inject; +import javax.inject.Singleton; +import javax.swing.BorderFactory; +import javax.swing.GroupLayout; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.SwingUtilities; +import javax.swing.border.CompoundBorder; +import javax.swing.border.EmptyBorder; import lombok.extern.slf4j.Slf4j; import net.runelite.api.ItemComposition; import net.runelite.api.kit.KitType; @@ -32,15 +47,6 @@ import net.runelite.client.game.ItemManager; import net.runelite.client.ui.ColorScheme; import net.runelite.client.ui.PluginPanel; -import javax.inject.Inject; -import javax.inject.Singleton; -import javax.swing.*; -import javax.swing.border.CompoundBorder; -import javax.swing.border.EmptyBorder; -import java.awt.*; -import java.util.HashMap; -import java.util.Map; - @Slf4j @Singleton public class EquipmentInspectorPanel extends PluginPanel @@ -71,27 +77,27 @@ public class EquipmentInspectorPanel extends PluginPanel header = new JPanel(); header.setLayout(new BorderLayout()); header.setBorder(new CompoundBorder( - BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(58, 58, 58)), - BorderFactory.createEmptyBorder(0, 0, 10, 0))); + BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(58, 58, 58)), + BorderFactory.createEmptyBorder(0, 0, 10, 0))); nameLabel = new JLabel(NO_PLAYER_SELECTED); nameLabel.setForeground(Color.WHITE); header.add(nameLabel, BorderLayout.CENTER); layout.setHorizontalGroup(layout.createParallelGroup() - .addComponent(equipmentPanels) - .addComponent(header) + .addComponent(equipmentPanels) + .addComponent(header) ); layout.setVerticalGroup(layout.createSequentialGroup() - .addComponent(header) - .addGap(10) - .addComponent(equipmentPanels) + .addComponent(header) + .addGap(10) + .addComponent(equipmentPanels) ); update(new HashMap<>(), ""); } public void update(Map playerEquipment, String playerName) { - if (playerName.isEmpty() || playerName == null) + if (playerName.isEmpty()) { nameLabel.setText(NO_PLAYER_SELECTED); } @@ -100,18 +106,18 @@ public class EquipmentInspectorPanel extends PluginPanel nameLabel.setText("Player: " + playerName); } SwingUtilities.invokeLater(() -> + { + equipmentPanels.removeAll(); + playerEquipment.forEach((kitType, itemComposition) -> { - equipmentPanels.removeAll(); - playerEquipment.forEach((kitType, itemComposition) -> - { - AsyncBufferedImage itemImage = itemManager.getImage(itemComposition.getId()); - equipmentPanels.add(new ItemPanel(itemComposition, kitType, itemImage), c); - c.gridy++; + AsyncBufferedImage itemImage = itemManager.getImage(itemComposition.getId()); + equipmentPanels.add(new ItemPanel(itemComposition, kitType, itemImage), c); + c.gridy++; - }); - header.revalidate(); - header.repaint(); - } + }); + header.revalidate(); + header.repaint(); + } ); } } \ No newline at end of file diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/equipmentinspector/EquipmentInspectorPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/equipmentinspector/EquipmentInspectorPlugin.java index 5fe9c58b02..09da007b46 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/equipmentinspector/EquipmentInspectorPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/equipmentinspector/EquipmentInspectorPlugin.java @@ -26,6 +26,18 @@ package net.runelite.client.plugins.equipmentinspector; import com.google.inject.Provides; +import java.awt.image.BufferedImage; +import java.lang.reflect.InvocationTargetException; +import java.text.NumberFormat; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.ScheduledExecutorService; +import javax.annotation.Nullable; +import javax.inject.Inject; +import javax.swing.SwingUtilities; import lombok.extern.slf4j.Slf4j; import net.runelite.api.ChatMessageType; import net.runelite.api.Client; @@ -49,208 +61,226 @@ import net.runelite.client.ui.NavigationButton; import net.runelite.client.util.ImageUtil; import net.runelite.client.util.Text; -import javax.annotation.Nullable; -import javax.inject.Inject; -import javax.swing.*; -import java.awt.image.BufferedImage; -import java.lang.reflect.InvocationTargetException; -import java.text.NumberFormat; -import java.util.*; -import java.util.concurrent.ScheduledExecutorService; - @PluginDescriptor( - name = "Equipment Inspector", - enabledByDefault = false, - type = PluginType.PVP + name = "Equipment Inspector", + enabledByDefault = false, + type = PluginType.PVP ) @Slf4j -public class EquipmentInspectorPlugin extends Plugin { +public class EquipmentInspectorPlugin extends Plugin +{ + private static final String INSPECT_EQUIPMENT = "Gear"; - private static final String INSPECT_EQUIPMENT = "Gear"; - private static final String KICK_OPTION = "Kick"; + @Inject + @Nullable + private Client client; - @Inject - @Nullable - private Client client; + @Inject + private ItemManager itemManager; - @Inject - private ItemManager itemManager; + @Inject + private EquipmentInspectorConfig config; - @Inject - private EquipmentInspectorConfig config; + @Inject + private ChatMessageManager chatMessageManager; + @Inject + private MenuManager menuManager; - @Inject - private ChatMessageManager chatMessageManager; - @Inject - private MenuManager menuManager; + @Inject + private ScheduledExecutorService executor; - @Inject - private ScheduledExecutorService executor; + @Inject + private ClientToolbar pluginToolbar; + private NavigationButton navButton; + private EquipmentInspectorPanel equipmentInspectorPanel; + private int TotalPrice = 0; + private int Prot1 = 0; + private int Prot2 = 0; + private int Prot3 = 0; + private int Prot4 = 0; - @Inject - private ClientToolbar pluginToolbar; - private NavigationButton navButton; - private EquipmentInspectorPanel equipmentInspectorPanel; - private int TotalPrice = 0; - private int Prot1 = 0; - private int Prot2 = 0; - private int Prot3 = 0; - private int Prot4 = 0; + @Provides + EquipmentInspectorConfig provideConfig(ConfigManager configManager) + { + return configManager.getConfig(EquipmentInspectorConfig.class); + } - @Provides - EquipmentInspectorConfig provideConfig(ConfigManager configManager) { - return configManager.getConfig(EquipmentInspectorConfig.class); - } + @Override + protected void startUp() throws Exception + { - @Override - protected void startUp() throws Exception { + equipmentInspectorPanel = injector.getInstance(EquipmentInspectorPanel.class); + if (client != null) + { + menuManager.addPlayerMenuItem(INSPECT_EQUIPMENT); + } - equipmentInspectorPanel = injector.getInstance(EquipmentInspectorPanel.class); - if (client != null) { - menuManager.addPlayerMenuItem(INSPECT_EQUIPMENT); - } + final BufferedImage icon = ImageUtil.getResourceStreamFromClass(this.getClass(), "normal.png"); - //synchronized (ImageIO.class) - //{ - final BufferedImage icon = ImageUtil.getResourceStreamFromClass(this.getClass(), "normal.png"); - //} - - navButton = NavigationButton.builder() - .tooltip("Equipment Inspector") - .icon(icon) - .priority(5) - .panel(equipmentInspectorPanel) - .build(); + navButton = NavigationButton.builder() + .tooltip("Equipment Inspector") + .icon(icon) + .priority(5) + .panel(equipmentInspectorPanel) + .build(); - pluginToolbar.addNavigation(navButton); + pluginToolbar.addNavigation(navButton); - } + } - @Override - protected void shutDown() throws Exception { + @Override + protected void shutDown() throws Exception + { - menuManager.removePlayerMenuItem(INSPECT_EQUIPMENT); - } + menuManager.removePlayerMenuItem(INSPECT_EQUIPMENT); + } - @Subscribe - public void onPlayerMenuOptionClicked(PlayerMenuOptionClicked event) { - if (event.getMenuOption().equals(INSPECT_EQUIPMENT)) { + @Subscribe + public void onPlayerMenuOptionClicked(PlayerMenuOptionClicked event) + { + if (event.getMenuOption().equals(INSPECT_EQUIPMENT)) + { - executor.execute(() -> - { - try { - SwingUtilities.invokeAndWait(() -> - { - if (!navButton.isSelected()) { - navButton.getOnSelect().run(); - } - }); - } catch (InterruptedException | InvocationTargetException e) { + executor.execute(() -> + { + try + { + SwingUtilities.invokeAndWait(() -> + { + if (!navButton.isSelected()) + { + navButton.getOnSelect().run(); + } + }); + } + catch (InterruptedException | InvocationTargetException e) + { - throw new RuntimeException(e); + throw new RuntimeException(e); - } - String playerName = Text.removeTags(event.getMenuTarget()); - // The player menu uses a non-breaking space in the player name, we need to replace this to compare - // against the playerName in the player cache. - String finalPlayerName = playerName.replace('\u00A0', ' '); - System.out.println(finalPlayerName); - List players = client.getPlayers(); - Optional targetPlayer = players.stream() - .filter(Objects::nonNull) - .filter(p -> p.getName().equals(finalPlayerName)).findFirst(); + } + String playerName = Text.removeTags(event.getMenuTarget()); + // The player menu uses a non-breaking space in the player name, we need to replace this to compare + // against the playerName in the player cache. + String finalPlayerName = playerName.replace('\u00A0', ' '); + System.out.println(finalPlayerName); + List players = client.getPlayers(); + Optional targetPlayer = players.stream() + .filter(Objects::nonNull) + .filter(p -> p.getName().equals(finalPlayerName)).findFirst(); - if (targetPlayer.isPresent()) { - TotalPrice = 0; - Prot1 = 0; - Prot2 = 0; - Prot3 = 0; - Prot4 = 0; - Player p = targetPlayer.get(); - Map playerEquipment = new HashMap<>(); + if (targetPlayer.isPresent()) + { + TotalPrice = 0; + Prot1 = 0; + Prot2 = 0; + Prot3 = 0; + Prot4 = 0; + Player p = targetPlayer.get(); + Map playerEquipment = new HashMap<>(); - for (KitType kitType : KitType.values()) { - if (kitType == KitType.RING) continue; //prevents the equipment inspector from breaking - if (kitType == KitType.AMMUNITION) continue; + for (KitType kitType : KitType.values()) + { + if (kitType == KitType.RING) + { + continue; //prevents the equipment inspector from breaking + } + if (kitType == KitType.AMMUNITION) + { + continue; + } - int itemId = p.getPlayerComposition().getEquipmentId(kitType); - if (itemId != -1) { - ItemComposition itemComposition = client.getItemDefinition(itemId); - playerEquipment.put(kitType, itemComposition); - int ItemPrice = itemManager.getItemPrice(itemId); - TotalPrice += ItemPrice; - if (ItemPrice > Prot1) { - Prot4 = Prot3; - Prot3 = Prot2; - Prot2 = Prot1; + int itemId = p.getPlayerComposition().getEquipmentId(kitType); + if (itemId != -1) + { + ItemComposition itemComposition = client.getItemDefinition(itemId); + playerEquipment.put(kitType, itemComposition); + int ItemPrice = itemManager.getItemPrice(itemId); + TotalPrice += ItemPrice; + if (ItemPrice > Prot1) + { + Prot4 = Prot3; + Prot3 = Prot2; + Prot2 = Prot1; - Prot1 = ItemPrice; - } else if (ItemPrice > Prot2) { - Prot4 = Prot3; - Prot3 = Prot2; - Prot2 = ItemPrice; - } else if (ItemPrice > Prot3) { - Prot4 = Prot3; - Prot3 = ItemPrice; - } else if (ItemPrice > Prot4) { - Prot4 = ItemPrice; - } - } - } - int IgnoredItems = config.protecteditems(); - if (IgnoredItems != 0 && IgnoredItems != 1 && IgnoredItems != 2 && IgnoredItems != 3) { - IgnoredItems = 4; + Prot1 = ItemPrice; + } + else if (ItemPrice > Prot2) + { + Prot4 = Prot3; + Prot3 = Prot2; + Prot2 = ItemPrice; + } + else if (ItemPrice > Prot3) + { + Prot4 = Prot3; + Prot3 = ItemPrice; + } + else if (ItemPrice > Prot4) + { + Prot4 = ItemPrice; + } + } + } + int IgnoredItems = config.protecteditems(); + if (IgnoredItems != 0 && IgnoredItems != 1 && IgnoredItems != 2 && IgnoredItems != 3) + { + IgnoredItems = 4; - } - if (config.ShowValue()) { - switch (IgnoredItems) { - case 1: - TotalPrice = TotalPrice - Prot1; - break; - case 2: - TotalPrice = TotalPrice - Prot1; - TotalPrice = TotalPrice - Prot2; + } + if (config.ShowValue()) + { + switch (IgnoredItems) + { + case 1: + TotalPrice = TotalPrice - Prot1; + break; + case 2: + TotalPrice = TotalPrice - Prot1; + TotalPrice = TotalPrice - Prot2; - break; - case 3: - TotalPrice = TotalPrice - Prot1; - TotalPrice = TotalPrice - Prot2; - TotalPrice = TotalPrice - Prot3; - break; - case 4: - TotalPrice = TotalPrice - Prot1; - TotalPrice = TotalPrice - Prot2; - TotalPrice = TotalPrice - Prot3; - TotalPrice = TotalPrice - Prot4; - break; - } - String StringPrice = ""; - if (!config.ExactValue()) { - TotalPrice = TotalPrice / 1000; - StringPrice = NumberFormat.getIntegerInstance().format(TotalPrice); - StringPrice = StringPrice + 'K'; - } - if (config.ExactValue()) { - StringPrice = NumberFormat.getIntegerInstance().format(TotalPrice); - } - chatMessageManager.queue(QueuedMessage.builder() - .type(ChatMessageType.FRIENDSCHATNOTIFICATION) - .runeLiteFormattedMessage(new ChatMessageBuilder() - .append(ChatColorType.HIGHLIGHT) - .append("Risked Value: ") - .append(ChatColorType.NORMAL) - .append(StringPrice) - .build()) - .build()); - } - equipmentInspectorPanel.update(playerEquipment, playerName); + break; + case 3: + TotalPrice = TotalPrice - Prot1; + TotalPrice = TotalPrice - Prot2; + TotalPrice = TotalPrice - Prot3; + break; + case 4: + TotalPrice = TotalPrice - Prot1; + TotalPrice = TotalPrice - Prot2; + TotalPrice = TotalPrice - Prot3; + TotalPrice = TotalPrice - Prot4; + break; + } + String StringPrice = ""; + if (!config.ExactValue()) + { + TotalPrice = TotalPrice / 1000; + StringPrice = NumberFormat.getIntegerInstance().format(TotalPrice); + StringPrice = StringPrice + 'K'; + } + if (config.ExactValue()) + { + StringPrice = NumberFormat.getIntegerInstance().format(TotalPrice); + } + chatMessageManager.queue(QueuedMessage.builder() + .type(ChatMessageType.FRIENDSCHATNOTIFICATION) + .runeLiteFormattedMessage(new ChatMessageBuilder() + .append(ChatColorType.HIGHLIGHT) + .append("Risked Value: ") + .append(ChatColorType.NORMAL) + .append(StringPrice) + .build()) + .build()); + } + equipmentInspectorPanel.update(playerEquipment, playerName); - } - }); - } - } + } + }); + } + } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/equipmentinspector/ItemPanel.java b/runelite-client/src/main/java/net/runelite/client/plugins/equipmentinspector/ItemPanel.java index 5c1e2101c1..b9b10d2531 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/equipmentinspector/ItemPanel.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/equipmentinspector/ItemPanel.java @@ -24,6 +24,10 @@ */ package net.runelite.client.plugins.equipmentinspector; +import javax.swing.GroupLayout; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; import net.runelite.api.ItemComposition; import net.runelite.api.kit.KitType; import net.runelite.client.game.AsyncBufferedImage; @@ -31,12 +35,8 @@ import net.runelite.client.ui.ColorScheme; import net.runelite.client.ui.FontManager; import org.apache.commons.lang3.StringUtils; -import javax.swing.*; -import javax.swing.border.EmptyBorder; - class ItemPanel extends JPanel { - ItemPanel(ItemComposition item, KitType kitType, AsyncBufferedImage icon) {