Add 7 new placeholders for Challenges Addon.

- `[gamemode]_challenge_total_completion_count` returns number of sum of challenge completions for user.
- `[gamemode]_challenge_completed_count` returns number of completed challenges (at least once) for user.
- `[gamemode]_challenge_uncompleted_count` returns number of uncompleted challenges for user.
- `[gamemode]_challenge_completed_level_count` returns number of completed levels for user.
- `[gamemode]_challenge_uncompleted_level_count` returns number of uncompleted levels for user.
- `[gamemode]_challenge_unlocked_level_count` returns number of unlocked levels for user.
- `[gamemode]_challenge_locked_level_count` returns number of locked levels for user.

Fixes #224
This commit is contained in:
BONNe 2020-04-05 21:27:09 +03:00
parent 6368585a57
commit 2958ca8b6c
2 changed files with 139 additions and 11 deletions

View File

@ -7,6 +7,7 @@ import java.util.Optional;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.addons.GameModeAddon;
@ -156,11 +157,8 @@ public class ChallengesAddon extends Addon {
List<GameModeAddon> hookedGameModes = new ArrayList<>();
this.getPlugin().getAddonsManager().getGameModeAddons().forEach(gameModeAddon -> {
if (!this.settings
.getDisabledGameModes()
.contains(gameModeAddon
.getDescription()
.getName()))
if (!this.settings.getDisabledGameModes().contains(
gameModeAddon.getDescription().getName()))
{
if (gameModeAddon.getPlayerCommand().isPresent())
{
@ -178,6 +176,8 @@ public class ChallengesAddon extends Addon {
CHALLENGES_WORLD_PROTECTION.addGameModeAddon(gameModeAddon);
CHALLENGES_ISLAND_PROTECTION.addGameModeAddon(gameModeAddon);
this.registerPlaceholders(gameModeAddon);
}
});
@ -314,6 +314,56 @@ public class ChallengesAddon extends Addon {
}
/**
* This method registers placeholders into GameMode addon.
* @param gameModeAddon GameMode addon where placeholders must be hooked in.
*/
private void registerPlaceholders(GameModeAddon gameModeAddon)
{
final String gameMode = gameModeAddon.getDescription().getName().toLowerCase();
final World world = gameModeAddon.getOverWorld();
// Number of completions for all challenges placeholder
this.getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gameMode + "_challenge_total_completion_count",
user -> String.valueOf(this.challengesManager.getTotalChallengeCompletionCount(user, world)));
// Completed challenge count placeholder
this.getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gameMode + "_challenge_completed_count",
user -> String.valueOf(this.challengesManager.getCompletedChallengeCount(user, world)));
// Uncompleted challenge count placeholder
this.getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gameMode + "_challenge_uncompleted_count",
user -> String.valueOf(this.challengesManager.getChallengeCount(world) -
this.challengesManager.getCompletedChallengeCount(user, world)));
// Completed challenge level count placeholder
this.getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gameMode + "_challenge_completed_level_count",
user -> String.valueOf(this.challengesManager.getCompletedLevelCount(user, world)));
// Uncompleted challenge level count placeholder
this.getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gameMode + "_challenge_uncompleted_level_count",
user -> String.valueOf(this.challengesManager.getLevelCount(world) -
this.challengesManager.getCompletedLevelCount(user, world)));
// Unlocked challenge level count placeholder
this.getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gameMode + "_challenge_unlocked_level_count",
user -> String.valueOf(this.challengesManager.getLevelCount(world) -
this.challengesManager.getUnlockedLevelCount(user, world)));
// Locked challenge level count placeholder
this.getPlugin().getPlaceholdersManager().registerPlaceholder(this,
gameMode + "_challenge_locked_level_count",
user -> String.valueOf(this.challengesManager.getLevelCount(world) -
this.challengesManager.getUnlockedLevelCount(user, world)));
}
// ---------------------------------------------------------------------
// Section: Getters
// ---------------------------------------------------------------------

View File

@ -1692,6 +1692,45 @@ public class ChallengesManager
}
/**
* This method returns number of challenges in given world.
* @param world World where challenge count must be returned.
* @return Number of challenges in given world.
*/
public int getChallengeCount(World world)
{
return this.getAllChallenges(world).size();
}
/**
* This method returns number of completed challenges in given world.
* @param user User which completed challenge count must be returned.
* @param world World where challenge count must be returned.
* @return Number of completed challenges by given user in given world.
*/
public long getCompletedChallengeCount(User user, World world)
{
return this.getAllChallenges(world).stream().
filter(challenge -> this.getChallengeTimes(user, world, challenge) > 0).
count();
}
/**
* This method returns total number of all completion times for all challenges in given world.
* @param user User which total completion count must be returned.
* @param world World where challenge count must be returned.
* @return Sum of completion count for all challenges by given user in given world.
*/
public long getTotalChallengeCompletionCount(User user, World world)
{
return this.getAllChallenges(world).stream().
mapToLong(challenge -> this.getChallengeTimes(user, world, challenge)).
sum();
}
// ---------------------------------------------------------------------
// Section: Level related methods
// ---------------------------------------------------------------------
@ -1962,4 +2001,43 @@ public class ChallengesManager
this.levelDatabase.loadObjects().stream().anyMatch(
level -> level.matchGameMode(gameMode));
}
/**
* This method returns number of levels in given world.
* @param world World where level count must be returned.
* @return Number of levels in given world.
*/
public int getLevelCount(World world)
{
return this.getLevels(world).size();
}
/**
* This method returns number of completed levels in given world.
* @param user User which completed level count must be returned.
* @param world World where level count must be returned.
* @return Number of completed levels by given user in given world.
*/
public long getCompletedLevelCount(User user, World world)
{
return this.getAllChallengeLevelStatus(user, world).stream().
filter(LevelStatus::isComplete).
count();
}
/**
* This method returns number of unlocked levels in given world.
* @param user User which unlocked level count must be returned.
* @param world World where level count must be returned.
* @return Number of unlocked levels by given user in given world.
*/
public long getUnlockedLevelCount(User user, World world)
{
return this.getAllChallengeLevelStatus(user, world).stream().
filter(LevelStatus::isUnlocked).
count();
}
}