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);
}
}
}