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() {
|
public boolean isEmpty() {
|
||||||
return this != NickoProfile.EMPTY_PROFILE || (name == null && skin == null);
|
return name == null && skin == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
|
|
@ -22,23 +22,25 @@ public class PlayerDataStore {
|
||||||
public static final HashMap<UUID, NickoProfile> PROFILES = new HashMap<>();
|
public static final HashMap<UUID, NickoProfile> PROFILES = new HashMap<>();
|
||||||
|
|
||||||
public Optional<NickoProfile> getData(UUID uuid) {
|
public Optional<NickoProfile> getData(UUID uuid) {
|
||||||
if(storage.isError()) {
|
if (storage.isError()) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PROFILES.containsKey(uuid)) {
|
if (PROFILES.containsKey(uuid)) {
|
||||||
return Optional.of(PROFILES.get(uuid));
|
return Optional.of(PROFILES.get(uuid));
|
||||||
} else if (storage.isStored(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));
|
retrievedProfile.ifPresent(profile -> PROFILES.put(uuid, profile));
|
||||||
return retrievedProfile;
|
return retrievedProfile;
|
||||||
} else {
|
} else {
|
||||||
return Optional.empty();
|
final NickoProfile newProfile = NickoProfile.EMPTY_PROFILE;
|
||||||
|
PROFILES.put(uuid, newProfile);
|
||||||
|
return Optional.of(newProfile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<NickoProfile> getOfflineData(String name) {
|
public Optional<NickoProfile> getOfflineData(String name) {
|
||||||
if(storage.isError()) {
|
if (storage.isError()) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +57,7 @@ public class PlayerDataStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveData(Player player) {
|
public void saveData(Player player) {
|
||||||
if(storage.isError()) {
|
if (storage.isError()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,11 +53,11 @@ public class JSONStorage extends Storage {
|
||||||
final File file = new File(directory, uuid.toString() + ".json");
|
final File file = new File(directory, uuid.toString() + ".json");
|
||||||
try (FileReader fileReader = new FileReader(file)) {
|
try (FileReader fileReader = new FileReader(file)) {
|
||||||
try (BufferedReader reader = new BufferedReader(fileReader)) {
|
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);
|
return Optional.of(value);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue