Skip to content

Commit 5f24b13

Browse files
Fix weird edgecase crash for MinecraftAccount.load() returning null (#25)
2 parents fde5187 + 2003438 commit 5f24b13

4 files changed

Lines changed: 24 additions & 8 deletions

File tree

app_pojavlauncher/src/main/java/com/kdt/mcgui/mcAccountSpinner.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.ArrayList;
4949
import java.util.HashMap;
5050
import java.util.List;
51+
import java.util.Objects;
5152

5253
import fr.spse.extended_view.ExtendedTextView;
5354

@@ -301,14 +302,23 @@ private void pickAccount(int position){
301302
PojavProfile.setCurrentProfile(getContext(), mAccountList.get(position));
302303
selectedAccount = PojavProfile.getCurrentProfileContent(getContext(), mAccountList.get(position));
303304

304-
305305
// WORKAROUND
306306
// Account file corrupted due to previous versions having improper encoding
307307
if (selectedAccount == null){
308-
removeCurrentAccount();
309-
pickAccount(-1);
310-
setSelection(0);
311-
return;
308+
Context ctx = Objects.requireNonNull(getContext());
309+
310+
new AlertDialog.Builder(ctx)
311+
.setCancelable(false)
312+
.setTitle(R.string.account_corrupted)
313+
.setMessage(R.string.login_again)
314+
.setPositiveButton(R.string.delete_account_and_login, (dialog, which) -> {
315+
removeCurrentAccount();
316+
pickAccount(-1);
317+
setSelection(0);
318+
})
319+
.show();
320+
321+
312322
}
313323
setSelection(position);
314324
}else {

app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/PojavProfile.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public static String getCurrentProfileName(Context ctx) {
4040
public static List<MinecraftAccount> getAllProfiles(){
4141
List<MinecraftAccount> mcAccountList = new ArrayList<>();;
4242
for (String accountName : getAllProfilesList()){
43-
mcAccountList.add(MinecraftAccount.load(accountName));
43+
if (MinecraftAccount.load(accountName) != null) {
44+
mcAccountList.add(MinecraftAccount.load(accountName));
45+
}
4446
}
4547
return mcAccountList;
4648
}

app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/value/MinecraftAccount.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import android.util.Base64;
1414

1515
import androidx.annotation.Keep;
16+
import androidx.annotation.Nullable;
1617

1718
import org.apache.commons.io.IOUtils;
1819

@@ -68,7 +69,7 @@ public String save() throws IOException {
6869
public static MinecraftAccount parse(String content) throws JsonSyntaxException {
6970
return Tools.GLOBAL_GSON.fromJson(content, MinecraftAccount.class);
7071
}
71-
72+
@Nullable
7273
public static MinecraftAccount load(String name) {
7374
if(!accountExists(name)) return null;
7475
try {
@@ -92,7 +93,7 @@ public static MinecraftAccount load(String name) {
9293
acc.msaRefreshToken = "0";
9394
}
9495
return acc;
95-
} catch(IOException | JsonSyntaxException e) {
96+
} catch(NullPointerException | IOException | JsonSyntaxException e) {
9697
Log.e(MinecraftAccount.class.getName(), "Caught an exception while loading the profile",e);
9798
return null;
9899
}

app_pojavlauncher/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,9 @@
450450
<string name="change_account">Please change accounts to use this function</string>
451451
<string name="no_minecraft_account_found">No Minecraft Account Found</string>
452452
<string name="feature_requires_java_account">This feature requires a Microsoft account that owns Minecraft Java Edition.</string>
453+
<string name="delete_account_and_login">Delete account and log in</string>
454+
<string name="login_again">Please log in again</string>
455+
<string name="account_corrupted">Selected account is corrupted</string>
453456
<string name="modloader_dl_install_neoforge">Create Neoforge profile</string>
454457
<string name="neoforge_dl_select_version">Select NeoForge version</string>
455458
<string name="neoforge_dl_no_installer">Sorry, but this version of NeoForge does not have an installer, which is not yet supported.</string>

0 commit comments

Comments
 (0)