Adds some protection around TopTen generation.

Maybe related to https://github.com/BentoBoxWorld/BSkyBlock/issues/312

It appears that the map is being corrupted due to multithreading, but
it's not clear where that is happening.
This commit is contained in:
tastybento 2020-05-01 16:43:09 -07:00
parent b92d412f0a
commit 0a768b0648
2 changed files with 29 additions and 3 deletions

View File

@ -14,7 +14,7 @@ import world.bentobox.bentobox.database.objects.DataObject;
/**
* This class stores and sorts the top ten.
* @author ben
* @author tastybento
*
*/
public class TopTenData implements DataObject {
@ -27,8 +27,11 @@ public class TopTenData implements DataObject {
public Map<UUID, Long> getTopTen() {
// Remove any entries that have level values less than 1
topTen.values().removeIf(l -> l < 1);
return topTen.entrySet().stream()
//topTen.values().removeIf(l -> l < 1);
// make copy
Map<UUID, Long> snapTopTen = Collections.unmodifiableMap(topTen);
return snapTopTen.entrySet().stream()
.filter(l -> l.getValue() > 0)
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).limit(10)
.collect(Collectors.toMap(
Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));

View File

@ -59,6 +59,29 @@ public class TopTenDataTest {
}
}
/**
* Test method for {@link world.bentobox.level.objects.TopTenData#setTopTen(java.util.Map)}.
*/
@Test
public void testSetAndGetTopTenShort() {
Map<UUID, Long> topTen2 = new LinkedHashMap<>();
// Create a top ten map
for (long i = 0; i < 3; i++) {
topTen2.put(UUID.randomUUID(), i);
}
// Add the top player
topTen2.put(uuid, 3L);
ttd.setTopTen(topTen2);
// Three only
assertEquals(3, ttd.getTopTen().size());
// Check order
long i = 3;
for (long l : ttd.getTopTen().values()) {
System.out.println(l);
assertEquals(i--, l);
}
}
/**
* Test method for {@link world.bentobox.level.objects.TopTenData#getUniqueId()}.
*/