Refactor Equipment inspector

This commit is contained in:
Scott Burns
2019-05-16 00:45:01 +02:00
parent d39d103bc2
commit b0194eeefd
4 changed files with 270 additions and 232 deletions

View File

@@ -24,41 +24,43 @@
*/ */
package net.runelite.client.plugins.equipmentinspector; package net.runelite.client.plugins.equipmentinspector;
import java.awt.Color;
import net.runelite.client.config.Config; import net.runelite.client.config.Config;
import net.runelite.client.config.ConfigGroup; import net.runelite.client.config.ConfigGroup;
import net.runelite.client.config.ConfigItem; 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") @ConfigGroup("grounditems")
public interface EquipmentInspectorConfig extends Config public interface EquipmentInspectorConfig extends Config
{ {
@ConfigItem( @ConfigItem(
keyName = "ShowValue", keyName = "ShowValue",
name = "Show the total value of the items", name = "Show the total value of the items",
description = "shows the total value of the items", description = "shows the total value of the items",
position = 1 position = 1
) )
default boolean ShowValue() default boolean ShowValue()
{ {
return true; return true;
} }
@ConfigItem(
keyName = "protecteditems", @ConfigItem(
name = "# of protected items", keyName = "protecteditems",
description = "Limit 4", name = "# of protected items",
position = 2 description = "Limit 4",
) position = 2
default int protecteditems() )
{ return 1; } default int protecteditems()
@ConfigItem( {
keyName = "ExactValue", return 1;
name = "Show exact value", }
description = "shows the excact gp value",
position = 3 @ConfigItem(
) keyName = "ExactValue",
default boolean ExactValue() name = "Show exact value",
{ return false; } description = "shows the excact gp value",
position = 3
)
default boolean ExactValue()
{
return false;
}
} }

View File

@@ -24,6 +24,21 @@
*/ */
package net.runelite.client.plugins.equipmentinspector; 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 lombok.extern.slf4j.Slf4j;
import net.runelite.api.ItemComposition; import net.runelite.api.ItemComposition;
import net.runelite.api.kit.KitType; 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.ColorScheme;
import net.runelite.client.ui.PluginPanel; 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 @Slf4j
@Singleton @Singleton
public class EquipmentInspectorPanel extends PluginPanel public class EquipmentInspectorPanel extends PluginPanel
@@ -71,27 +77,27 @@ public class EquipmentInspectorPanel extends PluginPanel
header = new JPanel(); header = new JPanel();
header.setLayout(new BorderLayout()); header.setLayout(new BorderLayout());
header.setBorder(new CompoundBorder( header.setBorder(new CompoundBorder(
BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(58, 58, 58)), BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(58, 58, 58)),
BorderFactory.createEmptyBorder(0, 0, 10, 0))); BorderFactory.createEmptyBorder(0, 0, 10, 0)));
nameLabel = new JLabel(NO_PLAYER_SELECTED); nameLabel = new JLabel(NO_PLAYER_SELECTED);
nameLabel.setForeground(Color.WHITE); nameLabel.setForeground(Color.WHITE);
header.add(nameLabel, BorderLayout.CENTER); header.add(nameLabel, BorderLayout.CENTER);
layout.setHorizontalGroup(layout.createParallelGroup() layout.setHorizontalGroup(layout.createParallelGroup()
.addComponent(equipmentPanels) .addComponent(equipmentPanels)
.addComponent(header) .addComponent(header)
); );
layout.setVerticalGroup(layout.createSequentialGroup() layout.setVerticalGroup(layout.createSequentialGroup()
.addComponent(header) .addComponent(header)
.addGap(10) .addGap(10)
.addComponent(equipmentPanels) .addComponent(equipmentPanels)
); );
update(new HashMap<>(), ""); update(new HashMap<>(), "");
} }
public void update(Map<KitType, ItemComposition> playerEquipment, String playerName) public void update(Map<KitType, ItemComposition> playerEquipment, String playerName)
{ {
if (playerName.isEmpty() || playerName == null) if (playerName.isEmpty())
{ {
nameLabel.setText(NO_PLAYER_SELECTED); nameLabel.setText(NO_PLAYER_SELECTED);
} }
@@ -100,18 +106,18 @@ public class EquipmentInspectorPanel extends PluginPanel
nameLabel.setText("Player: " + playerName); nameLabel.setText("Player: " + playerName);
} }
SwingUtilities.invokeLater(() -> SwingUtilities.invokeLater(() ->
{
equipmentPanels.removeAll();
playerEquipment.forEach((kitType, itemComposition) ->
{ {
equipmentPanels.removeAll(); AsyncBufferedImage itemImage = itemManager.getImage(itemComposition.getId());
playerEquipment.forEach((kitType, itemComposition) -> 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.revalidate();
header.repaint(); header.repaint();
} }
); );
} }
} }

View File

@@ -26,6 +26,18 @@ package net.runelite.client.plugins.equipmentinspector;
import com.google.inject.Provides; 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 lombok.extern.slf4j.Slf4j;
import net.runelite.api.ChatMessageType; import net.runelite.api.ChatMessageType;
import net.runelite.api.Client; 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.ImageUtil;
import net.runelite.client.util.Text; 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( @PluginDescriptor(
name = "Equipment Inspector", name = "Equipment Inspector",
enabledByDefault = false, enabledByDefault = false,
type = PluginType.PVP type = PluginType.PVP
) )
@Slf4j @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"; @Inject
private static final String KICK_OPTION = "Kick"; @Nullable
private Client client;
@Inject @Inject
@Nullable private ItemManager itemManager;
private Client client;
@Inject @Inject
private ItemManager itemManager; private EquipmentInspectorConfig config;
@Inject @Inject
private EquipmentInspectorConfig config; private ChatMessageManager chatMessageManager;
@Inject
private MenuManager menuManager;
@Inject @Inject
private ChatMessageManager chatMessageManager; private ScheduledExecutorService executor;
@Inject
private MenuManager menuManager;
@Inject @Inject
private ScheduledExecutorService executor; 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 @Provides
private ClientToolbar pluginToolbar; EquipmentInspectorConfig provideConfig(ConfigManager configManager)
private NavigationButton navButton; {
private EquipmentInspectorPanel equipmentInspectorPanel; return configManager.getConfig(EquipmentInspectorConfig.class);
private int TotalPrice = 0; }
private int Prot1 = 0;
private int Prot2 = 0;
private int Prot3 = 0;
private int Prot4 = 0;
@Provides @Override
EquipmentInspectorConfig provideConfig(ConfigManager configManager) { protected void startUp() throws Exception
return configManager.getConfig(EquipmentInspectorConfig.class); {
}
@Override equipmentInspectorPanel = injector.getInstance(EquipmentInspectorPanel.class);
protected void startUp() throws Exception { if (client != null)
{
menuManager.addPlayerMenuItem(INSPECT_EQUIPMENT);
}
equipmentInspectorPanel = injector.getInstance(EquipmentInspectorPanel.class); final BufferedImage icon = ImageUtil.getResourceStreamFromClass(this.getClass(), "normal.png");
if (client != null) {
menuManager.addPlayerMenuItem(INSPECT_EQUIPMENT);
}
//synchronized (ImageIO.class) navButton = NavigationButton.builder()
//{ .tooltip("Equipment Inspector")
final BufferedImage icon = ImageUtil.getResourceStreamFromClass(this.getClass(), "normal.png"); .icon(icon)
//} .priority(5)
.panel(equipmentInspectorPanel)
navButton = NavigationButton.builder() .build();
.tooltip("Equipment Inspector")
.icon(icon)
.priority(5)
.panel(equipmentInspectorPanel)
.build();
pluginToolbar.addNavigation(navButton); pluginToolbar.addNavigation(navButton);
} }
@Override @Override
protected void shutDown() throws Exception { protected void shutDown() throws Exception
{
menuManager.removePlayerMenuItem(INSPECT_EQUIPMENT); menuManager.removePlayerMenuItem(INSPECT_EQUIPMENT);
} }
@Subscribe @Subscribe
public void onPlayerMenuOptionClicked(PlayerMenuOptionClicked event) { public void onPlayerMenuOptionClicked(PlayerMenuOptionClicked event)
if (event.getMenuOption().equals(INSPECT_EQUIPMENT)) { {
if (event.getMenuOption().equals(INSPECT_EQUIPMENT))
{
executor.execute(() -> executor.execute(() ->
{ {
try { try
SwingUtilities.invokeAndWait(() -> {
{ SwingUtilities.invokeAndWait(() ->
if (!navButton.isSelected()) { {
navButton.getOnSelect().run(); if (!navButton.isSelected())
} {
}); navButton.getOnSelect().run();
} catch (InterruptedException | InvocationTargetException e) { }
});
}
catch (InterruptedException | InvocationTargetException e)
{
throw new RuntimeException(e); throw new RuntimeException(e);
} }
String playerName = Text.removeTags(event.getMenuTarget()); 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 // 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. // against the playerName in the player cache.
String finalPlayerName = playerName.replace('\u00A0', ' '); String finalPlayerName = playerName.replace('\u00A0', ' ');
System.out.println(finalPlayerName); System.out.println(finalPlayerName);
List<Player> players = client.getPlayers(); List<Player> players = client.getPlayers();
Optional<Player> targetPlayer = players.stream() Optional<Player> targetPlayer = players.stream()
.filter(Objects::nonNull) .filter(Objects::nonNull)
.filter(p -> p.getName().equals(finalPlayerName)).findFirst(); .filter(p -> p.getName().equals(finalPlayerName)).findFirst();
if (targetPlayer.isPresent()) { if (targetPlayer.isPresent())
TotalPrice = 0; {
Prot1 = 0; TotalPrice = 0;
Prot2 = 0; Prot1 = 0;
Prot3 = 0; Prot2 = 0;
Prot4 = 0; Prot3 = 0;
Player p = targetPlayer.get(); Prot4 = 0;
Map<KitType, ItemComposition> playerEquipment = new HashMap<>(); Player p = targetPlayer.get();
Map<KitType, ItemComposition> playerEquipment = new HashMap<>();
for (KitType kitType : KitType.values()) { for (KitType kitType : KitType.values())
if (kitType == KitType.RING) continue; //prevents the equipment inspector from breaking {
if (kitType == KitType.AMMUNITION) continue; if (kitType == KitType.RING)
{
continue; //prevents the equipment inspector from breaking
}
if (kitType == KitType.AMMUNITION)
{
continue;
}
int itemId = p.getPlayerComposition().getEquipmentId(kitType); int itemId = p.getPlayerComposition().getEquipmentId(kitType);
if (itemId != -1) { if (itemId != -1)
ItemComposition itemComposition = client.getItemDefinition(itemId); {
playerEquipment.put(kitType, itemComposition); ItemComposition itemComposition = client.getItemDefinition(itemId);
int ItemPrice = itemManager.getItemPrice(itemId); playerEquipment.put(kitType, itemComposition);
TotalPrice += ItemPrice; int ItemPrice = itemManager.getItemPrice(itemId);
if (ItemPrice > Prot1) { TotalPrice += ItemPrice;
Prot4 = Prot3; if (ItemPrice > Prot1)
Prot3 = Prot2; {
Prot2 = Prot1; Prot4 = Prot3;
Prot3 = Prot2;
Prot2 = Prot1;
Prot1 = ItemPrice; Prot1 = ItemPrice;
} else if (ItemPrice > Prot2) { }
Prot4 = Prot3; else if (ItemPrice > Prot2)
Prot3 = Prot2; {
Prot2 = ItemPrice; Prot4 = Prot3;
} else if (ItemPrice > Prot3) { Prot3 = Prot2;
Prot4 = Prot3; Prot2 = ItemPrice;
Prot3 = ItemPrice; }
} else if (ItemPrice > Prot4) { else if (ItemPrice > Prot3)
Prot4 = ItemPrice; {
} Prot4 = Prot3;
} Prot3 = ItemPrice;
} }
int IgnoredItems = config.protecteditems(); else if (ItemPrice > Prot4)
if (IgnoredItems != 0 && IgnoredItems != 1 && IgnoredItems != 2 && IgnoredItems != 3) { {
IgnoredItems = 4; Prot4 = ItemPrice;
}
}
}
int IgnoredItems = config.protecteditems();
if (IgnoredItems != 0 && IgnoredItems != 1 && IgnoredItems != 2 && IgnoredItems != 3)
{
IgnoredItems = 4;
} }
if (config.ShowValue()) { if (config.ShowValue())
switch (IgnoredItems) { {
case 1: switch (IgnoredItems)
TotalPrice = TotalPrice - Prot1; {
break; case 1:
case 2: TotalPrice = TotalPrice - Prot1;
TotalPrice = TotalPrice - Prot1; break;
TotalPrice = TotalPrice - Prot2; case 2:
TotalPrice = TotalPrice - Prot1;
TotalPrice = TotalPrice - Prot2;
break; break;
case 3: case 3:
TotalPrice = TotalPrice - Prot1; TotalPrice = TotalPrice - Prot1;
TotalPrice = TotalPrice - Prot2; TotalPrice = TotalPrice - Prot2;
TotalPrice = TotalPrice - Prot3; TotalPrice = TotalPrice - Prot3;
break; break;
case 4: case 4:
TotalPrice = TotalPrice - Prot1; TotalPrice = TotalPrice - Prot1;
TotalPrice = TotalPrice - Prot2; TotalPrice = TotalPrice - Prot2;
TotalPrice = TotalPrice - Prot3; TotalPrice = TotalPrice - Prot3;
TotalPrice = TotalPrice - Prot4; TotalPrice = TotalPrice - Prot4;
break; break;
} }
String StringPrice = ""; String StringPrice = "";
if (!config.ExactValue()) { if (!config.ExactValue())
TotalPrice = TotalPrice / 1000; {
StringPrice = NumberFormat.getIntegerInstance().format(TotalPrice); TotalPrice = TotalPrice / 1000;
StringPrice = StringPrice + 'K'; StringPrice = NumberFormat.getIntegerInstance().format(TotalPrice);
} StringPrice = StringPrice + 'K';
if (config.ExactValue()) { }
StringPrice = NumberFormat.getIntegerInstance().format(TotalPrice); if (config.ExactValue())
} {
chatMessageManager.queue(QueuedMessage.builder() StringPrice = NumberFormat.getIntegerInstance().format(TotalPrice);
.type(ChatMessageType.FRIENDSCHATNOTIFICATION) }
.runeLiteFormattedMessage(new ChatMessageBuilder() chatMessageManager.queue(QueuedMessage.builder()
.append(ChatColorType.HIGHLIGHT) .type(ChatMessageType.FRIENDSCHATNOTIFICATION)
.append("Risked Value: ") .runeLiteFormattedMessage(new ChatMessageBuilder()
.append(ChatColorType.NORMAL) .append(ChatColorType.HIGHLIGHT)
.append(StringPrice) .append("Risked Value: ")
.build()) .append(ChatColorType.NORMAL)
.build()); .append(StringPrice)
} .build())
equipmentInspectorPanel.update(playerEquipment, playerName); .build());
}
equipmentInspectorPanel.update(playerEquipment, playerName);
} }
}); });
} }
} }
} }

View File

@@ -24,6 +24,10 @@
*/ */
package net.runelite.client.plugins.equipmentinspector; 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.ItemComposition;
import net.runelite.api.kit.KitType; import net.runelite.api.kit.KitType;
import net.runelite.client.game.AsyncBufferedImage; import net.runelite.client.game.AsyncBufferedImage;
@@ -31,12 +35,8 @@ import net.runelite.client.ui.ColorScheme;
import net.runelite.client.ui.FontManager; import net.runelite.client.ui.FontManager;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
class ItemPanel extends JPanel class ItemPanel extends JPanel
{ {
ItemPanel(ItemComposition item, KitType kitType, AsyncBufferedImage icon) ItemPanel(ItemComposition item, KitType kitType, AsyncBufferedImage icon)
{ {