slayer plugin: store task in rs profile configuration

Co-authored-by: Adam <Adam@sigterm.info>
This commit is contained in:
Cyborger1
2021-02-02 23:34:33 -05:00
committed by Adam
parent e4ff82a3ac
commit 40720bd987
3 changed files with 124 additions and 193 deletions

View File

@@ -32,9 +32,21 @@ import net.runelite.client.config.ConfigGroup;
import net.runelite.client.config.ConfigItem;
import net.runelite.client.config.Units;
@ConfigGroup("slayer")
@ConfigGroup(SlayerConfig.GROUP_NAME)
public interface SlayerConfig extends Config
{
String GROUP_NAME = "slayer";
// Key names for stored task values
String TASK_NAME_KEY = "taskName";
String AMOUNT_KEY = "amount";
String INIT_AMOUNT_KEY = "initialAmount";
String TASK_LOC_KEY = "taskLocation";
String STREAK_KEY = "streak";
String POINTS_KEY = "points";
String EXPEDITIOUS_CHARGES_KEY = "expeditious";
String SLAUGHTER_CHARGES_KEY = "slaughter";
@ConfigItem(
position = 1,
keyName = "infobox",
@@ -124,148 +136,4 @@ public interface SlayerConfig extends Config
{
return true;
}
// Stored data
@ConfigItem(
keyName = "taskName",
name = "",
description = "",
hidden = true
)
default String taskName()
{
return "";
}
@ConfigItem(
keyName = "taskName",
name = "",
description = ""
)
void taskName(String key);
@ConfigItem(
keyName = "amount",
name = "",
description = "",
hidden = true
)
default int amount()
{
return -1;
}
@ConfigItem(
keyName = "amount",
name = "",
description = ""
)
void amount(int amt);
@ConfigItem(
keyName = "initialAmount",
name = "",
description = "",
hidden = true
)
default int initialAmount()
{
return -1;
}
@ConfigItem(
keyName = "initialAmount",
name = "",
description = ""
)
void initialAmount(int initialAmount);
@ConfigItem(
keyName = "taskLocation",
name = "",
description = "",
hidden = true
)
default String taskLocation()
{
return "";
}
@ConfigItem(
keyName = "taskLocation",
name = "",
description = ""
)
void taskLocation(String key);
@ConfigItem(
keyName = "streak",
name = "",
description = "",
hidden = true
)
default int streak()
{
return -1;
}
@ConfigItem(
keyName = "streak",
name = "",
description = ""
)
void streak(int streak);
@ConfigItem(
keyName = "points",
name = "",
description = "",
hidden = true
)
default int points()
{
return -1;
}
@ConfigItem(
keyName = "points",
name = "",
description = ""
)
void points(int points);
@ConfigItem(
keyName = "expeditious",
name = "",
description = "",
hidden = true
)
default int expeditious()
{
return -1;
}
@ConfigItem(
keyName = "expeditious",
name = "",
description = ""
)
void expeditious(int expeditious);
@ConfigItem(
keyName = "slaughter",
name = "",
description = "",
hidden = true
)
default int slaughter()
{
return -1;
}
@ConfigItem(
keyName = "slaughter",
name = "",
description = ""
)
void slaughter(int slaughter);
}

View File

@@ -139,6 +139,9 @@ public class SlayerPlugin extends Plugin
@Inject
private SlayerConfig config;
@Inject
private ConfigManager configManager;
@Inject
private OverlayManager overlayManager;
@@ -227,12 +230,17 @@ public class SlayerPlugin extends Plugin
{
cachedXp = client.getSkillExperience(SLAYER);
if (config.amount() != -1
&& !config.taskName().isEmpty())
migrateConfig();
if (getIntProfileConfig(SlayerConfig.AMOUNT_KEY) != -1
&& !getStringProfileConfig(SlayerConfig.TASK_NAME_KEY).isEmpty())
{
setExpeditiousChargeCount(config.expeditious());
setSlaughterChargeCount(config.slaughter());
clientThread.invoke(() -> setTask(config.taskName(), config.amount(), config.initialAmount(), config.taskLocation(), false));
setExpeditiousChargeCount(getIntProfileConfig(SlayerConfig.EXPEDITIOUS_CHARGES_KEY));
setSlaughterChargeCount(getIntProfileConfig(SlayerConfig.SLAUGHTER_CHARGES_KEY));
clientThread.invoke(() -> setTask(getStringProfileConfig(SlayerConfig.TASK_NAME_KEY),
getIntProfileConfig(SlayerConfig.AMOUNT_KEY),
getIntProfileConfig(SlayerConfig.INIT_AMOUNT_KEY),
getStringProfileConfig(SlayerConfig.TASK_LOC_KEY), false));
}
}
@@ -275,27 +283,50 @@ public class SlayerPlugin extends Plugin
taggedNpcs.clear();
break;
case LOGGED_IN:
if (config.amount() != -1
&& !config.taskName().isEmpty()
migrateConfig();
if (getIntProfileConfig(SlayerConfig.AMOUNT_KEY) != -1
&& !getStringProfileConfig(SlayerConfig.TASK_NAME_KEY).isEmpty()
&& loginFlag)
{
setExpeditiousChargeCount(config.expeditious());
setSlaughterChargeCount(config.slaughter());
setTask(config.taskName(), config.amount(), config.initialAmount(), config.taskLocation(), false);
setExpeditiousChargeCount(getIntProfileConfig(SlayerConfig.EXPEDITIOUS_CHARGES_KEY));
setSlaughterChargeCount(getIntProfileConfig(SlayerConfig.SLAUGHTER_CHARGES_KEY));
setTask(getStringProfileConfig(SlayerConfig.TASK_NAME_KEY),
getIntProfileConfig(SlayerConfig.AMOUNT_KEY),
getIntProfileConfig(SlayerConfig.INIT_AMOUNT_KEY),
getStringProfileConfig(SlayerConfig.TASK_LOC_KEY), false);
loginFlag = false;
}
break;
}
}
@VisibleForTesting
int getIntProfileConfig(String key)
{
Integer value = configManager.getRSProfileConfiguration(SlayerConfig.GROUP_NAME, key, int.class);
return value == null ? -1 : value;
}
@VisibleForTesting
String getStringProfileConfig(String key)
{
String value = configManager.getRSProfileConfiguration(SlayerConfig.GROUP_NAME, key, String.class);
return value == null ? "" : value;
}
private void setProfileConfig(String key, Object value)
{
configManager.setRSProfileConfiguration(SlayerConfig.GROUP_NAME, key, value);
}
private void save()
{
config.amount(amount);
config.initialAmount(initialAmount);
config.taskName(taskName);
config.taskLocation(taskLocation);
config.expeditious(expeditiousChargeCount);
config.slaughter(slaughterChargeCount);
setProfileConfig(SlayerConfig.AMOUNT_KEY, amount);
setProfileConfig(SlayerConfig.INIT_AMOUNT_KEY, initialAmount);
setProfileConfig(SlayerConfig.TASK_NAME_KEY, taskName);
setProfileConfig(SlayerConfig.TASK_LOC_KEY, taskLocation);
setProfileConfig(SlayerConfig.EXPEDITIOUS_CHARGES_KEY, expeditiousChargeCount);
setProfileConfig(SlayerConfig.SLAUGHTER_CHARGES_KEY, slaughterChargeCount);
}
@Subscribe
@@ -345,7 +376,7 @@ public class SlayerPlugin extends Plugin
int amount = Integer.parseInt(mAssignBoss.group(2));
setTask(mAssignBoss.group(1), amount, amount);
int points = Integer.parseInt(mAssignBoss.group(3).replaceAll(",", ""));
config.points(points);
setProfileConfig(SlayerConfig.POINTS_KEY, points);
}
else if (mCurrent.find())
{
@@ -363,12 +394,12 @@ public class SlayerPlugin extends Plugin
if (braceletText.contains("bracelet of slaughter"))
{
slaughterChargeCount = SLAUGHTER_CHARGE;
config.slaughter(slaughterChargeCount);
setProfileConfig(SlayerConfig.SLAUGHTER_CHARGES_KEY, slaughterChargeCount);
}
else if (braceletText.contains("expeditious bracelet"))
{
expeditiousChargeCount = EXPEDITIOUS_CHARGE;
config.expeditious(expeditiousChargeCount);
setProfileConfig(SlayerConfig.EXPEDITIOUS_CHARGES_KEY, expeditiousChargeCount);
}
}
@@ -380,12 +411,12 @@ public class SlayerPlugin extends Plugin
Matcher mPoints = REWARD_POINTS.matcher(w.getText());
if (mPoints.find())
{
final int prevPoints = config.points();
final int prevPoints = getIntProfileConfig(SlayerConfig.POINTS_KEY);
int points = Integer.parseInt(mPoints.group(1).replaceAll(",", ""));
if (prevPoints != points)
{
config.points(points);
setProfileConfig(SlayerConfig.POINTS_KEY, points);
removeCounter();
addCounter();
}
@@ -426,7 +457,7 @@ public class SlayerPlugin extends Plugin
amount++;
slaughterChargeCount = mSlaughter.find() ? Integer.parseInt(mSlaughter.group(1)) : SLAUGHTER_CHARGE;
config.slaughter(slaughterChargeCount);
setProfileConfig(SlayerConfig.SLAUGHTER_CHARGES_KEY, slaughterChargeCount);
}
if (chatMsg.startsWith(CHAT_BRACELET_EXPEDITIOUS))
@@ -435,7 +466,7 @@ public class SlayerPlugin extends Plugin
amount--;
expeditiousChargeCount = mExpeditious.find() ? Integer.parseInt(mExpeditious.group(1)) : EXPEDITIOUS_CHARGE;
config.expeditious(expeditiousChargeCount);
setProfileConfig(SlayerConfig.EXPEDITIOUS_CHARGES_KEY, expeditiousChargeCount);
}
if (chatMsg.startsWith(CHAT_BRACELET_EXPEDITIOUS_CHARGE))
@@ -448,7 +479,7 @@ public class SlayerPlugin extends Plugin
}
expeditiousChargeCount = Integer.parseInt(mExpeditious.group(1));
config.expeditious(expeditiousChargeCount);
setProfileConfig(SlayerConfig.EXPEDITIOUS_CHARGES_KEY, expeditiousChargeCount);
}
if (chatMsg.startsWith(CHAT_BRACELET_SLAUGHTER_CHARGE))
@@ -460,7 +491,7 @@ public class SlayerPlugin extends Plugin
}
slaughterChargeCount = Integer.parseInt(mSlaughter.group(1));
config.slaughter(slaughterChargeCount);
setProfileConfig(SlayerConfig.SLAUGHTER_CHARGES_KEY, slaughterChargeCount);
}
if (chatMsg.startsWith("You've completed") && (chatMsg.contains("Slayer master") || chatMsg.contains("Slayer Master")))
@@ -479,12 +510,12 @@ public class SlayerPlugin extends Plugin
if (mTasks != null)
{
int streak = Integer.parseInt(mTasks.replace(",", ""));
config.streak(streak);
setProfileConfig(SlayerConfig.STREAK_KEY, streak);
}
if (mPoints != null)
{
int points = Integer.parseInt(mPoints.replace(",", ""));
config.points(points);
setProfileConfig(SlayerConfig.POINTS_KEY, points);
}
}
@@ -597,7 +628,7 @@ public class SlayerPlugin extends Plugin
@Subscribe
private void onConfigChanged(ConfigChanged event)
{
if (!event.getGroup().equals("slayer") || !event.getKey().equals("infobox"))
if (!event.getGroup().equals(SlayerConfig.GROUP_NAME) || !event.getKey().equals("infobox"))
{
return;
}
@@ -627,7 +658,8 @@ public class SlayerPlugin extends Plugin
amount--;
}
config.amount(amount); // save changed value
// save changed value
setProfileConfig(SlayerConfig.AMOUNT_KEY, amount);
if (!config.showInfobox())
{
@@ -772,7 +804,7 @@ public class SlayerPlugin extends Plugin
}
counter = new TaskCounter(taskImg, this, amount);
counter.setTooltip(String.format(taskTooltip, capsString(taskName), config.points(), config.streak()));
counter.setTooltip(String.format(taskTooltip, capsString(taskName), getIntProfileConfig(SlayerConfig.POINTS_KEY), getIntProfileConfig(SlayerConfig.STREAK_KEY)));
infoBoxManager.addInfoBox(counter);
}
@@ -891,4 +923,26 @@ public class SlayerPlugin extends Plugin
{
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
private void migrateConfig()
{
migrateConfigKey(SlayerConfig.TASK_NAME_KEY);
migrateConfigKey(SlayerConfig.AMOUNT_KEY);
migrateConfigKey(SlayerConfig.INIT_AMOUNT_KEY);
migrateConfigKey(SlayerConfig.TASK_LOC_KEY);
migrateConfigKey(SlayerConfig.STREAK_KEY);
migrateConfigKey(SlayerConfig.POINTS_KEY);
migrateConfigKey(SlayerConfig.EXPEDITIOUS_CHARGES_KEY);
migrateConfigKey(SlayerConfig.SLAUGHTER_CHARGES_KEY);
}
private void migrateConfigKey(String key)
{
Object value = configManager.getConfiguration(SlayerConfig.GROUP_NAME, key);
if (value != null)
{
configManager.unsetConfiguration(SlayerConfig.GROUP_NAME, key);
configManager.setRSProfileConfiguration(SlayerConfig.GROUP_NAME, key, value);
}
}
}

