Rebuild top tens on load.

https://github.com/BentoBoxWorld/Level/issues/187
This commit is contained in:
tastybento 2020-09-07 11:20:45 -07:00
parent 4efc5cbc06
commit 732d2ea039
2 changed files with 34 additions and 15 deletions

View File

@ -13,6 +13,7 @@ import java.util.Objects;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -65,7 +66,7 @@ public class LevelsManager {
private final Database<TopTenData> topTenHandler; private final Database<TopTenData> topTenHandler;
// Top ten lists // Top ten lists
private Map<World,TopTenData> topTenLists; private final Map<World,TopTenData> topTenLists;
// Background // Background
private final PanelItem background; private final PanelItem background;
@ -82,7 +83,7 @@ public class LevelsManager {
// Initialize the cache // Initialize the cache
levelsCache = new HashMap<>(); levelsCache = new HashMap<>();
// Initialize top ten lists // Initialize top ten lists
topTenLists = new HashMap<>(); topTenLists = new ConcurrentHashMap<>();
// Background // Background
background = new PanelItemBuilder().icon(Material.BLACK_STAINED_GLASS_PANE).name(" ").build(); background = new PanelItemBuilder().icon(Material.BLACK_STAINED_GLASS_PANE).name(" ").build();
} }
@ -140,6 +141,21 @@ public class LevelsManager {
} }
} }
/**
* Add an island to a top ten
* @param island - island to add
* @param lv - level
* @return true if successful, false if not added
*/
private boolean addToTopTen(Island island, long lv) {
if (island != null && island.getOwner() != null && hasTopTenPerm(island.getWorld(), island.getOwner())) {
topTenLists.computeIfAbsent(island.getWorld(), k -> new TopTenData(island.getWorld()))
.getTopTen().put(island.getOwner(), lv);
return true;
}
return false;
}
/** /**
* Calculate the island level, set all island member's levels to the result and try to add the owner to the top ten * Calculate the island level, set all island member's levels to the result and try to add the owner to the top ten
* @param targetPlayer - uuid of targeted player - owner or team member * @param targetPlayer - uuid of targeted player - owner or team member
@ -430,18 +446,19 @@ public class LevelsManager {
* Loads all the top tens from the database * Loads all the top tens from the database
*/ */
void loadTopTens() { void loadTopTens() {
topTenLists = new HashMap<>(); topTenLists.clear();
topTenHandler.loadObjects().forEach(tt -> { Bukkit.getScheduler().runTaskAsynchronously(addon.getPlugin(), () -> {
World world = Bukkit.getWorld(tt.getUniqueId()); addon.log("Generating Top Ten Tables");
if (world != null) { handler.loadObjects().forEach(il -> {
topTenLists.put(world, tt); if (il.getLevel() > 0) {
addon.log("Loaded top ten for " + world.getName()); addon.getIslands().getIslandById(il.getUniqueId()).ifPresent(i -> this.addToTopTen(i, il.getLevel()));
// Update based on user data }
// Remove any non island owners });
tt.getTopTen().keySet().removeIf(u -> !addon.getIslands().isOwner(world, u)); topTenLists.keySet().forEach(w -> {
} else { addon.log("Loaded top ten for " + w.getName());
addon.logError("TopTen world '" + tt.getUniqueId() + "' is not known on server. You might want to delete this table. Skipping..."); this.saveTopTen(w);
} });
}); });
} }

View File

@ -22,7 +22,9 @@ import world.bentobox.bentobox.database.objects.Table;
@Table(name = "IslandLevels") @Table(name = "IslandLevels")
public class IslandLevels implements DataObject { public class IslandLevels implements DataObject {
// uniqueId is the island's UUID /**
* uniqueId is the island's UUID
*/
@Expose @Expose
private String uniqueId = ""; private String uniqueId = "";