Fix potential NPE in YggdrasilAccount(selectedProfile is null)

This commit is contained in:
yushijinhun
2018-06-30 20:54:27 +08:00
parent 9fa56a9cd4
commit cfb0526cbb

View File

@@ -66,40 +66,46 @@ public class YggdrasilAccount extends Account {
@Override @Override
public AuthInfo logIn() throws AuthenticationException { public AuthInfo logIn() throws AuthenticationException {
if (!canPlayOnline()) { if (!canPlayOnline()) {
logInWithToken(); if (service.validate(session.getAccessToken(), session.getClientToken())) {
selectProfile(new SpecificCharacterSelector(characterUUID)); isOnline = true;
} else {
updateSession(service.refresh(session.getAccessToken(), session.getClientToken(), null), new SpecificCharacterSelector(characterUUID));
}
} }
return session.toAuthInfo(); return session.toAuthInfo();
} }
@Override @Override
public final AuthInfo logInWithPassword(String password) throws AuthenticationException { public AuthInfo logInWithPassword(String password) throws AuthenticationException {
return logInWithPassword(password, new SpecificCharacterSelector(characterUUID)); return logInWithPassword(password, new SpecificCharacterSelector(characterUUID));
} }
protected AuthInfo logInWithPassword(String password, CharacterSelector selector) throws AuthenticationException { protected AuthInfo logInWithPassword(String password, CharacterSelector selector) throws AuthenticationException {
session = service.authenticate(username, password, UUIDTypeAdapter.fromUUID(UUID.randomUUID())); updateSession(service.authenticate(username, password, UUIDTypeAdapter.fromUUID(UUID.randomUUID())), selector);
selectProfile(selector);
return session.toAuthInfo(); return session.toAuthInfo();
} }
private void selectProfile(CharacterSelector selector) throws AuthenticationException { /**
if (session.getSelectedProfile() == null) { * Updates the current session. This method shall be invoked after authenticate/refresh operation.
if (session.getAvailableProfiles() == null || session.getAvailableProfiles().length <= 0) * {@link #session} field shall be set only using this method. This method ensures {@link #session}
* has a profile selected.
*
* @param acquiredSession the session acquired by making an authenticate/refresh request
*/
private void updateSession(YggdrasilSession acquiredSession, CharacterSelector selector) throws AuthenticationException {
if (acquiredSession.getSelectedProfile() == null) {
if (acquiredSession.getAvailableProfiles() == null || acquiredSession.getAvailableProfiles().length == 0)
throw new NoCharacterException(this); throw new NoCharacterException(this);
session = service.refresh(session.getAccessToken(), session.getClientToken(), selector.select(this, Arrays.asList(session.getAvailableProfiles()))); this.session = service.refresh(
acquiredSession.getAccessToken(),
acquiredSession.getClientToken(),
selector.select(this, Arrays.asList(acquiredSession.getAvailableProfiles())));
} else {
this.session = acquiredSession;
} }
characterUUID = session.getSelectedProfile().getId(); this.characterUUID = this.session.getSelectedProfile().getId();
}
private void logInWithToken() throws AuthenticationException {
if (service.validate(session.getAccessToken(), session.getClientToken())) {
isOnline = true;
return;
}
session = service.refresh(session.getAccessToken(), session.getClientToken(), null);
} }
@Override @Override