From ee95632d5a210631b1023a94983c5bdd7396b360 Mon Sep 17 00:00:00 2001 From: James <38226001+f0rmatme@users.noreply.github.com> Date: Thu, 27 Jun 2019 14:21:32 -0700 Subject: [PATCH] Added coal bag overlay (#777) * Added coal bag overlay * Added original author of .onChatMessage() listener and .update() to license header. Author originally from pull #6242. * Fixed line endings to be LF * Fixed license header to be consistent with runelite license header. * Added check for RUNELITE menu action type so that method will not trigger while specifying Shift Click options Co-Authored-By: Hydrox6 * Added import for MenuAction class * Combined guard clauses and removed superfluous method calls in MiningCoalBagOverlay constructor. * Changed overlay text colour for coal bag to be white * Removed whitespace and fixed naming of max inventory space constant. * Updated name of full coal bag constant and update methods to be more descriptive * Re-added necessary render method call that was not meant to be removed. Also now call the coal bag amount through the config interface instead of the plugin class. * Added amountOfCoalInBag getter/setter overloads to MiningConfig in order to save the quantity of coal in the bag between sessions. * Improved encapsulation and readability. Added guard to check wthat the message type is a game message (as opposed to a public chat message) * Removed 'position' attribute from showCoalBagOverlay option inn Mining plugin. * Removed unused import * Fix's for errors --- .../plugins/mining/MiningCoalBagOverlay.java | 63 +++++++++ .../client/plugins/mining/MiningConfig.java | 62 +++++++++ .../client/plugins/mining/MiningPlugin.java | 121 ++++++++++++++++-- 3 files changed, 237 insertions(+), 9 deletions(-) create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/mining/MiningCoalBagOverlay.java create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/mining/MiningConfig.java diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/mining/MiningCoalBagOverlay.java b/runelite-client/src/main/java/net/runelite/client/plugins/mining/MiningCoalBagOverlay.java new file mode 100644 index 0000000000..2acbf90aa8 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/mining/MiningCoalBagOverlay.java @@ -0,0 +1,63 @@ +/* + * Copyright 2019 Jarred Vardy + * 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.mining; + +import java.awt.Color; +import java.awt.Graphics2D; +import javax.inject.Inject; +import net.runelite.api.ItemID; +import net.runelite.api.Point; +import net.runelite.api.widgets.WidgetItem; +import net.runelite.client.ui.FontManager; +import net.runelite.client.ui.overlay.WidgetItemOverlay; + +public class MiningCoalBagOverlay extends WidgetItemOverlay +{ + private final MiningPlugin plugin; + private final MiningConfig config; + + @Inject + MiningCoalBagOverlay(MiningPlugin plugin, MiningConfig config) + { + showOnInventory(); + this.plugin = plugin; + this.config = config; + } + + @Override + public void renderItemOverlay(Graphics2D graphics, int itemId, WidgetItem itemWidget) + { + if (!config.showCoalBagOverlay() || (itemId != ItemID.COAL_BAG && itemId != ItemID.COAL_BAG_12019)) + { + return; + } + + graphics.setFont(FontManager.getRunescapeSmallFont()); + graphics.setColor(Color.WHITE); + Point location = itemWidget.getCanvasLocation(); + + graphics.drawString(config.amountOfCoalInCoalBag() + "", location.getX(), location.getY() + 14); + } +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/mining/MiningConfig.java b/runelite-client/src/main/java/net/runelite/client/plugins/mining/MiningConfig.java new file mode 100644 index 0000000000..1d01ce3707 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/mining/MiningConfig.java @@ -0,0 +1,62 @@ +/* + * Copyright 2019 Jarred Vardy + * 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.mining; + +import net.runelite.client.config.Config; +import net.runelite.client.config.ConfigGroup; +import net.runelite.client.config.ConfigItem; + +@ConfigGroup("mining") +public interface MiningConfig extends Config +{ + @ConfigItem( + keyName = "showCoalBagOverlay", + name = "Show coal bag overlay", + description = "Overlays how much coal is inside of your coal bag" + ) + default boolean showCoalBagOverlay() + { + return true; + } + + @ConfigItem( + keyName = "amountOfCoalInCoalBag", + name = "", + description = "To store coal amount between sessions", + hidden = true + ) + default int amountOfCoalInCoalBag() + { + return 0; + } + + @ConfigItem( + keyName = "amountOfCoalInCoalBag", + name = "", + description = "Overload to set coal amount", + hidden = true + ) + void amountOfCoalInCoalBag(int amount); +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/mining/MiningPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/mining/MiningPlugin.java index 0c526222c3..f8f4ae7bc7 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/mining/MiningPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/mining/MiningPlugin.java @@ -1,5 +1,7 @@ /* * Copyright (c) 2019, Adam + * Copyright (c) 2018, Anthony + * Copyright (c) 2019, Jarred Vardy * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -24,16 +26,26 @@ */ package net.runelite.client.plugins.mining; +import com.google.inject.Provides; import java.time.Instant; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.inject.Inject; import lombok.AccessLevel; import lombok.Getter; import lombok.extern.slf4j.Slf4j; +import net.runelite.api.ChatMessageType; import net.runelite.api.Client; import net.runelite.api.GameObject; import net.runelite.api.GameState; +import net.runelite.api.InventoryID; +import net.runelite.api.Item; +import net.runelite.api.ItemContainer; +import net.runelite.api.ItemID; +import net.runelite.api.MenuAction; import static net.runelite.api.ObjectID.DEPLETED_VEIN_26665; import static net.runelite.api.ObjectID.DEPLETED_VEIN_26666; import static net.runelite.api.ObjectID.DEPLETED_VEIN_26667; @@ -45,10 +57,9 @@ import static net.runelite.api.ObjectID.ORE_VEIN_26663; import static net.runelite.api.ObjectID.ORE_VEIN_26664; import net.runelite.api.WallObject; import net.runelite.api.coords.WorldPoint; -import net.runelite.api.events.GameObjectDespawned; -import net.runelite.api.events.GameStateChanged; -import net.runelite.api.events.GameTick; -import net.runelite.api.events.WallObjectSpawned; +import net.runelite.api.events.*; +import net.runelite.api.widgets.WidgetInfo; +import net.runelite.client.config.ConfigManager; import net.runelite.client.eventbus.Subscribe; import net.runelite.client.plugins.Plugin; import net.runelite.client.plugins.PluginDescriptor; @@ -57,8 +68,8 @@ import net.runelite.client.ui.overlay.OverlayManager; @Slf4j @PluginDescriptor( name = "Mining", - description = "Show ore respawn timers", - tags = {"overlay", "skilling", "timers"}, + description = "Show ore respawn timers and coal bag overlay", + tags = {"overlay", "skilling", "timers", "coal", "coalbag", "coal bag"}, enabledByDefault = false ) public class MiningPlugin extends Plugin @@ -66,6 +77,17 @@ public class MiningPlugin extends Plugin private static final int ROCK_DISTANCE = 14; private static final int MINING_GUILD_REGION = 12183; + private static final Pattern COAL_BAG_EMPTY_MESSAGE = Pattern.compile("^The coal bag is (now )?empty\\.$"); + private static final Pattern COAL_BAG_ONE_MESSAGE = Pattern.compile("^The coal bag contains one piece of coal\\.$"); + private static final Pattern COAL_BAG_AMOUNT_MESSAGE = Pattern.compile("^The coal bag contains (\\d+) pieces of coal\\.$"); + + private static final int MAX_INVENTORY_SPACE = 28; + private static final int FULL_COAL_BAG_AMOUNT = 27; + + private static final String FILL_OPTION = "fill"; + private static final String EMPTY_OPTION = "empty"; + + @Inject private Client client; @@ -73,7 +95,13 @@ public class MiningPlugin extends Plugin private OverlayManager overlayManager; @Inject - private MiningOverlay overlay; + private MiningOverlay miningOverlay; + + @Inject + private MiningCoalBagOverlay coalBagOverlay; + + @Inject + private MiningConfig config; @Getter(AccessLevel.PACKAGE) private final List respawns = new ArrayList<>(); @@ -82,16 +110,24 @@ public class MiningPlugin extends Plugin @Override protected void startUp() { - overlayManager.add(overlay); + overlayManager.add(miningOverlay); + overlayManager.add(coalBagOverlay); } @Override protected void shutDown() throws Exception { - overlayManager.remove(overlay); + overlayManager.remove(miningOverlay); + overlayManager.remove(coalBagOverlay); respawns.clear(); } + @Provides + MiningConfig provideConfig(ConfigManager configManager) + { + return configManager.getConfig(MiningConfig.class); + } + @Subscribe public void onGameStateChanged(GameStateChanged event) { @@ -176,6 +212,73 @@ public class MiningPlugin extends Plugin } } + @Subscribe + public void onMenuOptionClicked(MenuOptionClicked event) + { + //TODO: should work hopefully + if (event.getMenuAction() != MenuAction.RUNELITE || event.getActionParam1() != WidgetInfo.INVENTORY.getId()) + { + return; + } + + ItemContainer inventoryItemContainer = client.getItemContainer(InventoryID.INVENTORY); + Item[] inventoryItems = inventoryItemContainer.getItems(); + + switch (event.getOption().toLowerCase()) + { + case FILL_OPTION: + int coalInInventoryCount = (int) Arrays.stream(inventoryItems).filter(i -> i.getId() == ItemID.COAL).count(); + updateAmountOfCoalInBag(coalInInventoryCount); + break; + + case EMPTY_OPTION: + int emptyInventorySpaceCount = (int) Arrays.stream(inventoryItems).filter(i -> i.getId() != -1).count(); + int difference = MAX_INVENTORY_SPACE - emptyInventorySpaceCount; + updateAmountOfCoalInBag(-difference); + break; + } + } + + @Subscribe + public void onChatMessage(ChatMessage event) + { + if (event.getType() != ChatMessageType.GAMEMESSAGE) + { + return; + } + + String chatMsg = event.getMessage(); + if (COAL_BAG_EMPTY_MESSAGE.matcher(chatMsg).find()) + { + updateAmountOfCoalInBag(0); + } + else if (COAL_BAG_ONE_MESSAGE.matcher(chatMsg).find()) + { + updateAmountOfCoalInBag(1); + } + else + { + Matcher matcher = COAL_BAG_AMOUNT_MESSAGE.matcher(chatMsg); + if (matcher.find()) + { + updateAmountOfCoalInBag(Integer.parseInt(matcher.group(1)) - config.amountOfCoalInCoalBag()); + } + } + } + + /** + * Update the player's count of coal in their Coal Bag + * + * @param delta How much to add/subtract from the amount. + * Supply a negative number to subtract, or positive number to add. + */ + protected void updateAmountOfCoalInBag(int delta) + { + // check for upper/lower bounds of amount of coal in a bag + // 0 <= X <= 27 + config.amountOfCoalInCoalBag(Math.max(0, Math.min(FULL_COAL_BAG_AMOUNT, config.amountOfCoalInCoalBag() + delta))); + } + private boolean inMiningGuild() { return client.getLocalPlayer().getWorldLocation().getRegionID() == MINING_GUILD_REGION;