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 boolean fetchXp; // fetch lastXp for the online xp tracker
private long lastXp = 0;
private boolean initializeTracker;
private final XpClient xpClient = new XpClient();
private final XpState xpState = new XpState();
@@ -153,8 +154,9 @@ public class XpTrackerPlugin extends Plugin
clientToolbar.addNavigation(navButton);
// Initialize the last xp if already logged in
// Initialize the tracker & last xp if already logged in
fetchXp = true;
initializeTracker = true;
}
@Override
@@ -188,8 +190,14 @@ public class XpTrackerPlugin extends Plugin
fetchXp = true;
lastWorldType = type;
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)
{
Player local = client.getLocalPlayer();
@@ -327,6 +335,12 @@ public class XpTrackerPlugin extends Plugin
final int startGoalXp = startGoal != null ? client.getVar(startGoal) : -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)
{
return;
@@ -345,18 +359,9 @@ public class XpTrackerPlugin extends Plugin
final XpUpdateResult updateResult = xpState.updateSkill(skill, currentXp, startGoalXp, endGoalXp);
xpPanel.updateSkillExperience(updateResult == XpUpdateResult.UPDATED, xpPauseState.isPaused(skill), skill, xpState.getSkillSnapshot(skill));
if (skill == Skill.CONSTRUCTION && updateResult == XpUpdateResult.INITIALIZED)
{
// Construction is the last skill initialized on login, now initialize the total experience
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());
}
// Also update the total experience
xpState.updateSkill(Skill.OVERALL, client.getOverallExperience(), -1, -1);
xpPanel.updateTotal(xpState.getTotalSnapshot());
}
@Subscribe
@@ -382,12 +387,43 @@ public class XpTrackerPlugin extends Plugin
@Subscribe
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)
{
lastXp = client.getOverallExperience();
fetchXp = false;
}
rebuildSkills();
}
@Subscribe