Implement Challenge Description into SelectChallengeGUI.

Fix issue, when reset and complete user challenges showed all challenges, instead of mission one.
This commit is contained in:
BONNe 2019-02-12 13:03:13 +02:00
parent 7778426b1e
commit 0317d057ab
7 changed files with 81 additions and 18 deletions

View File

@ -482,9 +482,22 @@ public class ChallengesManager
* @return - true if completed
*/
public boolean isChallengeComplete(User user, Challenge challenge)
{
return this.isChallengeComplete(user.getUniqueId(), challenge);
}
/**
* Checks if a challenge is complete or not
*
* @param user - User who must be checked.
* @param challenge - Challenge
* @return - true if completed
*/
public boolean isChallengeComplete(UUID user, Challenge challenge)
{
this.addPlayer(user);
return this.playerCacheData.get(user.getUniqueId()).isChallengeDone(challenge.getUniqueId());
return this.playerCacheData.get(user).isChallengeDone(challenge.getUniqueId());
}

View File

@ -5,6 +5,7 @@ import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Collections;
@ -255,14 +256,14 @@ public abstract class CommonGUI
* @param challenge Challenge which description must be generated.
* @return List of strings that will be used in challenges description.
*/
protected List<String> generateChallengeDescription(Challenge challenge, User user)
protected List<String> generateChallengeDescription(Challenge challenge, Player user)
{
List<String> result = new ArrayList<>();
// Some values to avoid overchecking.
ChallengesManager manager = this.addon.getChallengesManager();
final boolean isCompletedOnce = manager.isChallengeComplete(user, challenge);
final boolean isCompletedOnce = manager.isChallengeComplete(user.getUniqueId(), challenge);
final long doneTimes = challenge.isRepeatable() ?
manager.getChallengeTimes(this.user, challenge) :
isCompletedOnce ? 0 : 1;
@ -490,7 +491,7 @@ public abstract class CommonGUI
* @param user Target user for command string.
* @return List of strings that contains message from challenges.
*/
private List<String> getChallengeRewardItems(Challenge challenge, boolean isCompletedOnce, User user)
private List<String> getChallengeRewardItems(Challenge challenge, boolean isCompletedOnce, Player user)
{
List<String> result = new ArrayList<>();

View File

@ -5,10 +5,8 @@ import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import net.wesjd.anvilgui.AnvilGUI;
import world.bentobox.bentobox.api.panels.PanelItem;
@ -586,11 +584,19 @@ public class EditLevelGUI extends CommonGUI
clickHandler = (panel, user, clickType, slot) -> {
ChallengesManager manager = this.addon.getChallengesManager();
// Get all challenge that is not in current challenge.
// Get all challenge that is not in current level.
List<Challenge> challengeList = manager.getAllChallenges(this.world);
challengeList.removeAll(manager.getLevelChallenges(this.challengeLevel));
new SelectChallengeGUI(this.user, challengeList, lineLength, (status, value) -> {
// Generate descriptions for these challenges
Map<Challenge, List<String>> challengeDescriptionMap = challengeList.stream().
collect(Collectors.toMap(challenge -> challenge,
challenge -> this.generateChallengeDescription(challenge, this.user.getPlayer()),
(a, b) -> b,
() -> new LinkedHashMap<>(challengeList.size())));
// Open select gui
new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, value) -> {
if (status)
{
manager.addChallengeToLevel(value, this.challengeLevel);
@ -612,7 +618,18 @@ public class EditLevelGUI extends CommonGUI
clickHandler = (panel, user, clickType, slot) -> {
ChallengesManager manager = this.addon.getChallengesManager();
new SelectChallengeGUI(this.user, manager.getLevelChallenges(this.challengeLevel), lineLength, (status, value) -> {
// Get all challenge that is in current level.
List<Challenge> challengeList = manager.getLevelChallenges(this.challengeLevel);
// Generate descriptions for these challenges
Map<Challenge, List<String>> challengeDescriptionMap = challengeList.stream().
collect(Collectors.toMap(challenge -> challenge,
challenge -> this.generateChallengeDescription(challenge, this.user.getPlayer()),
(a, b) -> b,
() -> new LinkedHashMap<>(challengeList.size())));
// Open select gui
new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, value) -> {
if (status)
{
manager.removeChallengeFromLevel(value, this.challengeLevel);

View File

@ -134,7 +134,7 @@ public class ListChallengesGUI extends CommonGUI
{
PanelItemBuilder itemBuilder = new PanelItemBuilder().
name(challenge.getFriendlyName()).
description(GuiUtils.stringSplit(this.generateChallengeDescription(challenge, this.user),
description(GuiUtils.stringSplit(this.generateChallengeDescription(challenge, this.user.getPlayer()),
this.addon.getChallengesSettings().getLoreLineLength())).
icon(challenge.getIcon()).
glow(challenge.isDeployed());

View File

@ -6,7 +6,9 @@ import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import world.bentobox.bentobox.api.panels.PanelItem;
@ -16,6 +18,7 @@ import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Players;
import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.ChallengesManager;
import world.bentobox.challenges.database.object.Challenge;
import world.bentobox.challenges.panel.CommonGUI;
import world.bentobox.challenges.panel.util.ConfirmationGUI;
import world.bentobox.challenges.panel.util.SelectChallengeGUI;
@ -177,11 +180,22 @@ public class ListUsersGUI extends CommonGUI
return new PanelItemBuilder().name(player.getName()).icon(player.getName()).clickHandler(
(panel, user1, clickType, slot) -> {
ChallengesManager manager = this.addon.getChallengesManager();
Map<Challenge, List<String>> challengeDescriptionMap;
switch (this.operationMode)
{
case COMPLETE:
new SelectChallengeGUI(this.user, manager.getAllChallenges(this.world), lineLength, (status, value) -> {
challengeDescriptionMap = new LinkedHashMap<>();
for (Challenge challenge : manager.getAllChallenges(this.world))
{
if (!manager.isChallengeComplete(player.getUniqueId(), challenge))
{
challengeDescriptionMap.put(challenge, this.generateChallengeDescription(challenge, player));
}
}
new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, value) -> {
if (status)
{
manager.setChallengeComplete(User.getInstance(player), value);
@ -193,7 +207,17 @@ public class ListUsersGUI extends CommonGUI
});
break;
case RESET:
new SelectChallengeGUI(this.user, manager.getAllChallenges(this.world), lineLength, (status, value) -> {
challengeDescriptionMap = new LinkedHashMap<>();
for (Challenge challenge : manager.getAllChallenges(this.world))
{
if (manager.isChallengeComplete(player.getUniqueId(), challenge))
{
challengeDescriptionMap.put(challenge, this.generateChallengeDescription(challenge, player));
}
}
new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, value) -> {
if (status)
{
manager.resetChallenge(User.getInstance(player), value);

View File

@ -343,7 +343,7 @@ public class ChallengesGUI extends CommonGUI
return new PanelItemBuilder().
icon(challenge.getIcon()).
name(challenge.getFriendlyName().isEmpty() ? challenge.getUniqueId() : challenge.getFriendlyName()).
description(GuiUtils.stringSplit(this.generateChallengeDescription(challenge, this.user),
description(GuiUtils.stringSplit(this.generateChallengeDescription(challenge, this.user.getPlayer()),
this.addon.getChallengesSettings().getLoreLineLength())).
clickHandler((panel, user1, clickType, slot) -> {
if (TryToComplete.complete(this.addon,

View File

@ -2,7 +2,9 @@ package world.bentobox.challenges.panel.util;
import org.bukkit.Material;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import world.bentobox.bentobox.api.panels.PanelItem;
@ -18,11 +20,12 @@ import world.bentobox.challenges.utils.GuiUtils;
*/
public class SelectChallengeGUI
{
public SelectChallengeGUI(User user, List<Challenge> challengesList, int lineLength, BiConsumer<Boolean, Challenge> consumer)
public SelectChallengeGUI(User user, Map<Challenge, List<String>> challengesDescriptionMap, int lineLength, BiConsumer<Boolean, Challenge> consumer)
{
this.consumer = consumer;
this.user = user;
this.challengesList = challengesList;
this.challengesList = new ArrayList<>(challengesDescriptionMap.keySet());
this.challengesDescriptionMap = challengesDescriptionMap;
this.lineLength = lineLength;
this.build(0);
@ -128,7 +131,7 @@ public class SelectChallengeGUI
{
return new PanelItemBuilder().
name(challenge.getFriendlyName()).
description(GuiUtils.stringSplit(challenge.getDescription(), this.lineLength)).
description(GuiUtils.stringSplit(this.challengesDescriptionMap.get(challenge), this.lineLength)).
icon(challenge.getIcon()).
clickHandler((panel, user1, clickType, slot) -> {
this.consumer.accept(true, challenge);
@ -157,6 +160,11 @@ public class SelectChallengeGUI
*/
private List<Challenge> challengesList;
/**
* Map that contains all challenge descriptions
*/
private Map<Challenge, List<String>> challengesDescriptionMap;
/**
* This variable stores how large line can be, before warp it.
*/