Skip to content

Commit

Permalink
Remove null cache values
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Feb 2, 2020
1 parent b3b6502 commit 4068119
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/main/java/world/bentobox/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import org.bukkit.Bukkit;
Expand Down Expand Up @@ -81,7 +82,12 @@ public long getIslandLevel(World world, @Nullable UUID targetPlayer) {
public LevelsData getLevelsData(@NonNull UUID targetPlayer) {
// Get from database if not in cache
if (!levelsCache.containsKey(targetPlayer) && handler.objectExists(targetPlayer.toString())) {
levelsCache.put(targetPlayer, handler.loadObject(targetPlayer.toString()));
LevelsData ld = handler.loadObject(targetPlayer.toString());
if (ld != null) {
levelsCache.put(targetPlayer, ld);
} else {
handler.deleteID(targetPlayer.toString());
}
}
// Return cached value or null
return levelsCache.get(targetPlayer);
Expand Down Expand Up @@ -215,7 +221,8 @@ public void onEnable() {
* Save the levels to the database
*/
private void save(){
// No async for now
// Remove any potential null values from the cache
levelsCache.values().removeIf(Objects::isNull);
levelsCache.values().forEach(handler::saveObject);
}

Expand Down Expand Up @@ -262,6 +269,8 @@ public void setInitialIslandLevel(@NonNull Island island, long level) {
* @return level or 0 by default
*/
public long getInitialIslandLevel(@NonNull Island island) {
// Remove any potential null values from the cache
levelsCache.values().removeIf(Objects::isNull);
return levelsCache.containsKey(island.getOwner()) ? levelsCache.get(island.getOwner()).getInitialLevel(island.getWorld()) : 0L;
}

Expand Down

0 comments on commit 4068119

Please sign in to comment.