Skip to content

Commit

Permalink
Fixes issue with null placeholder error
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jun 26, 2020
1 parent a6be22b commit 1a7d48a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
16 changes: 12 additions & 4 deletions src/main/java/world/bentobox/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,27 @@ private void registerPlaceholders(GameModeAddon gm) {

}

private String getRankName(World world, int rank) {
String getRankName(World world, int rank) {
if (rank < 1) rank = 1;
if (rank > 10) rank = 10;
return getPlayers().getName(getManager().getTopTen(world, 10).keySet().stream().skip(rank - 1L).limit(1L).findFirst().orElse(null));
}

private String getRankLevel(World world, int rank) {
String getRankLevel(World world, int rank) {
if (rank < 1) rank = 1;
if (rank > 10) rank = 10;
return getManager().formatLevel(getManager().getTopTen(world, 10).values().stream().skip(rank - 1L).limit(1L).findFirst().orElse(null));
return getManager()
.formatLevel(getManager()
.getTopTen(world, 10)
.values()
.stream()
.skip(rank - 1L)
.limit(1L)
.findFirst()
.orElse(null));
}

private String getVisitedIslandLevel(GameModeAddon gm, User user) {
String getVisitedIslandLevel(GameModeAddon gm, User user) {
if (!gm.inWorld(user.getLocation())) return "";
return getIslands().getIslandAt(user.getLocation())
.map(island -> getManager().getIslandLevelString(gm.getOverWorld(), island.getOwner()))
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/world/bentobox/level/LevelsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ private boolean fireIslandLevelCalcEvent(UUID targetPlayer, Island island, Resul
* @param lvl - long value to represent
* @return string of the level.
*/
public String formatLevel(long lvl) {
public String formatLevel(@Nullable Long lvl) {
if (lvl == null) return "";
String level = String.valueOf(lvl);
// Asking for the level of another player
if(addon.getSettings().isShorthand()) {
Expand Down Expand Up @@ -270,7 +271,8 @@ public String getPointsToNextString(@NonNull World world, @Nullable UUID targetP
* @param size - size of the top ten
* @return sorted top ten map
*/
public Map<UUID, Long> getTopTen(World world, int size) {
@NonNull
public Map<UUID, Long> getTopTen(@NonNull World world, int size) {
topTenLists.computeIfAbsent(world, TopTenData::new);
// Remove player from top ten if they are online and do not have the perm
topTenLists.get(world).getTopTen().keySet().removeIf(u -> !hasTopTenPerm(world, u));
Expand All @@ -286,7 +288,7 @@ public Map<UUID, Long> getTopTen(World world, int size) {
* Checks if player has the correct top ten perm to have their level saved
* @param world
* @param targetPlayer
* @return
* @return true if player has the perm
*/
boolean hasTopTenPerm(@NonNull World world, @NonNull UUID targetPlayer) {
String permPrefix = addon.getPlugin().getIWM().getPermissionPrefix(world);
Expand Down
9 changes: 8 additions & 1 deletion src/test/java/world/bentobox/level/LevelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,5 +300,12 @@ public void testGetSettings() {
assertEquals(100, s.getLevelCost());
}


/**
* Test method for {@link world.bentobox.level.Level#getRankLevel(World, int)}.
*/
@Test
public void testRankLevel() {
addon.onEnable();
assertEquals("",addon.getRankLevel(world, 1));
}
}

0 comments on commit 1a7d48a

Please sign in to comment.