Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minimum item stack value and sorting by value #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies {
}

group = 'com.bankmemory'
version = '1.3.0'
version = '1.3.1'

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/bankmemory/BankDiffPanelController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.bankmemory.data.DisplayNameMapper;
import com.bankmemory.data.PluginDataStore;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.inject.Inject;
import javax.swing.SwingUtilities;
Expand All @@ -27,6 +29,7 @@ public class BankDiffPanelController {
@Inject private ItemManager itemManager;
@Inject private PluginDataStore dataStore;
@Inject private ItemListDiffGenerator diffGenerator;
@Inject BankMemoryConfig config;

private BankDiffPanel diffPanel;
private DataUpdateListener dataListener;
Expand Down Expand Up @@ -120,7 +123,17 @@ private void gatherItemDataToDisplay(List<BankItem> differences, boolean keepLis
AsyncBufferedImage icon = itemManager.getImage(i.getItemId(), i.getQuantity(), false);
int geValue = itemManager.getItemPrice(i.getItemId()) * i.getQuantity();
int haValue = ic.getHaPrice() * i.getQuantity();
items.add(new ItemListEntry(ic.getName(), i.getQuantity(), icon, geValue, haValue));

if (Math.abs(geValue) >= config.minValue()) {
items.add(new ItemListEntry(ic.getName(), i.getQuantity(), icon, geValue, haValue));
}
}

if (config.sortMode() == SortMode.VALUE) {
items.sort((item1, item2) -> {
// Sort by geValue in descending order, use absolute values because removed items are displayed as negatives
return Integer.compare(Math.abs(item2.getGeValue()), Math.abs(item1.getGeValue()));
});
}

SwingUtilities.invokeLater(() -> diffPanel.displayItems(items, keepListPosition));
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/bankmemory/BankMemoryConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,23 @@ default boolean showTooltips() {
}

void setTooltips(boolean tooltips);

@ConfigItem(
position = 2,
keyName = "minValue",
name = "Minimum stack value",
description = "Minimum item stack GE value to display"
)
default int minValue() {
return 0;
}
@ConfigItem(
position = 3,
keyName = "sortMode",
name = "Sort mode",
description = "Mode to sort the displayed items"
)
default SortMode sortMode() {
return SortMode.DEFAULT;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/bankmemory/BankMemoryPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
import com.bankmemory.util.Constants;
import com.google.inject.Provides;
import java.awt.image.BufferedImage;
import java.util.Objects;
import javax.inject.Inject;
import javax.swing.SwingUtilities;

import lombok.Getter;
import lombok.Setter;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.InventoryID;
Expand All @@ -21,6 +25,7 @@
import net.runelite.client.callback.ClientThread;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.events.ConfigChanged;
import net.runelite.client.game.ItemManager;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
Expand Down Expand Up @@ -56,6 +61,10 @@ public class BankMemoryPlugin extends Plugin {
private NavigationButton navButton;
private boolean displayNameRegistered = false;

@Getter
@Setter
private boolean configChanged;

@Provides
BankMemoryConfig provideConfig(ConfigManager configManager) {
return configManager.getConfig(BankMemoryConfig.class);
Expand Down Expand Up @@ -89,6 +98,7 @@ protected void startUp() throws Exception {
diffPanelController.startUp(pluginPanel.getSavedBanksTopPanel().getDiffPanel());

overlayManager.add(itemOverlay);
setConfigChanged(false);
}

@Override
Expand All @@ -110,6 +120,14 @@ public void onGameStateChanged(GameStateChanged gameStateChanged) {
}
}

@Subscribe
public void onConfigChanged(ConfigChanged configChanged)
{
if (Objects.equals(configChanged.getGroup(), CONFIG_GROUP)) {
setConfigChanged(true);
}
}

@Subscribe
public void onGameTick(GameTick event) {
if (!displayNameRegistered) {
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/bankmemory/CopyItemsToClipboardAction.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bankmemory;

import com.bankmemory.data.BankItem;
import com.bankmemory.data.BankSave;
import com.bankmemory.util.ClipboardActions;
import com.bankmemory.util.Constants;
Expand All @@ -14,11 +15,13 @@ public abstract class CopyItemsToClipboardAction extends AbstractAction {

private final ClientThread clientThread;
private final ItemManager itemManager;
private final BankMemoryConfig config;

CopyItemsToClipboardAction(ClientThread clientThread, ItemManager itemManager) {
CopyItemsToClipboardAction(ClientThread clientThread, ItemManager itemManager,BankMemoryConfig config) {
super(Constants.ACTION_COPY_ITEM_DATA_TO_CLIPBOARD);
this.clientThread = Objects.requireNonNull(clientThread);
this.itemManager = Objects.requireNonNull(itemManager);
this.config = config;
}

@Override
Expand All @@ -27,8 +30,7 @@ public void actionPerformed(ActionEvent e) {
if (data == null) {
return;
}

ClipboardActions.copyItemDataAsTsvToClipboardOnClientThread(clientThread, itemManager, data.getItemData());
ClipboardActions.copyItemDataAsTsvToClipboardOnClientThread(clientThread, itemManager, config, data.getItemData());
}

@Nullable
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/com/bankmemory/CurrentBankPanelController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class CurrentBankPanelController {
@Inject private ClientThread clientThread;
@Inject private ItemManager itemManager;
@Inject private PluginDataStore dataStore;
@Inject BankMemoryConfig config;
@Inject BankMemoryPlugin plugin;

private BankViewPanel panel;

Expand All @@ -51,7 +53,7 @@ public void startUp(BankViewPanel panel) {
}

private void setPopupMenuActionOnBankView() {
this.panel.setItemListPopupMenuAction(new CopyItemsToClipboardAction(clientThread, itemManager) {
this.panel.setItemListPopupMenuAction(new CopyItemsToClipboardAction(clientThread, itemManager, config) {
@Nullable
@Override
public BankSave getBankItemData() {
Expand Down Expand Up @@ -84,13 +86,18 @@ private void updateDisplayForCurrentAccount() {
}
}

private boolean hasConfigUpdated() //if config has updated reload the list in case people change the min value/sort mode
{
return plugin.isConfigChanged();
}

private void viewBankSave(BankSave bankSave) {
assert client.isClientThread();

dataStore.currentBankViewed(bankSave.getId());

boolean shouldReset = isBankIdentityDifferentToLastDisplayed(bankSave);
boolean shouldUpdateItemsDisplay = shouldReset || isItemDataNew(bankSave);
boolean shouldUpdateItemsDisplay = shouldReset || isItemDataNew(bankSave) || hasConfigUpdated();
List<ItemListEntry> items = new ArrayList<>();
if (shouldUpdateItemsDisplay) {
// Get all the data we need for the UI on this thread (the game thread)
Expand All @@ -100,16 +107,27 @@ private void viewBankSave(BankSave bankSave) {
AsyncBufferedImage icon = itemManager.getImage(i.getItemId(), i.getQuantity(), i.getQuantity() > 1);
int geValue = itemManager.getItemPrice(i.getItemId()) * i.getQuantity();
int haValue = ic.getHaPrice() * i.getQuantity();
items.add(new ItemListEntry(ic.getName(), i.getQuantity(), icon, geValue, haValue));
if (Math.abs(geValue) >= config.minValue()) {
items.add(new ItemListEntry(ic.getName(), i.getQuantity(), icon, geValue, haValue));
}
}
}

if (config.sortMode() == SortMode.VALUE) {
items.sort((item1, item2) -> {
// Sort by geValue in descending order, use absolute values because removed items are displayed as negatives
return Integer.compare(Math.abs(item2.getGeValue()), Math.abs(item1.getGeValue()));
});
}

SwingUtilities.invokeLater(() -> {
if (shouldReset) {
panel.reset();
}
panel.updateTimeDisplay(bankSave.getDateTimeString());
if (shouldUpdateItemsDisplay) {
panel.displayItemListings(items, true);
plugin.setConfigChanged(false); //we've used the value, set it back to false
}
});
latestDisplayedData = bankSave;
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/bankmemory/SavedBanksPanelController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class SavedBanksPanelController {
@Inject private ClientThread clientThread;
@Inject private ItemManager itemManager;
@Inject private PluginDataStore dataStore;
@Inject BankMemoryConfig config;

private BankSavesTopPanel topPanel;
private ImageIcon casketIcon;
Expand Down Expand Up @@ -79,7 +80,7 @@ private void updateCurrentBanksList() {
}

private void setPopupMenuActionOnBankView() {
topPanel.getBankViewPanel().setItemListPopupMenuAction(new CopyItemsToClipboardAction(clientThread, itemManager) {
topPanel.getBankViewPanel().setItemListPopupMenuAction(new CopyItemsToClipboardAction(clientThread, itemManager,config) {
@Nullable
@Override
public BankSave getBankItemData() {
Expand Down Expand Up @@ -109,7 +110,15 @@ private void openSavedBank(BanksListEntry selected) {
AsyncBufferedImage icon = itemManager.getImage(i.getItemId(), i.getQuantity(), i.getQuantity() > 1);
int geValue = itemManager.getItemPrice(i.getItemId()) * i.getQuantity();
int haValue = ic.getHaPrice() * i.getQuantity();
items.add(new ItemListEntry(ic.getName(), i.getQuantity(), icon, geValue, haValue));
if (Math.abs(geValue) >= config.minValue()) {
items.add(new ItemListEntry(ic.getName(), i.getQuantity(), icon, geValue, haValue));
}
}
if (config.sortMode() == SortMode.VALUE) {
items.sort((item1, item2) -> {
// Sort by geValue in descending order, use absolute values because removed items are displayed as negatives
return Integer.compare(Math.abs(item2.getGeValue()), Math.abs(item1.getGeValue()));
});
}
SwingUtilities.invokeLater(() -> {
workingToOpenBank.set(false);
Expand Down Expand Up @@ -151,7 +160,7 @@ public void saveBankAs(BanksListEntry save, String saveName) {
public void copyBankSaveItemDataToClipboard(BanksListEntry save) {
Optional<BankSave> existingSave = dataStore.getBankSaveWithId(save.getSaveId());
if (existingSave.isPresent()) {
ClipboardActions.copyItemDataAsTsvToClipboardOnClientThread(clientThread, itemManager, existingSave.get().getItemData());
ClipboardActions.copyItemDataAsTsvToClipboardOnClientThread(clientThread, itemManager, config, existingSave.get().getItemData());
} else {
log.error("Tried to copy CSV data to clipboard for missing bank save: {}", save);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/bankmemory/SortMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.bankmemory;

public enum SortMode
{
DEFAULT,
VALUE
}
22 changes: 14 additions & 8 deletions src/main/java/com/bankmemory/data/BankSave.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import com.google.gson.annotations.SerializedName;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nullable;

import lombok.Value;
import net.runelite.api.ItemContainer;
import net.runelite.api.ItemID;
Expand All @@ -28,15 +30,22 @@ public class BankSave {
String dateTimeString;
@SerializedName(value = "accountIdentifier", alternate = {"userName"}) String accountIdentifier;
@Nullable String saveName;
ImmutableList<BankItem> itemData;

List<BankItem> itemData;



// public List<BankItem> getItemData() {
//
// }

@VisibleForTesting
public BankSave(
BankWorldType worldType,
String accountIdentifier,
@Nullable String saveName,
String dateTimeString,
ImmutableList<BankItem> itemData) {
List<BankItem> itemData) {
id = ID_BASE + idIncrementer.incrementAndGet();
this.worldType = worldType;
this.accountIdentifier = accountIdentifier;
Expand Down Expand Up @@ -86,8 +95,7 @@ public static BankSave snapshotFromExistingBank(String newName, BankSave existin
existingBank.worldType,
existingBank.accountIdentifier,
newName,
existingBank.dateTimeString,
existingBank.itemData);
existingBank.dateTimeString, existingBank.itemData);
}

public static BankSave cleanItemData(BankSave existingBank) {
Expand All @@ -101,8 +109,7 @@ public static BankSave cleanItemData(BankSave existingBank) {
existingBank.worldType,
existingBank.accountIdentifier,
existingBank.saveName,
existingBank.dateTimeString,
cleanItemData);
existingBank.dateTimeString, cleanItemData);
}

public static BankSave withNewAccountId(String newAccountId, BankSave existingBank) {
Expand All @@ -111,8 +118,7 @@ public static BankSave withNewAccountId(String newAccountId, BankSave existingBa
existingBank.worldType,
newAccountId,
existingBank.saveName,
existingBank.dateTimeString,
existingBank.itemData);
existingBank.dateTimeString, existingBank.itemData);
}

@Override
Expand Down
Loading