Skip to content

Commit

Permalink
Fixes bug where when upgrading, level may not show.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jul 5, 2020
1 parent 85cd89b commit 45577e4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
6 changes: 0 additions & 6 deletions src/main/java/world/bentobox/level/LevelsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.events.addon.AddonBaseEvent;
import world.bentobox.bentobox.api.events.addon.AddonEvent;
import world.bentobox.bentobox.api.panels.PanelItem;
Expand Down Expand Up @@ -126,13 +125,10 @@ public CompletableFuture<Results> calculateLevel(UUID targetPlayer, Island islan
result.complete(null);
}
// Save result
addon.logWarning("Saving results");
setIslandResults(island.getWorld(), island.getOwner(), r);
// Save top ten
addon.logWarning("Saving top ten");
addon.getManager().saveTopTen(island.getWorld());
// Save the island scan details
addon.logWarning("Saved");
result.complete(r);
});
return result;
Expand Down Expand Up @@ -435,9 +431,7 @@ public void saveTopTen(World world) {
* @param lv - initial island level
*/
public void setInitialIslandLevel(@NonNull Island island, long lv) {
BentoBox.getInstance().logDebug("Setting initial island level " + island +" " + lv);
if (island.getOwner() == null || island.getWorld() == null) return;
BentoBox.getInstance().logDebug("saving");
levelsCache.computeIfAbsent(island.getOwner(), LevelsData::new).setInitialLevel(island.getWorld(), lv);
handler.saveObjectAsync(levelsCache.get(island.getOwner()));
}
Expand Down
54 changes: 35 additions & 19 deletions src/main/java/world/bentobox/level/objects/LevelsData.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@
import com.google.common.collect.Multiset;
import com.google.gson.annotations.Expose;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.database.objects.Table;

/**
* Stores the levels data of the user.
* A note - if this class is extended to support new exposed fields and legacy data doesn't include those fields
* they will be set to null by GSON. They will not be initialized and if any attempt is made to use them, then
* the JVM will give up WITHOUT AN ERROR!!! That is why there are null checks throughout this class.
*
* @author tastybento
*
*/
@Table(name = "LevelsData")
public class LevelsData implements DataObject {

Expand All @@ -28,23 +36,23 @@ public class LevelsData implements DataObject {
* Map of world name and island level
*/
@Expose
private Map<String, Long> levels = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
private Map<String, Long> levels;
/**
* Map of world name to island initial level
*/
@Expose
private Map<String, Long> initialLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
private Map<String, Long> initialLevel;
/**
* Map of world name to points to next level
*/
@Expose
private Map<String, Long> pointsToNextLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
private Map<String, Long> pointsToNextLevel;

@Expose
private Map<String, Map<Material, Integer>> uwCount = new HashMap<>();
private Map<String, Map<Material, Integer>> uwCount;

@Expose
private Map<String, Map<Material, Integer>> mdCount = new HashMap<>();
private Map<String, Map<Material, Integer>> mdCount;

/**
* Create a level entry for target player
Expand All @@ -54,6 +62,11 @@ public class LevelsData implements DataObject {
*/
public LevelsData(UUID targetPlayer) {
uniqueId = targetPlayer.toString();
levels = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
initialLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
pointsToNextLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
uwCount = new HashMap<>();
mdCount = new HashMap<>();
}

/* (non-Javadoc)
Expand All @@ -78,20 +91,23 @@ public void setUniqueId(String uniqueId) {
* @return island level
*/
public Long getLevel(World world) {
if (levels == null) levels = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
return world == null ? 0L : levels.getOrDefault(world.getName(), 0L);
}

/**
* @return the levels
*/
public Map<String, Long> getLevels() {
if (levels == null) levels = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
return levels;
}

/**
* @param levels the levels to set
*/
public void setLevels(Map<String, Long> levels) {
if (levels == null) levels = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
this.levels = levels;
}

Expand All @@ -101,6 +117,7 @@ public void setLevels(Map<String, Long> levels) {
* @param lv - level
*/
public void setLevel(World world, Long lv) {
if (levels == null) levels = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
String name = world.getName().toLowerCase(Locale.ENGLISH);
levels.put(name, lv - this.initialLevel.getOrDefault(name, 0L));
}
Expand All @@ -111,20 +128,23 @@ public void setLevel(World world, Long lv) {
* @param level - level
*/
public void setInitialLevel(World world, long level) {
if (initialLevel == null) initialLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
this.initialLevel.put(world.getName().toLowerCase(Locale.ENGLISH), level);
}

/**
* @return the initialLevel
*/
public Map<String, Long> getInitialLevel() {
if (initialLevel == null) initialLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
return initialLevel;
}

/**
* @param initialLevel the initialLevel to set
*/
public void setInitialLevel(Map<String, Long> initialLevel) {
if (initialLevel == null) initialLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
this.initialLevel = initialLevel;
}

Expand All @@ -134,6 +154,7 @@ public void setInitialLevel(Map<String, Long> initialLevel) {
* @return initial island level or 0 by default
*/
public long getInitialLevel(World world) {
if (initialLevel == null) initialLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
return initialLevel.getOrDefault(world.getName().toLowerCase(Locale.ENGLISH), 0L);
}

Expand All @@ -142,7 +163,6 @@ public long getInitialLevel(World world) {
* @param world - world to remove
*/
public void remove(World world) {
BentoBox.getInstance().logDebug("Removing world");
this.levels.remove(world.getName().toLowerCase(Locale.ENGLISH));
this.initialLevel.remove(world.getName().toLowerCase(Locale.ENGLISH));
this.pointsToNextLevel.remove(world.getName().toLowerCase(Locale.ENGLISH));
Expand All @@ -154,13 +174,15 @@ public void remove(World world) {
* @return the pointsToNextLevel
*/
public Map<String, Long> getPointsToNextLevel() {
if (pointsToNextLevel == null) pointsToNextLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
return pointsToNextLevel;
}

/**
* @param pointsToNextLevel the pointsToNextLevel to set
*/
public void setPointsToNextLevel(Map<String, Long> pointsToNextLevel) {
if (pointsToNextLevel == null) pointsToNextLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
this.pointsToNextLevel = pointsToNextLevel;
}

Expand All @@ -171,6 +193,7 @@ public void setPointsToNextLevel(Map<String, Long> pointsToNextLevel) {
* @param points - points to next level
*/
public void setPointsToNextLevel(World world, Long points) {
if (pointsToNextLevel == null) pointsToNextLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
pointsToNextLevel.put(world.getName().toLowerCase(Locale.ENGLISH), points);
}

Expand All @@ -181,16 +204,15 @@ public void setPointsToNextLevel(World world, Long points) {
* @return points to next level or zero if unknown
*/
public long getPointsToNextLevel(World world) {
if (pointsToNextLevel == null) pointsToNextLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
return pointsToNextLevel.getOrDefault(world.getName().toLowerCase(Locale.ENGLISH), 0L);
}

/**
* @param uwCount the uwCount to set
*/
public void setUwCount(World world, Multiset<Material> uwCount) {
if (this.uwCount == null) {
this.uwCount = new HashMap<>();
}
if (this.uwCount == null) this.uwCount = new HashMap<>();
Map<Material, Integer> count = new HashMap<>();
uwCount.forEach(m -> count.put(m, uwCount.count(m)));

Expand All @@ -201,9 +223,7 @@ public void setUwCount(World world, Multiset<Material> uwCount) {
* @param mdCount the mdCount to set
*/
public void setMdCount(World world, Multiset<Material> mdCount) {
if (this.mdCount == null) {
this.mdCount = new HashMap<>();
}
if (this.mdCount == null) this.mdCount = new HashMap<>();
Map<Material, Integer> count = new HashMap<>();
mdCount.forEach(m -> count.put(m, mdCount.count(m)));

Expand All @@ -216,9 +236,7 @@ public void setMdCount(World world, Multiset<Material> mdCount) {
* @return the uwCount
*/
public Map<Material, Integer> getUwCount(World world) {
if (this.uwCount == null) {
this.uwCount = new HashMap<>();
}
if (this.uwCount == null) this.uwCount = new HashMap<>();
return uwCount.getOrDefault(world.getName(), Collections.emptyMap());
}

Expand All @@ -227,9 +245,7 @@ public Map<Material, Integer> getUwCount(World world) {
* @return the mdCount
*/
public Map<Material, Integer> getMdCount(World world) {
if (this.mdCount == null) {
this.mdCount = new HashMap<>();
}
if (this.mdCount == null) this.mdCount = new HashMap<>();
return mdCount.getOrDefault(world.getName(), Collections.emptyMap());
}

Expand Down

0 comments on commit 45577e4

Please sign in to comment.