Use a constant instead of 10 throughout code.

This commit is contained in:
tastybento 2021-05-02 07:54:28 -07:00
parent e0736fca4c
commit eba7e1d531
6 changed files with 27 additions and 24 deletions

View File

@ -42,6 +42,9 @@ import world.bentobox.level.requests.TopTenRequestHandler;
*/ */
public class Level extends Addon implements Listener { public class Level extends Addon implements Listener {
// The 10 in top ten
public static final int TEN = 10;
// Settings // Settings
private ConfigSettings settings; private ConfigSettings settings;
private Config<ConfigSettings> configObject = new Config<>(this, ConfigSettings.class); private Config<ConfigSettings> configObject = new Config<>(this, ConfigSettings.class);
@ -201,16 +204,16 @@ public class Level extends Addon implements Listener {
String getRankName(World world, int rank) { String getRankName(World world, int rank) {
if (rank < 1) rank = 1; if (rank < 1) rank = 1;
if (rank > 10) rank = 10; if (rank > TEN) rank = TEN;
return getPlayers().getName(getManager().getTopTen(world, 10).keySet().stream().skip(rank - 1L).limit(1L).findFirst().orElse(null)); return getPlayers().getName(getManager().getTopTen(world, TEN).keySet().stream().skip(rank - 1L).limit(1L).findFirst().orElse(null));
} }
String getRankLevel(World world, int rank) { String getRankLevel(World world, int rank) {
if (rank < 1) rank = 1; if (rank < 1) rank = 1;
if (rank > 10) rank = 10; if (rank > TEN) rank = TEN;
return getManager() return getManager()
.formatLevel(getManager() .formatLevel(getManager()
.getTopTen(world, 10) .getTopTen(world, TEN)
.values() .values()
.stream() .stream()
.skip(rank - 1L) .skip(rank - 1L)

View File

@ -241,7 +241,7 @@ public class LevelsManager {
*/ */
public void getGUI(World world, final User user) { public void getGUI(World world, final User user) {
// Check world // Check world
Map<UUID, Long> topTen = getTopTen(world, 10); Map<UUID, Long> topTen = getTopTen(world, Level.TEN);
PanelBuilder panel = new PanelBuilder() PanelBuilder panel = new PanelBuilder()
.name(user.getTranslation("island.top.gui-title")) .name(user.getTranslation("island.top.gui-title"))
@ -441,9 +441,9 @@ public class LevelsManager {
public int getRank(@NonNull World world, UUID uuid) { public int getRank(@NonNull World world, UUID uuid) {
createAndCleanRankings(world); createAndCleanRankings(world);
Stream<Entry<UUID, Long>> stream = topTenLists.get(world).getTopTen().entrySet().stream() Stream<Entry<UUID, Long>> stream = topTenLists.get(world).getTopTen().entrySet().stream()
.filter(e -> addon.getIslands().isOwner(world, e.getKey())) .filter(e -> addon.getIslands().isOwner(world, e.getKey()))
.filter(l -> l.getValue() > 0) .filter(l -> l.getValue() > 0)
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue())); .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()));
return takeWhile(stream, x -> !x.getKey().equals(uuid)).map(Map.Entry::getKey).collect(Collectors.toList()).size() + 1; return takeWhile(stream, x -> !x.getKey().equals(uuid)).map(Map.Entry::getKey).collect(Collectors.toList()).size() + 1;
} }

View File

@ -30,7 +30,7 @@ public class AdminTopCommand extends CompositeCommand {
public boolean execute(User user, String label, List<String> args) { public boolean execute(User user, String label, List<String> args) {
user.sendMessage("island.top.gui-title"); user.sendMessage("island.top.gui-title");
int rank = 0; int rank = 0;
for (Map.Entry<UUID, Long> topTen : levelPlugin.getManager().getTopTen(getWorld(), 10).entrySet()) { for (Map.Entry<UUID, Long> topTen : levelPlugin.getManager().getTopTen(getWorld(), Level.TEN).entrySet()) {
Island island = getPlugin().getIslands().getIsland(getWorld(), topTen.getKey()); Island island = getPlugin().getIslands().getIsland(getWorld(), topTen.getKey());
if (island != null) { if (island != null) {
rank++; rank++;

View File

@ -59,7 +59,7 @@ public class AdminTopRemoveCommand extends CompositeCommand {
@Override @Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) { public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
return Optional.of(addon.getManager().getTopTen(getWorld(), 10).keySet().stream().map(addon.getPlayers()::getName) return Optional.of(addon.getManager().getTopTen(getWorld(), Level.TEN).keySet().stream().map(addon.getPlayers()::getName)
.filter(n -> !n.isEmpty()).collect(Collectors.toList())); .filter(n -> !n.isEmpty()).collect(Collectors.toList()));
} }
} }

View File

@ -54,6 +54,6 @@ public class TopTenRequestHandler extends AddonRequestHandler {
} }
// No null check required // No null check required
return addon.getManager().getTopTen(Bukkit.getWorld((String) map.get(WORLD_NAME)), 10); return addon.getManager().getTopTen(Bukkit.getWorld((String) map.get(WORLD_NAME)), Level.TEN);
} }
} }

View File

@ -337,7 +337,7 @@ public class LevelsManagerTest {
*/ */
@Test @Test
public void testGetTopTenEmpty() { public void testGetTopTenEmpty() {
Map<UUID, Long> tt = lm.getTopTen(world, 10); Map<UUID, Long> tt = lm.getTopTen(world, Level.TEN);
assertTrue(tt.isEmpty()); assertTrue(tt.isEmpty());
} }
@ -347,7 +347,7 @@ public class LevelsManagerTest {
@Test @Test
public void testGetTopTen() { public void testGetTopTen() {
testLoadTopTens(); testLoadTopTens();
Map<UUID, Long> tt = lm.getTopTen(world, 10); Map<UUID, Long> tt = lm.getTopTen(world, Level.TEN);
assertFalse(tt.isEmpty()); assertFalse(tt.isEmpty());
assertEquals(1, tt.size()); assertEquals(1, tt.size());
assertEquals(1, lm.getTopTen(world, 1).size()); assertEquals(1, lm.getTopTen(world, 1).size());
@ -360,7 +360,7 @@ public class LevelsManagerTest {
public void testGetTopTenNoOwners() { public void testGetTopTenNoOwners() {
when(im.isOwner(eq(world), any())).thenReturn(false); when(im.isOwner(eq(world), any())).thenReturn(false);
testLoadTopTens(); testLoadTopTens();
Map<UUID, Long> tt = lm.getTopTen(world, 10); Map<UUID, Long> tt = lm.getTopTen(world, Level.TEN);
assertTrue(tt.isEmpty()); assertTrue(tt.isEmpty());
} }
@ -394,10 +394,10 @@ public class LevelsManagerTest {
@Test @Test
public void testRemoveEntry() { public void testRemoveEntry() {
testLoadTopTens(); testLoadTopTens();
Map<UUID, Long> tt = lm.getTopTen(world, 10); Map<UUID, Long> tt = lm.getTopTen(world, Level.TEN);
assertTrue(tt.containsKey(uuid)); assertTrue(tt.containsKey(uuid));
lm.removeEntry(world, uuid); lm.removeEntry(world, uuid);
tt = lm.getTopTen(world, 10); tt = lm.getTopTen(world, Level.TEN);
assertFalse(tt.containsKey(uuid)); assertFalse(tt.containsKey(uuid));
} }
@ -414,8 +414,8 @@ public class LevelsManagerTest {
*/ */
@Test @Test
public void testSetInitialIslandLevel() { public void testSetInitialIslandLevel() {
lm.setInitialIslandLevel(island, 10); lm.setInitialIslandLevel(island, Level.TEN);
assertEquals(10, lm.getInitialLevel(island)); assertEquals(Level.TEN, lm.getInitialLevel(island));
} }
/** /**
@ -453,7 +453,7 @@ public class LevelsManagerTest {
Map<World, TopTenData> ttl = lm.getTopTenLists(); Map<World, TopTenData> ttl = lm.getTopTenLists();
Map<UUID, Long> tt = ttl.get(world).getTopTen(); Map<UUID, Long> tt = ttl.get(world).getTopTen();
for (long i = 100; i < 150; i++) { for (long i = 100; i < 150; i++) {
tt.put(UUID.randomUUID(), i); tt.put(UUID.randomUUID(), i);
} }
// Put player as lowest rank // Put player as lowest rank
tt.put(uuid, 10L); tt.put(uuid, 10L);