xptracker: move initialization until after login

This fixes the xptracker not being properly initialized if it starts up
after the player is already logged in.
This commit is contained in:
Adam
2019-08-07 19:08:26 -04:00
parent f300c541dc
commit 3b40f945a7

View File

@@ -120,6 +120,7 @@ public class XpTrackerPlugin extends Plugin
private long lastTickMillis = 0; private long lastTickMillis = 0;
private boolean fetchXp; // fetch lastXp for the online xp tracker private boolean fetchXp; // fetch lastXp for the online xp tracker
private long lastXp = 0; private long lastXp = 0;
private boolean initializeTracker;
private final XpClient xpClient = new XpClient(); private final XpClient xpClient = new XpClient();
private final XpState xpState = new XpState(); private final XpState xpState = new XpState();
@@ -153,8 +154,9 @@ public class XpTrackerPlugin extends Plugin
clientToolbar.addNavigation(navButton); clientToolbar.addNavigation(navButton);
// Initialize the last xp if already logged in // Initialize the tracker & last xp if already logged in
fetchXp = true; fetchXp = true;
initializeTracker = true;
} }
@Override @Override
@@ -188,8 +190,14 @@ public class XpTrackerPlugin extends Plugin
fetchXp = true; fetchXp = true;
lastWorldType = type; lastWorldType = type;
resetState(); resetState();
// Must be set from hitting the LOGGING_IN case below
assert initializeTracker;
} }
} }
else if (state == GameState.LOGGING_IN)
{
initializeTracker = true;
}
else if (state == GameState.LOGIN_SCREEN) else if (state == GameState.LOGIN_SCREEN)
{ {
Player local = client.getLocalPlayer(); Player local = client.getLocalPlayer();
@@ -327,6 +335,12 @@ public class XpTrackerPlugin extends Plugin
final int startGoalXp = startGoal != null ? client.getVar(startGoal) : -1; final int startGoalXp = startGoal != null ? client.getVar(startGoal) : -1;
final int endGoalXp = endGoal != null ? client.getVar(endGoal) : -1; final int endGoalXp = endGoal != null ? client.getVar(endGoal) : -1;
if (initializeTracker)
{
// This is the XP sync on login, wait until after login to begin counting
return;
}
if (xpTrackerConfig.hideMaxed() && currentLevel >= Experience.MAX_REAL_LEVEL) if (xpTrackerConfig.hideMaxed() && currentLevel >= Experience.MAX_REAL_LEVEL)
{ {
return; return;
@@ -345,18 +359,9 @@ public class XpTrackerPlugin extends Plugin
final XpUpdateResult updateResult = xpState.updateSkill(skill, currentXp, startGoalXp, endGoalXp); final XpUpdateResult updateResult = xpState.updateSkill(skill, currentXp, startGoalXp, endGoalXp);
xpPanel.updateSkillExperience(updateResult == XpUpdateResult.UPDATED, xpPauseState.isPaused(skill), skill, xpState.getSkillSnapshot(skill)); xpPanel.updateSkillExperience(updateResult == XpUpdateResult.UPDATED, xpPauseState.isPaused(skill), skill, xpState.getSkillSnapshot(skill));
if (skill == Skill.CONSTRUCTION && updateResult == XpUpdateResult.INITIALIZED) // Also update the total experience
{ xpState.updateSkill(Skill.OVERALL, client.getOverallExperience(), -1, -1);
// Construction is the last skill initialized on login, now initialize the total experience xpPanel.updateTotal(xpState.getTotalSnapshot());
long overallXp = client.getOverallExperience();
log.debug("Initializing XP tracker with {} overall exp", overallXp);
xpState.initializeSkill(Skill.OVERALL, overallXp);
}
else if (xpState.isInitialized(Skill.OVERALL))
{
xpState.updateSkill(Skill.OVERALL, client.getOverallExperience(), -1, -1);
xpPanel.updateTotal(xpState.getTotalSnapshot());
}
} }
@Subscribe @Subscribe
@@ -382,12 +387,43 @@ public class XpTrackerPlugin extends Plugin
@Subscribe @Subscribe
public void onGameTick(GameTick event) public void onGameTick(GameTick event)
{ {
rebuildSkills(); if (initializeTracker)
{
initializeTracker = false;
// Initialize the tracker with the initial xp if not already initialized
for (Skill skill : Skill.values())
{
if (skill == Skill.OVERALL)
{
continue;
}
if (!xpState.isInitialized(skill))
{
final int currentXp = client.getSkillExperience(skill);
// goal exps are not necessary for skill initialization
XpUpdateResult xpUpdateResult = xpState.updateSkill(skill, currentXp, -1, -1);
assert xpUpdateResult == XpUpdateResult.INITIALIZED;
}
}
// Initialize the overall xp
if (!xpState.isInitialized(Skill.OVERALL))
{
long overallXp = client.getOverallExperience();
log.debug("Initializing XP tracker with {} overall exp", overallXp);
xpState.initializeSkill(Skill.OVERALL, overallXp);
}
}
if (fetchXp) if (fetchXp)
{ {
lastXp = client.getOverallExperience(); lastXp = client.getOverallExperience();
fetchXp = false; fetchXp = false;
} }
rebuildSkills();
} }
@Subscribe @Subscribe