fix(storage): fix some npe and unaccurate check

This commit is contained in:
aroooo 2022-10-26 20:13:24 +02:00
parent 023cabd4a3
commit fe5e172d1d
3 changed files with 10 additions and 8 deletions

View file

@ -12,7 +12,7 @@ public class NickoProfile {
}
public boolean isEmpty() {
return this != NickoProfile.EMPTY_PROFILE || (name == null && skin == null);
return name == null && skin == null;
}
public String getName() {

View file

@ -29,11 +29,13 @@ public class PlayerDataStore {
if (PROFILES.containsKey(uuid)) {
return Optional.of(PROFILES.get(uuid));
} else if (storage.isStored(uuid)) {
Optional<NickoProfile> retrievedProfile = storage.retrieve(uuid);
final Optional<NickoProfile> retrievedProfile = storage.retrieve(uuid);
retrievedProfile.ifPresent(profile -> PROFILES.put(uuid, profile));
return retrievedProfile;
} else {
return Optional.empty();
final NickoProfile newProfile = NickoProfile.EMPTY_PROFILE;
PROFILES.put(uuid, newProfile);
return Optional.of(newProfile);
}
}

View file

@ -53,11 +53,11 @@ public class JSONStorage extends Storage {
final File file = new File(directory, uuid.toString() + ".json");
try (FileReader fileReader = new FileReader(file)) {
try (BufferedReader reader = new BufferedReader(fileReader)) {
NickoProfile value = gson.fromJson(reader, NickoProfile.class);
final NickoProfile value = gson.fromJson(reader, NickoProfile.class);
return Optional.of(value);
}
} catch (IOException e) {
throw new RuntimeException(e);
return Optional.empty();
}
}