mirror of
https://github.com/BentoBoxWorld/Level.git
synced 2025-02-20 05:32:25 +01:00
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:
parent
b92d412f0a
commit
0a768b0648
@ -14,7 +14,7 @@ import world.bentobox.bentobox.database.objects.DataObject;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This class stores and sorts the top ten.
|
* This class stores and sorts the top ten.
|
||||||
* @author ben
|
* @author tastybento
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class TopTenData implements DataObject {
|
public class TopTenData implements DataObject {
|
||||||
@ -27,8 +27,11 @@ public class TopTenData implements DataObject {
|
|||||||
|
|
||||||
public Map<UUID, Long> getTopTen() {
|
public Map<UUID, Long> getTopTen() {
|
||||||
// Remove any entries that have level values less than 1
|
// Remove any entries that have level values less than 1
|
||||||
topTen.values().removeIf(l -> l < 1);
|
//topTen.values().removeIf(l -> l < 1);
|
||||||
return topTen.entrySet().stream()
|
// 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)
|
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).limit(10)
|
||||||
.collect(Collectors.toMap(
|
.collect(Collectors.toMap(
|
||||||
Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
|
Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
|
||||||
|
@ -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()}.
|
* Test method for {@link world.bentobox.level.objects.TopTenData#getUniqueId()}.
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user