From aea258fb5cac5a5539fe7294bab57cea61f4dfa6 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 11 May 2020 16:19:29 -0400 Subject: [PATCH] ge plugin: add buy limit reset timer Co-authored-by: renfc --- .../grandexchange/GrandExchangeConfig.java | 22 +++++-- .../grandexchange/GrandExchangePlugin.java | 64 +++++++++++++++++-- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/grandexchange/GrandExchangeConfig.java b/runelite-client/src/main/java/net/runelite/client/plugins/grandexchange/GrandExchangeConfig.java index 3268aa6716..9cb66911af 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/grandexchange/GrandExchangeConfig.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/grandexchange/GrandExchangeConfig.java @@ -28,9 +28,11 @@ import net.runelite.client.config.Config; import net.runelite.client.config.ConfigGroup; import net.runelite.client.config.ConfigItem; -@ConfigGroup("grandexchange") +@ConfigGroup(GrandExchangeConfig.CONFIG_GROUP) public interface GrandExchangeConfig extends Config { + String CONFIG_GROUP = "grandexchange"; + @ConfigItem( position = 1, keyName = "quickLookup", @@ -77,6 +79,18 @@ public interface GrandExchangeConfig extends Config @ConfigItem( position = 5, + keyName = "enableGELimitReset", + name = "Enable GE Limit Reset Timer", + description = "Shows when GE Trade limits reset (H:MM)" + ) + + default boolean enableGELimitReset() + { + return true; + } + + @ConfigItem( + position = 6, keyName = "showTotal", name = "Show grand exchange total", description = "Show grand exchange total" @@ -87,7 +101,7 @@ public interface GrandExchangeConfig extends Config } @ConfigItem( - position = 6, + position = 7, keyName = "showExact", name = "Show exact total value", description = "Show exact total value" @@ -98,7 +112,7 @@ public interface GrandExchangeConfig extends Config } @ConfigItem( - position = 7, + position = 8, keyName = "highlightSearchMatch", name = "Highlight Search Match", description = "Highlights the search match with an underline" @@ -109,7 +123,7 @@ public interface GrandExchangeConfig extends Config } @ConfigItem( - position = 8, + position = 9, keyName = "geSearchMode", name = "Search Mode", description = "The search mode to use for the GE
" diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/grandexchange/GrandExchangePlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/grandexchange/GrandExchangePlugin.java index 76f51245ee..8215e16675 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/grandexchange/GrandExchangePlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/grandexchange/GrandExchangePlugin.java @@ -30,18 +30,18 @@ package net.runelite.client.plugins.grandexchange; import com.google.common.annotations.VisibleForTesting; import com.google.common.primitives.Shorts; -import com.google.common.reflect.TypeToken; import com.google.gson.Gson; import com.google.inject.Provides; import java.awt.Color; import java.awt.image.BufferedImage; import java.io.IOException; +import java.time.Duration; +import java.time.Instant; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Locale; -import java.util.Map; import java.util.concurrent.ScheduledExecutorService; import java.util.function.ToIntFunction; import java.util.stream.Collectors; @@ -98,6 +98,7 @@ import net.runelite.http.api.ge.GrandExchangeTrade; import net.runelite.http.api.item.ItemStats; import net.runelite.http.api.osbuddy.OSBGrandExchangeClient; import net.runelite.http.api.osbuddy.OSBGrandExchangeResult; +import org.apache.commons.lang3.time.DurationFormatUtils; import org.apache.commons.text.similarity.FuzzyScore; @PluginDescriptor( @@ -114,10 +115,9 @@ public class GrandExchangePlugin extends Plugin private static final String OSB_GE_TEXT = "
OSBuddy Actively traded price: "; private static final String BUY_LIMIT_GE_TEXT = "
Buy limit: "; + private static final String BUY_LIMIT_KEY = "buylimit_"; private static final Gson GSON = new Gson(); - private static final TypeToken> BUY_LIMIT_TOKEN = new TypeToken>() - { - }; + private static final Duration BUY_LIMIT_RESET = Duration.ofHours(4); static final String SEARCH_GRAND_EXCHANGE = "Search Grand Exchange"; @@ -315,7 +315,7 @@ public class GrandExchangePlugin extends Plugin @Subscribe public void onConfigChanged(ConfigChanged event) { - if (event.getGroup().equals("grandexchange")) + if (event.getGroup().equals(GrandExchangeConfig.CONFIG_GROUP)) { if (event.getKey().equals("quickLookup")) { @@ -403,6 +403,8 @@ public class GrandExchangePlugin extends Plugin savedOffer.setSpent(offer.getSpent()); savedOffer.setState(offer.getState()); setOffer(slot, savedOffer); + + updateLimitTimer(offer); } } @@ -679,6 +681,46 @@ public class GrandExchangePlugin extends Plugin stringStack[stringStackSize - 1] += titleBuilder.toString(); } + private void setLimitResetTime(int itemId) + { + Instant lastDateTime = configManager.getConfiguration(GrandExchangeConfig.CONFIG_GROUP, + BUY_LIMIT_KEY + client.getUsername().toLowerCase() + "." + itemId, Instant.class); + if (lastDateTime == null || lastDateTime.isBefore(Instant.now())) + { + configManager.setConfiguration(GrandExchangeConfig.CONFIG_GROUP, + BUY_LIMIT_KEY + client.getUsername().toLowerCase() + "." + itemId, + Instant.now().plus(BUY_LIMIT_RESET)); + } + } + + private Instant getLimitResetTime(int itemId) + { + Instant lastDateTime = configManager.getConfiguration(GrandExchangeConfig.CONFIG_GROUP, + BUY_LIMIT_KEY + client.getUsername().toLowerCase() + "." + itemId, Instant.class); + if (lastDateTime == null) + { + return null; + } + + if (lastDateTime.isBefore(Instant.now())) + { + configManager.unsetConfiguration(GrandExchangeConfig.CONFIG_GROUP, BUY_LIMIT_KEY + client.getUsername().toLowerCase() + "." + itemId); + return null; + } + + return lastDateTime; + } + + private void updateLimitTimer(GrandExchangeOffer offer) + { + if (offer.getState() == GrandExchangeOfferState.BOUGHT || + (offer.getQuantitySold() > 0 && + offer.getState() == GrandExchangeOfferState.BUYING)) + { + setLimitResetTime(offer.getItemId()); + } + } + private void rebuildGeText() { if (grandExchangeText == null || grandExchangeItem == null || grandExchangeItem.isHidden()) @@ -709,6 +751,16 @@ public class GrandExchangePlugin extends Plugin } } + if (config.enableGELimitReset()) + { + Instant resetTime = getLimitResetTime(itemId); + if (resetTime != null) + { + Duration remaining = Duration.between(Instant.now(), resetTime); + text += " (" + DurationFormatUtils.formatDuration(remaining.toMillis(), "H:mm") + ")"; + } + } + geText.setText(text); if (!config.enableOsbPrices())