From 844a99734bf7f2396904ce3996fddd3047c55355 Mon Sep 17 00:00:00 2001 From: Keebler408 <85420839+Keebler408@users.noreply.github.com> Date: Sat, 18 Dec 2021 03:48:38 -0600 Subject: Fix per-profile fairy soul bug that wipes progress for all but one location (#32) --- .../notenoughupdates/miscfeatures/FairySouls.java | 60 ++++++---------------- 1 file changed, 16 insertions(+), 44 deletions(-) (limited to 'src') diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/FairySouls.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/FairySouls.java index 968a9f95..8d611845 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/FairySouls.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/FairySouls.java @@ -135,60 +135,32 @@ public class FairySouls { } public static void load(File file, Gson gson) { - HashMap>> foundSoulsList; - BufferedReader reader; + loadedFoundSouls = new HashMap<>(); + String fileContent; try { - reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)); + fileContent = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) + .lines() + .collect(Collectors.joining(System.lineSeparator())); } catch (FileNotFoundException e) { - loadedFoundSouls = new HashMap<>(); return; } - String fileContent = reader - .lines() - .collect(Collectors.joining(System.lineSeparator())); - if (file.exists()) { + try { + //noinspection UnstableApiUsage + Type multiProfileSoulsType = new TypeToken>>>() { + }.getType(); + loadedFoundSouls = gson.fromJson(fileContent, multiProfileSoulsType); + } catch (JsonSyntaxException e) { + //The file is in the old format, convert it to the new one and set the profile to unknown try { //noinspection UnstableApiUsage - Type type = new TypeToken>>>() { + Type singleProfileSoulsType = new TypeToken>>() { }.getType(); - - foundSoulsList = gson.fromJson(fileContent, type); - for (Map.Entry>> soulsJson : foundSoulsList.entrySet()) { - String profileName = soulsJson.getKey(); - for (Map.Entry> souls : soulsJson.getValue().entrySet()) { - HashMap> map = new HashMap<>(); - map.put(souls.getKey(), souls.getValue()); - loadedFoundSouls.put(profileName, map); - } - } - return; - } catch (JsonSyntaxException e) { - //The file is in the old format, convert it to the new one and set the profile to unknown - try { - //noinspection UnstableApiUsage - Type type = new TypeToken>>() { - }.getType(); - - HashMap> foundSoulsListOldFormat = gson.fromJson(fileContent, type); - HashMap> map = new HashMap<>(); - for (Map.Entry> entry : foundSoulsListOldFormat.entrySet()) { - HashSet set = new HashSet<>(); - for (Number n : entry.getValue()) { - set.add(n.intValue()); - } - map.put(entry.getKey(), set); - } - loadedFoundSouls.put(unknownProfile, map); - return; - } catch (JsonSyntaxException e2) { - System.err.println("Can't read file containing collected fairy souls, resetting."); - loadedFoundSouls = new HashMap<>(); - return; - } + loadedFoundSouls.put(unknownProfile, gson.fromJson(fileContent, singleProfileSoulsType)); + } catch (JsonSyntaxException e2) { + System.err.println("Can't read file containing collected fairy souls, resetting."); } } - loadedFoundSouls = new HashMap<>(); } public static void save(File file, Gson gson) { -- cgit