Skip to content

Commit

Permalink
Add better challenge sorting in AdminGUI's
Browse files Browse the repository at this point in the history
Now challenges will be sorted by their level order number, their order number, their unique id. All free challenges will always be at the start.
  • Loading branch information
BONNe authored and BuildTools committed Sep 4, 2019
1 parent f2a4ab5 commit 40e34e7
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/main/java/world/bentobox/challenges/ChallengesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,45 @@ public class ChallengesManager
public static final String FREE = "";


// ---------------------------------------------------------------------
// Section: Comparators
// ---------------------------------------------------------------------


/**
* This comparator orders challenges by their level, order and name.
*/
private final Comparator<Challenge> challengeComparator = (o1, o2) -> {
if (o1.getLevel().equals(o2.getLevel()))
{
if (o1.getOrder() == o2.getOrder())
{
// If orders are equal, sort by unique Id
return o1.getUniqueId().compareToIgnoreCase(o2.getUniqueId());
}
else
{
// If levels are equal, sort them by order numbers.
return Integer.compare(o1.getOrder(), o2.getOrder());
}
}
else
{
if (o1.getLevel().isEmpty() || o2.getLevel().isEmpty())
{
// If exist free level challenge, then it should be at the start.
return Boolean.compare(o2.getLevel().isEmpty(), o1.getLevel().isEmpty());
}
else
{
// Sort by challenges level order numbers
return Integer.compare(this.getLevel(o1.getLevel()).getOrder(),
this.getLevel(o2.getLevel()).getOrder());
}
}
};


// ---------------------------------------------------------------------
// Section: Constructor
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -1385,7 +1424,7 @@ public List<String> getAllChallengesNames(@NonNull World world)
{
return this.islandWorldManager.getAddon(world).map(gameMode ->
this.challengeCacheData.values().stream().
sorted(Comparator.comparing(Challenge::getOrder)).
sorted(this.challengeComparator).
filter(challenge -> challenge.matchGameMode(gameMode.getDescription().getName())).
map(Challenge::getUniqueId).
collect(Collectors.toList())).
Expand All @@ -1403,7 +1442,7 @@ public List<Challenge> getAllChallenges(@NonNull World world)
{
return this.islandWorldManager.getAddon(world).map(gameMode ->
this.challengeCacheData.values().stream().
sorted(Comparator.comparing(Challenge::getOrder)).
sorted(this.challengeComparator).
filter(challenge -> challenge.matchGameMode(gameMode.getDescription().getName())).
collect(Collectors.toList())).
orElse(Collections.emptyList());
Expand Down

0 comments on commit 40e34e7

Please sign in to comment.