View File

@@ -53,6 +53,7 @@ import net.runelite.api.widgets.WidgetInfo;
import net.runelite.client.Notifier;
import net.runelite.client.chat.ChatCommandManager;
import net.runelite.client.chat.ChatMessageManager;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.game.ItemManager;
import net.runelite.client.ui.overlay.OverlayManager;
import net.runelite.client.ui.overlay.infobox.InfoBoxManager;
@@ -64,6 +65,7 @@ import org.junit.runner.RunWith;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import org.mockito.Mock;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@@ -137,6 +139,10 @@ public class SlayerPluginTest
@Bind
Client client;
@Mock
@Bind
ConfigManager configManager;
@Mock
@Bind
SlayerConfig slayerConfig;
@@ -283,7 +289,7 @@ public class SlayerPluginTest
assertEquals("Vet'ion", slayerPlugin.getTaskName());
assertEquals(3, slayerPlugin.getAmount());
verify(slayerConfig).points(914);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.POINTS_KEY, 914);
}
@Test
@@ -296,7 +302,7 @@ public class SlayerPluginTest
assertEquals("Chaos Elemental", slayerPlugin.getTaskName());
assertEquals(3, slayerPlugin.getAmount());
verify(slayerConfig).points(914);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.POINTS_KEY, 914);
}
@Test
@@ -309,7 +315,7 @@ public class SlayerPluginTest
assertEquals("Alchemical Hydra", slayerPlugin.getTaskName());
assertEquals(35, slayerPlugin.getAmount());
verify(slayerConfig).points(724);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.POINTS_KEY, 724);
}
@Test
@@ -440,7 +446,7 @@ public class SlayerPluginTest
when(client.getWidget(WidgetInfo.SLAYER_REWARDS_TOPBAR)).thenReturn(rewardBar);
slayerPlugin.onGameTick(new GameTick());
verify(slayerConfig).points(17566);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.POINTS_KEY, 17566);
}
@Test
@@ -449,7 +455,7 @@ public class SlayerPluginTest
ChatMessage chatMessageEvent = new ChatMessage(null, GAMEMESSAGE, "Perterter", TASK_ONE, null, 0);
slayerPlugin.onChatMessage(chatMessageEvent);
verify(slayerConfig).streak(1);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.STREAK_KEY, 1);
assertEquals("", slayerPlugin.getTaskName());
assertEquals(0, slayerPlugin.getAmount());
}
@@ -460,7 +466,7 @@ public class SlayerPluginTest
ChatMessage chatMessageEvent = new ChatMessage(null, GAMEMESSAGE, "Perterter", TASK_COMPLETE_NO_POINTS, null, 0);
slayerPlugin.onChatMessage(chatMessageEvent);
verify(slayerConfig).streak(3);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.STREAK_KEY, 3);
assertEquals("", slayerPlugin.getTaskName());
assertEquals(0, slayerPlugin.getAmount());
}
@@ -471,10 +477,10 @@ public class SlayerPluginTest
ChatMessage chatMessageEvent = new ChatMessage(null, GAMEMESSAGE, "Perterter", TASK_POINTS, null, 0);
slayerPlugin.onChatMessage(chatMessageEvent);
verify(slayerConfig).streak(9);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.STREAK_KEY, 9);
assertEquals("", slayerPlugin.getTaskName());
assertEquals(0, slayerPlugin.getAmount());
verify(slayerConfig).points(18_000);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.POINTS_KEY, 18_000);
}
@Test
@@ -483,10 +489,10 @@ public class SlayerPluginTest
ChatMessage chatMessageEvent = new ChatMessage(null, GAMEMESSAGE, "Perterter", TASK_LARGE_STREAK, null, 0);
slayerPlugin.onChatMessage(chatMessageEvent);
verify(slayerConfig).streak(2465);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.STREAK_KEY, 2465);
assertEquals("", slayerPlugin.getTaskName());
assertEquals(0, slayerPlugin.getAmount());
verify(slayerConfig).points(131_071);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.POINTS_KEY, 131_071);
}
@Test
@@ -495,7 +501,7 @@ public class SlayerPluginTest
ChatMessage chatMessageEvent = new ChatMessage(null, GAMEMESSAGE, "Perterter", TASK_COMPETE_TURAEL, null, 0);
slayerPlugin.onChatMessage(chatMessageEvent);
verify(slayerConfig).streak(104);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.STREAK_KEY, 104);
assertEquals("", slayerPlugin.getTaskName());
assertEquals(0, slayerPlugin.getAmount());
}
@@ -506,8 +512,8 @@ public class SlayerPluginTest
ChatMessage chatMessageEvent = new ChatMessage(null, GAMEMESSAGE, "", TASK_MAX_STREAK, null, 0);
slayerPlugin.onChatMessage(chatMessageEvent);
verify(slayerConfig).streak(16_000);
verify(slayerConfig).points(131_071);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.STREAK_KEY, 16000);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.POINTS_KEY, 131_071);
assertEquals("", slayerPlugin.getTaskName());
assertEquals(0, slayerPlugin.getAmount());
}
@@ -518,8 +524,8 @@ public class SlayerPluginTest
ChatMessage chatMessageEvent = new ChatMessage(null, GAMEMESSAGE, "", TASK_MAX_POINTS, null, 0);
slayerPlugin.onChatMessage(chatMessageEvent);
verify(slayerConfig).streak(9);
verify(slayerConfig).points(131_071);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.STREAK_KEY, 9);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.POINTS_KEY, 131_071);
assertEquals("", slayerPlugin.getTaskName());
assertEquals(0, slayerPlugin.getAmount());
}
@@ -530,8 +536,8 @@ public class SlayerPluginTest
ChatMessage chatMessageEvent = new ChatMessage(null, GAMEMESSAGE, "", TASK_WILDERNESS, null, 0);
slayerPlugin.onChatMessage(chatMessageEvent);
verify(slayerConfig).streak(9);
verify(slayerConfig).points(18_000);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.STREAK_KEY, 9);
verify(configManager).setRSProfileConfiguration(SlayerConfig.GROUP_NAME, SlayerConfig.POINTS_KEY, 18_000);
assertEquals("", slayerPlugin.getTaskName());
assertEquals(0, slayerPlugin.getAmount());
}
@@ -919,7 +925,10 @@ public class SlayerPluginTest
@Test
public void infoboxNotAddedOnLogin()
{
when(slayerConfig.taskName()).thenReturn(Task.BLOODVELD.getName());
when(slayerPlugin.getStringProfileConfig(SlayerConfig.TASK_NAME_KEY)).thenReturn(Task.BLOODVELD.getName());
when(slayerPlugin.getIntProfileConfig(SlayerConfig.AMOUNT_KEY)).thenReturn(50);
// Lenient required as this is not called assuming correct plugin logic
lenient().when(slayerConfig.showInfobox()).thenReturn(true);
GameStateChanged loggingIn = new GameStateChanged();
loggingIn.setGameState(GameState.LOGGING_IN);