Refactor reputation tracking

This commit is contained in:
Patrick
2022-07-14 14:42:46 +04:00
parent b3793a7876
commit 47ced20608
3 changed files with 100 additions and 62 deletions

View File

@@ -8,15 +8,7 @@ import net.runelite.api.GameObject;
import net.runelite.api.GameState; import net.runelite.api.GameState;
import net.runelite.api.InventoryID; import net.runelite.api.InventoryID;
import net.runelite.api.Skill; import net.runelite.api.Skill;
import net.runelite.api.events.GameObjectDespawned; import net.runelite.api.events.*;
import net.runelite.api.events.GameObjectSpawned;
import net.runelite.api.events.GameStateChanged;
import net.runelite.api.events.GameTick;
import net.runelite.api.events.ItemContainerChanged;
import net.runelite.api.events.NpcDespawned;
import net.runelite.api.events.NpcSpawned;
import net.runelite.api.events.ScriptPostFired;
import net.runelite.api.events.StatChanged;
import net.runelite.api.widgets.Widget; import net.runelite.api.widgets.Widget;
import net.runelite.client.Notifier; import net.runelite.client.Notifier;
import net.runelite.client.callback.ClientThread; import net.runelite.client.callback.ClientThread;
@@ -54,21 +46,6 @@ public class EasyGiantsFoundryPlugin extends Plugin
private static final int PREFORM = 27010; private static final int PREFORM = 27010;
private static final int SHOP_WIDGET = 753;
private static final int CHAT_WIDGET = 229;
private static final int SHOP_POINTS_TEXT = 13;
private static final int CHAT_POINTS_TEXT = 1;
private static final Pattern pattern = Pattern.compile("quality: (?<points>\\d+) Best");
@Getter
private int shopPoints;
private boolean increasePoints = false;
private Stage oldStage; private Stage oldStage;
private int lastBoost; private int lastBoost;
@@ -108,6 +85,10 @@ public class EasyGiantsFoundryPlugin extends Plugin
@Inject @Inject
private ConfigManager configManager; private ConfigManager configManager;
@Getter
@Inject
private ReputationTracker reputationTracker;
@Override @Override
protected void startUp() protected void startUp()
{ {
@@ -163,11 +144,7 @@ public class EasyGiantsFoundryPlugin extends Plugin
if (event.getGameState().equals(GameState.LOGGED_IN)) if (event.getGameState().equals(GameState.LOGGED_IN))
{ {
Integer points = configManager.getRSProfileConfiguration(config.GROUP, config.POINTS_KEY, int.class); reputationTracker.load();
if (points != null)
{
shopPoints = points;
}
} }
} }
@@ -193,7 +170,7 @@ public class EasyGiantsFoundryPlugin extends Plugin
oldStage = state.getCurrentStage(); oldStage = state.getCurrentStage();
} }
else if (config.showGiantsFoundryHeatNotifications() && else if (config.showGiantsFoundryHeatNotifications() &&
helper.getActionsForHeatLevel() == config.HeatNotificationsThreshold()) helper.getActionsForHeatLevel() == config.HeatNotificationsThreshold())
{ {
notifier.notify("About to run out of heat!"); notifier.notify("About to run out of heat!");
} }
@@ -252,7 +229,7 @@ public class EasyGiantsFoundryPlugin extends Plugin
public void onItemContainerChanged(ItemContainerChanged event) public void onItemContainerChanged(ItemContainerChanged event)
{ {
if (event.getContainerId() == InventoryID.EQUIPMENT.getId() if (event.getContainerId() == InventoryID.EQUIPMENT.getId()
&& event.getItemContainer().count(PREFORM) == 0) && event.getItemContainer().count(PREFORM) == 0)
{ {
state.reset(); state.reset();
oldStage = null; oldStage = null;
@@ -289,44 +266,19 @@ public class EasyGiantsFoundryPlugin extends Plugin
public void onGameTick(GameTick event) public void onGameTick(GameTick event)
{ {
checkBonus(); checkBonus();
Widget chat = client.getWidget(CHAT_WIDGET, CHAT_POINTS_TEXT);
Widget shop = client.getWidget(SHOP_WIDGET, SHOP_POINTS_TEXT);
if (shop != null && shop.getText() != null && Integer.parseInt(shop.getText()) != shopPoints)
{
shopPoints = Integer.parseInt(shop.getText());
storePoints();
return;
}
if (chat == null)
{
increasePoints = true;
return;
}
String chatText = Text.sanitizeMultilineText(chat.getText());
final Matcher matcher = pattern.matcher(chatText);
if (increasePoints && matcher.find())
{
shopPoints += Integer.parseInt(matcher.group("points"));
storePoints();
increasePoints = false;
}
} }
private void storePoints() @Subscribe
public void onWidgetLoaded(WidgetLoaded event)
{ {
configManager.setRSProfileConfiguration(config.GROUP, config.POINTS_KEY, shopPoints); reputationTracker.onWidgetLoaded(event.getGroupId());
} }
private void checkBonus() private void checkBonus()
{ {
if (!state.isEnabled() || state.getCurrentStage() == null if (!state.isEnabled() || state.getCurrentStage() == null
|| state.getCurrentStage().getHeat() != state.getCurrentHeat() || state.getCurrentStage().getHeat() != state.getCurrentHeat()
|| !BonusWidget.isActive(client)) || !BonusWidget.isActive(client))
{ {
bonusNotified = false; bonusNotified = false;
return; return;

View File

@@ -91,7 +91,7 @@ public class FoundryOverlay2D extends OverlayPanel
); );
} }
int points = plugin.getShopPoints(); int points = plugin.getReputationTracker().getShopPoints();
if (config.drawShopPoints()) if (config.drawShopPoints())
{ {
panelComponent.getChildren().add( panelComponent.getChildren().add(

View File

@@ -0,0 +1,86 @@
package com.toofifty.easygiantsfoundry;
import lombok.Getter;
import net.runelite.api.Client;
import net.runelite.api.widgets.Widget;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.util.Text;
import javax.inject.Inject;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReputationTracker
{
private static final int SHOP_WIDGET = 753;
private static final int CHAT_WIDGET = 229;
private static final int SHOP_POINTS_TEXT = 13;
private static final int CHAT_POINTS_TEXT = 1;
private static final Pattern pattern = Pattern.compile("quality: (?<points>\\d+) Best");
@Getter
private int shopPoints;
@Inject
private ConfigManager configManager;
@Inject
private Client client;
public void load()
{
Integer points = configManager.getRSProfileConfiguration(EasyGiantsFoundryConfig.GROUP, EasyGiantsFoundryConfig.POINTS_KEY, int.class);
if (points != null)
{
shopPoints = points;
}
}
private void save()
{
configManager.setRSProfileConfiguration(EasyGiantsFoundryConfig.GROUP, EasyGiantsFoundryConfig.POINTS_KEY, shopPoints);
}
public void onWidgetLoaded(int groupId)
{
if (groupId == SHOP_WIDGET)
{
shopOpened();
}
else if (groupId == CHAT_WIDGET)
{
chatBox();
}
}
private void chatBox()
{
Widget chat = client.getWidget(CHAT_WIDGET, CHAT_POINTS_TEXT);
if (chat == null)
{
return;
}
String chatText = Text.sanitizeMultilineText(chat.getText());
final Matcher matcher = pattern.matcher(chatText);
if (matcher.find())
{
shopPoints += Integer.parseInt(matcher.group("points"));
save();
}
}
private void shopOpened()
{
Widget shop = client.getWidget(SHOP_WIDGET, SHOP_POINTS_TEXT);
if (shop != null && shop.getText() != null && Integer.parseInt(shop.getText()) != shopPoints)
{
shopPoints = Integer.parseInt(shop.getText());
save();
}
}
}