Skip to content

Commit

Permalink
Rebuild top tens on load.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Sep 7, 2020
1 parent 4efc5cb commit 732d2ea
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
45 changes: 31 additions & 14 deletions src/main/java/world/bentobox/level/LevelsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
Expand Down Expand Up @@ -65,7 +66,7 @@ public class LevelsManager {

private final Database<TopTenData> topTenHandler;
// Top ten lists
private Map<World,TopTenData> topTenLists;
private final Map<World,TopTenData> topTenLists;
// Background
private final PanelItem background;

Expand All @@ -82,7 +83,7 @@ public LevelsManager(Level addon) {
// Initialize the cache
levelsCache = new HashMap<>();
// Initialize top ten lists
topTenLists = new HashMap<>();
topTenLists = new ConcurrentHashMap<>();
// Background
background = new PanelItemBuilder().icon(Material.BLACK_STAINED_GLASS_PANE).name(" ").build();
}
Expand Down Expand Up @@ -140,6 +141,21 @@ private void addToTopTen(@NonNull World world, @NonNull UUID targetPlayer, long
}
}

/**
* 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
* @param targetPlayer - uuid of targeted player - owner or team member
Expand Down Expand Up @@ -430,18 +446,19 @@ boolean hasTopTenPerm(@NonNull World world, @NonNull UUID targetPlayer) {
* Loads all the top tens from the database
*/
void loadTopTens() {
topTenLists = new HashMap<>();
topTenHandler.loadObjects().forEach(tt -> {
World world = Bukkit.getWorld(tt.getUniqueId());
if (world != null) {
topTenLists.put(world, tt);
addon.log("Loaded top ten for " + world.getName());
// Update based on user data
// Remove any non island owners
tt.getTopTen().keySet().removeIf(u -> !addon.getIslands().isOwner(world, u));
} else {
addon.logError("TopTen world '" + tt.getUniqueId() + "' is not known on server. You might want to delete this table. Skipping...");
}
topTenLists.clear();
Bukkit.getScheduler().runTaskAsynchronously(addon.getPlugin(), () -> {
addon.log("Generating Top Ten Tables");
handler.loadObjects().forEach(il -> {
if (il.getLevel() > 0) {
addon.getIslands().getIslandById(il.getUniqueId()).ifPresent(i -> this.addToTopTen(i, il.getLevel()));
}
});
topTenLists.keySet().forEach(w -> {
addon.log("Loaded top ten for " + w.getName());
this.saveTopTen(w);
});

});
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/world/bentobox/level/objects/IslandLevels.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
@Table(name = "IslandLevels")
public class IslandLevels implements DataObject {

// uniqueId is the island's UUID
/**
* uniqueId is the island's UUID
*/
@Expose
private String uniqueId = "";

Expand Down

0 comments on commit 732d2ea

Please sign in to comment.