Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/world/bentobox/level/LevelsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ private boolean addToTopTen(Island island, long lv) {
&& hasTopTenPerm(island.getWorld(), island.getOwner())) {
topTenLists.computeIfAbsent(island.getWorld(), k -> new TopTenData(island.getWorld())).getTopTen()
.put(island.getUniqueId(), lv);
// Invalidate the cache for this world because of the update
cache.remove(island.getWorld());
return true;
}
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/world/bentobox/level/objects/TopTenData.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package world.bentobox.level.objects;

import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.bukkit.World;

Expand All @@ -20,7 +20,7 @@ public class TopTenData {
@Expose
private String uniqueId = "";
@Expose
private Map<String, Long> topTen = new LinkedHashMap<>();
private Map<String, Long> topTen = new ConcurrentHashMap<>();

public TopTenData(World k) {
uniqueId = k.getName().toLowerCase(Locale.ENGLISH);
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/world/bentobox/level/LevelsManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,43 @@ public void testSetIslandLevel() {

}

/**
* Test method for
* {@link world.bentobox.level.LevelsManager#getTopTen(org.bukkit.World, int)}.
* Verifies that the top ten is sorted in descending order by level.
*/
@Test
public void testGetTopTenSortOrder() {
lm.createAndCleanRankings(world);
Map<World, TopTenData> ttl = lm.getTopTenLists();
Map<String, Long> tt = ttl.get(world).getTopTen();
// Add islands in non-sorted order, mimicking the reported issue
String island65 = UUID.randomUUID().toString();
String island1065 = UUID.randomUUID().toString();
String island500 = UUID.randomUUID().toString();
String island200 = UUID.randomUUID().toString();
// Insert in arbitrary order
tt.put(island65, 65L);
tt.put(island1065, 1065L);
tt.put(island500, 500L);
tt.put(island200, 200L);
when(im.getIslandById(island65)).thenReturn(Optional.of(island));
when(im.getIslandById(island1065)).thenReturn(Optional.of(island));
when(im.getIslandById(island500)).thenReturn(Optional.of(island));
when(im.getIslandById(island200)).thenReturn(Optional.of(island));

Map<String, Long> topTen = lm.getTopTen(world, Level.TEN);
// Verify descending order
long previousLevel = Long.MAX_VALUE;
for (Long level : topTen.values()) {
assertTrue("Top ten not in descending order: " + level + " should be <= " + previousLevel,
level <= previousLevel);
previousLevel = level;
}
// Verify highest is first
assertEquals(1065L, topTen.values().iterator().next().longValue());
}

/**
* Test method for
* {@link world.bentobox.level.LevelsManager#getRank(World, UUID)}
Expand Down