fix(storage): fix some npe and unaccurate check
This commit is contained in:
parent
023cabd4a3
commit
fe5e172d1d
3 changed files with 10 additions and 8 deletions
|
@ -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() {
|
||||
|
|
|
@ -22,23 +22,25 @@ public class PlayerDataStore {
|
|||
public static final HashMap<UUID, NickoProfile> PROFILES = new HashMap<>();
|
||||
|
||||
public Optional<NickoProfile> getData(UUID uuid) {
|
||||
if(storage.isError()) {
|
||||
if (storage.isError()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<NickoProfile> getOfflineData(String name) {
|
||||
if(storage.isError()) {
|
||||
if (storage.isError()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
|
@ -55,7 +57,7 @@ public class PlayerDataStore {
|
|||
}
|
||||
|
||||
public void saveData(Player player) {
|
||||
if(storage.isError()) {
|
||||
if (storage.isError()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue