Fixes bug where when upgrading, level may not show.

This commit is contained in:
tastybento 2020-07-04 20:36:30 -07:00
parent 85cd89bdf7
commit 45577e446f
2 changed files with 35 additions and 25 deletions

View File

@ -21,7 +21,6 @@ import org.bukkit.World;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable; 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.AddonBaseEvent;
import world.bentobox.bentobox.api.events.addon.AddonEvent; import world.bentobox.bentobox.api.events.addon.AddonEvent;
import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.PanelItem;
@ -126,13 +125,10 @@ public class LevelsManager {
result.complete(null); result.complete(null);
} }
// Save result // Save result
addon.logWarning("Saving results");
setIslandResults(island.getWorld(), island.getOwner(), r); setIslandResults(island.getWorld(), island.getOwner(), r);
// Save top ten // Save top ten
addon.logWarning("Saving top ten");
addon.getManager().saveTopTen(island.getWorld()); addon.getManager().saveTopTen(island.getWorld());
// Save the island scan details // Save the island scan details
addon.logWarning("Saved");
result.complete(r); result.complete(r);
}); });
return result; return result;
@ -435,9 +431,7 @@ public class LevelsManager {
* @param lv - initial island level * @param lv - initial island level
*/ */
public void setInitialIslandLevel(@NonNull Island island, long lv) { 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; if (island.getOwner() == null || island.getWorld() == null) return;
BentoBox.getInstance().logDebug("saving");
levelsCache.computeIfAbsent(island.getOwner(), LevelsData::new).setInitialLevel(island.getWorld(), lv); levelsCache.computeIfAbsent(island.getOwner(), LevelsData::new).setInitialLevel(island.getWorld(), lv);
handler.saveObjectAsync(levelsCache.get(island.getOwner())); handler.saveObjectAsync(levelsCache.get(island.getOwner()));
} }

View File

@ -13,10 +13,18 @@ import org.bukkit.World;
import com.google.common.collect.Multiset; import com.google.common.collect.Multiset;
import com.google.gson.annotations.Expose; import com.google.gson.annotations.Expose;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.database.objects.DataObject; import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.database.objects.Table; 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") @Table(name = "LevelsData")
public class LevelsData implements DataObject { public class LevelsData implements DataObject {
@ -28,23 +36,23 @@ public class LevelsData implements DataObject {
* Map of world name and island level * Map of world name and island level
*/ */
@Expose @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 * Map of world name to island initial level
*/ */
@Expose @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 * Map of world name to points to next level
*/ */
@Expose @Expose
private Map<String, Long> pointsToNextLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); private Map<String, Long> pointsToNextLevel;
@Expose @Expose
private Map<String, Map<Material, Integer>> uwCount = new HashMap<>(); private Map<String, Map<Material, Integer>> uwCount;
@Expose @Expose
private Map<String, Map<Material, Integer>> mdCount = new HashMap<>(); private Map<String, Map<Material, Integer>> mdCount;
/** /**
* Create a level entry for target player * Create a level entry for target player
@ -54,6 +62,11 @@ public class LevelsData implements DataObject {
*/ */
public LevelsData(UUID targetPlayer) { public LevelsData(UUID targetPlayer) {
uniqueId = targetPlayer.toString(); 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) /* (non-Javadoc)
@ -78,6 +91,7 @@ public class LevelsData implements DataObject {
* @return island level * @return island level
*/ */
public Long getLevel(World world) { 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 world == null ? 0L : levels.getOrDefault(world.getName(), 0L);
} }
@ -85,6 +99,7 @@ public class LevelsData implements DataObject {
* @return the levels * @return the levels
*/ */
public Map<String, Long> getLevels() { public Map<String, Long> getLevels() {
if (levels == null) levels = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
return levels; return levels;
} }
@ -92,6 +107,7 @@ public class LevelsData implements DataObject {
* @param levels the levels to set * @param levels the levels to set
*/ */
public void setLevels(Map<String, Long> levels) { public void setLevels(Map<String, Long> levels) {
if (levels == null) levels = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
this.levels = levels; this.levels = levels;
} }
@ -101,6 +117,7 @@ public class LevelsData implements DataObject {
* @param lv - level * @param lv - level
*/ */
public void setLevel(World world, Long lv) { public void setLevel(World world, Long lv) {
if (levels == null) levels = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
String name = world.getName().toLowerCase(Locale.ENGLISH); String name = world.getName().toLowerCase(Locale.ENGLISH);
levels.put(name, lv - this.initialLevel.getOrDefault(name, 0L)); levels.put(name, lv - this.initialLevel.getOrDefault(name, 0L));
} }
@ -111,6 +128,7 @@ public class LevelsData implements DataObject {
* @param level - level * @param level - level
*/ */
public void setInitialLevel(World world, long 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); this.initialLevel.put(world.getName().toLowerCase(Locale.ENGLISH), level);
} }
@ -118,6 +136,7 @@ public class LevelsData implements DataObject {
* @return the initialLevel * @return the initialLevel
*/ */
public Map<String, Long> getInitialLevel() { public Map<String, Long> getInitialLevel() {
if (initialLevel == null) initialLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
return initialLevel; return initialLevel;
} }
@ -125,6 +144,7 @@ public class LevelsData implements DataObject {
* @param initialLevel the initialLevel to set * @param initialLevel the initialLevel to set
*/ */
public void setInitialLevel(Map<String, Long> initialLevel) { public void setInitialLevel(Map<String, Long> initialLevel) {
if (initialLevel == null) initialLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
this.initialLevel = initialLevel; this.initialLevel = initialLevel;
} }
@ -134,6 +154,7 @@ public class LevelsData implements DataObject {
* @return initial island level or 0 by default * @return initial island level or 0 by default
*/ */
public long getInitialLevel(World world) { public long getInitialLevel(World world) {
if (initialLevel == null) initialLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
return initialLevel.getOrDefault(world.getName().toLowerCase(Locale.ENGLISH), 0L); return initialLevel.getOrDefault(world.getName().toLowerCase(Locale.ENGLISH), 0L);
} }
@ -142,7 +163,6 @@ public class LevelsData implements DataObject {
* @param world - world to remove * @param world - world to remove
*/ */
public void remove(World world) { public void remove(World world) {
BentoBox.getInstance().logDebug("Removing world");
this.levels.remove(world.getName().toLowerCase(Locale.ENGLISH)); this.levels.remove(world.getName().toLowerCase(Locale.ENGLISH));
this.initialLevel.remove(world.getName().toLowerCase(Locale.ENGLISH)); this.initialLevel.remove(world.getName().toLowerCase(Locale.ENGLISH));
this.pointsToNextLevel.remove(world.getName().toLowerCase(Locale.ENGLISH)); this.pointsToNextLevel.remove(world.getName().toLowerCase(Locale.ENGLISH));
@ -154,6 +174,7 @@ public class LevelsData implements DataObject {
* @return the pointsToNextLevel * @return the pointsToNextLevel
*/ */
public Map<String, Long> getPointsToNextLevel() { public Map<String, Long> getPointsToNextLevel() {
if (pointsToNextLevel == null) pointsToNextLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
return pointsToNextLevel; return pointsToNextLevel;
} }
@ -161,6 +182,7 @@ public class LevelsData implements DataObject {
* @param pointsToNextLevel the pointsToNextLevel to set * @param pointsToNextLevel the pointsToNextLevel to set
*/ */
public void setPointsToNextLevel(Map<String, Long> pointsToNextLevel) { public void setPointsToNextLevel(Map<String, Long> pointsToNextLevel) {
if (pointsToNextLevel == null) pointsToNextLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
this.pointsToNextLevel = pointsToNextLevel; this.pointsToNextLevel = pointsToNextLevel;
} }
@ -171,6 +193,7 @@ public class LevelsData implements DataObject {
* @param points - points to next level * @param points - points to next level
*/ */
public void setPointsToNextLevel(World world, Long points) { 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); pointsToNextLevel.put(world.getName().toLowerCase(Locale.ENGLISH), points);
} }
@ -181,6 +204,7 @@ public class LevelsData implements DataObject {
* @return points to next level or zero if unknown * @return points to next level or zero if unknown
*/ */
public long getPointsToNextLevel(World world) { public long getPointsToNextLevel(World world) {
if (pointsToNextLevel == null) pointsToNextLevel = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
return pointsToNextLevel.getOrDefault(world.getName().toLowerCase(Locale.ENGLISH), 0L); return pointsToNextLevel.getOrDefault(world.getName().toLowerCase(Locale.ENGLISH), 0L);
} }
@ -188,9 +212,7 @@ public class LevelsData implements DataObject {
* @param uwCount the uwCount to set * @param uwCount the uwCount to set
*/ */
public void setUwCount(World world, Multiset<Material> uwCount) { public void setUwCount(World world, Multiset<Material> uwCount) {
if (this.uwCount == null) { if (this.uwCount == null) this.uwCount = new HashMap<>();
this.uwCount = new HashMap<>();
}
Map<Material, Integer> count = new HashMap<>(); Map<Material, Integer> count = new HashMap<>();
uwCount.forEach(m -> count.put(m, uwCount.count(m))); uwCount.forEach(m -> count.put(m, uwCount.count(m)));
@ -201,9 +223,7 @@ public class LevelsData implements DataObject {
* @param mdCount the mdCount to set * @param mdCount the mdCount to set
*/ */
public void setMdCount(World world, Multiset<Material> mdCount) { public void setMdCount(World world, Multiset<Material> mdCount) {
if (this.mdCount == null) { if (this.mdCount == null) this.mdCount = new HashMap<>();
this.mdCount = new HashMap<>();
}
Map<Material, Integer> count = new HashMap<>(); Map<Material, Integer> count = new HashMap<>();
mdCount.forEach(m -> count.put(m, mdCount.count(m))); mdCount.forEach(m -> count.put(m, mdCount.count(m)));
@ -216,9 +236,7 @@ public class LevelsData implements DataObject {
* @return the uwCount * @return the uwCount
*/ */
public Map<Material, Integer> getUwCount(World world) { public Map<Material, Integer> getUwCount(World world) {
if (this.uwCount == null) { if (this.uwCount == null) this.uwCount = new HashMap<>();
this.uwCount = new HashMap<>();
}
return uwCount.getOrDefault(world.getName(), Collections.emptyMap()); return uwCount.getOrDefault(world.getName(), Collections.emptyMap());
} }
@ -227,9 +245,7 @@ public class LevelsData implements DataObject {
* @return the mdCount * @return the mdCount
*/ */
public Map<Material, Integer> getMdCount(World world) { public Map<Material, Integer> getMdCount(World world) {
if (this.mdCount == null) { if (this.mdCount == null) this.mdCount = new HashMap<>();
this.mdCount = new HashMap<>();
}
return mdCount.getOrDefault(world.getName(), Collections.emptyMap()); return mdCount.getOrDefault(world.getName(), Collections.emptyMap());
} }