Fix confusing Select Challenge GUI. #86

Left Click on challenge will immediately return to previous panel (status = true), right click will select challenge.
This commit is contained in:
BONNe1704 2019-02-14 11:46:37 +02:00
parent 14660dd42c
commit 8f9aa78435
4 changed files with 86 additions and 29 deletions

View File

@ -611,12 +611,25 @@ public class ChallengesManager
* @param user - user * @param user - user
* @param challenge - challenge * @param challenge - challenge
*/ */
public void setChallengeComplete(User user, Challenge challenge) public void setChallengeComplete(@NonNull User user, @NonNull Challenge challenge)
{
this.setChallengeComplete(user.getUniqueId(), challenge);
}
/**
* Sets the challenge as complete and increments the number of times it has been
* completed
*
* @param user - user
* @param challenge - challenge
*/
public void setChallengeComplete(@NonNull UUID user, @NonNull Challenge challenge)
{ {
this.addPlayer(user); this.addPlayer(user);
this.playerCacheData.get(user.getUniqueId()).setChallengeDone(challenge.getUniqueId()); this.playerCacheData.get(user).setChallengeDone(challenge.getUniqueId());
// Save // Save
this.savePlayer(user.getUniqueId()); this.savePlayer(user);
} }
@ -627,11 +640,23 @@ public class ChallengesManager
* @param challenge - challenge * @param challenge - challenge
*/ */
public void resetChallenge(@NonNull User user, @NonNull Challenge challenge) public void resetChallenge(@NonNull User user, @NonNull Challenge challenge)
{
this.resetChallenge(user.getUniqueId(), challenge);
}
/**
* Reset the challenge to zero time / not done
*
* @param user - user
* @param challenge - challenge
*/
public void resetChallenge(@NonNull UUID user, @NonNull Challenge challenge)
{ {
this.addPlayer(user); this.addPlayer(user);
this.playerCacheData.get(user.getUniqueId()).setChallengeTimes(challenge.getUniqueId(), 0); this.playerCacheData.get(user).setChallengeTimes(challenge.getUniqueId(), 0);
// Save // Save
this.savePlayer(user.getUniqueId()); this.savePlayer(user);
} }

View File

@ -596,10 +596,10 @@ public class EditLevelGUI extends CommonGUI
() -> new LinkedHashMap<>(challengeList.size()))); () -> new LinkedHashMap<>(challengeList.size())));
// Open select gui // Open select gui
new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, value) -> { new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, valueSet) -> {
if (status) if (status)
{ {
manager.addChallengeToLevel(value, this.challengeLevel); valueSet.forEach(challenge -> manager.addChallengeToLevel(challenge, this.challengeLevel));
} }
this.build(); this.build();
@ -629,10 +629,10 @@ public class EditLevelGUI extends CommonGUI
() -> new LinkedHashMap<>(challengeList.size()))); () -> new LinkedHashMap<>(challengeList.size())));
// Open select gui // Open select gui
new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, value) -> { new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, valueSet) -> {
if (status) if (status)
{ {
manager.removeChallengeFromLevel(value, this.challengeLevel); valueSet.forEach(challenge -> manager.removeChallengeFromLevel(challenge, this.challengeLevel));
} }
this.build(); this.build();

View File

@ -196,15 +196,13 @@ public class ListUsersGUI extends CommonGUI
} }
} }
new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, value) -> { new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, valueSet) -> {
if (status) if (status)
{ {
manager.setChallengeComplete(User.getInstance(player), value); valueSet.forEach(challenge -> manager.setChallengeComplete(player.getUniqueId(), challenge));
}
else
{
this.build();
} }
this.build();
}); });
break; break;
case RESET: case RESET:
@ -218,15 +216,13 @@ public class ListUsersGUI extends CommonGUI
} }
} }
new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, value) -> { new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, valueSet) -> {
if (status) if (status)
{ {
manager.resetChallenge(User.getInstance(player), value); valueSet.forEach(challenge -> manager.resetChallenge(player.getUniqueId(), challenge));
}
else
{
this.build();
} }
this.build();
}); });
break; break;
case RESET_ALL: case RESET_ALL:

View File

@ -2,9 +2,8 @@ package world.bentobox.challenges.panel.util;
import org.bukkit.Material; import org.bukkit.Material;
import java.util.ArrayList; import org.bukkit.event.inventory.ClickType;
import java.util.List; import java.util.*;
import java.util.Map;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.PanelItem;
@ -20,13 +19,14 @@ import world.bentobox.challenges.utils.GuiUtils;
*/ */
public class SelectChallengeGUI public class SelectChallengeGUI
{ {
public SelectChallengeGUI(User user, Map<Challenge, List<String>> challengesDescriptionMap, int lineLength, BiConsumer<Boolean, Challenge> consumer) public SelectChallengeGUI(User user, Map<Challenge, List<String>> challengesDescriptionMap, int lineLength, BiConsumer<Boolean, Set<Challenge>> consumer)
{ {
this.consumer = consumer; this.consumer = consumer;
this.user = user; this.user = user;
this.challengesList = new ArrayList<>(challengesDescriptionMap.keySet()); this.challengesList = new ArrayList<>(challengesDescriptionMap.keySet());
this.challengesDescriptionMap = challengesDescriptionMap; this.challengesDescriptionMap = challengesDescriptionMap;
this.lineLength = lineLength; this.lineLength = lineLength;
this.selectedChallenges = new HashSet<>(this.challengesList.size());
this.build(0); this.build(0);
} }
@ -129,14 +129,45 @@ public class SelectChallengeGUI
*/ */
private PanelItem createChallengeButton(Challenge challenge) private PanelItem createChallengeButton(Challenge challenge)
{ {
List<String> description;
if (this.selectedChallenges.contains(challenge))
{
description = new ArrayList<>();
description.add(this.user.getTranslation("challenges.gui.descriptions.admin.selected"));
description.addAll(this.challengesDescriptionMap.get(challenge));
}
else
{
description = this.challengesDescriptionMap.get(challenge);
}
return new PanelItemBuilder(). return new PanelItemBuilder().
name(challenge.getFriendlyName()). name(challenge.getFriendlyName()).
description(GuiUtils.stringSplit(this.challengesDescriptionMap.get(challenge), this.lineLength)). description(GuiUtils.stringSplit(description, this.lineLength)).
icon(challenge.getIcon()). icon(challenge.getIcon()).
clickHandler((panel, user1, clickType, slot) -> { clickHandler((panel, user1, clickType, slot) -> {
this.consumer.accept(true, challenge); if (clickType == ClickType.RIGHT)
{
// If challenge is not selected, then select :)
if (!this.selectedChallenges.remove(challenge))
{
this.selectedChallenges.add(challenge);
}
// Reset button.
panel.getInventory().setItem(slot, this.createChallengeButton(challenge).getItem());
}
else
{
this.consumer.accept(true, this.selectedChallenges);
}
return true; return true;
}).build(); }).
glow(this.selectedChallenges.contains(challenge)).
build();
} }
@ -148,7 +179,7 @@ public class SelectChallengeGUI
/** /**
* This variable stores consumer. * This variable stores consumer.
*/ */
private BiConsumer<Boolean, Challenge> consumer; private BiConsumer<Boolean, Set<Challenge>> consumer;
/** /**
* User who runs GUI. * User who runs GUI.
@ -160,6 +191,11 @@ public class SelectChallengeGUI
*/ */
private List<Challenge> challengesList; private List<Challenge> challengesList;
/**
* Selected challenges that will be returned to consumer.
*/
private Set<Challenge> selectedChallenges;
/** /**
* Map that contains all challenge descriptions * Map that contains all challenge descriptions
*/ */