From 8f9aa78435c1ab8624bed90706816253af419276 Mon Sep 17 00:00:00 2001 From: BONNe1704 Date: Thu, 14 Feb 2019 11:46:37 +0200 Subject: [PATCH] Fix confusing Select Challenge GUI. #86 Left Click on challenge will immediately return to previous panel (status = true), right click will select challenge. --- .../challenges/ChallengesManager.java | 35 +++++++++++-- .../challenges/panel/admin/EditLevelGUI.java | 8 +-- .../challenges/panel/admin/ListUsersGUI.java | 20 +++---- .../panel/util/SelectChallengeGUI.java | 52 ++++++++++++++++--- 4 files changed, 86 insertions(+), 29 deletions(-) diff --git a/src/main/java/world/bentobox/challenges/ChallengesManager.java b/src/main/java/world/bentobox/challenges/ChallengesManager.java index 7be5392..c837f7b 100644 --- a/src/main/java/world/bentobox/challenges/ChallengesManager.java +++ b/src/main/java/world/bentobox/challenges/ChallengesManager.java @@ -611,12 +611,25 @@ public class ChallengesManager * @param user - user * @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.playerCacheData.get(user.getUniqueId()).setChallengeDone(challenge.getUniqueId()); + this.playerCacheData.get(user).setChallengeDone(challenge.getUniqueId()); // Save - this.savePlayer(user.getUniqueId()); + this.savePlayer(user); } @@ -627,11 +640,23 @@ public class ChallengesManager * @param 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.playerCacheData.get(user.getUniqueId()).setChallengeTimes(challenge.getUniqueId(), 0); + this.playerCacheData.get(user).setChallengeTimes(challenge.getUniqueId(), 0); // Save - this.savePlayer(user.getUniqueId()); + this.savePlayer(user); } diff --git a/src/main/java/world/bentobox/challenges/panel/admin/EditLevelGUI.java b/src/main/java/world/bentobox/challenges/panel/admin/EditLevelGUI.java index f384695..7b08e60 100644 --- a/src/main/java/world/bentobox/challenges/panel/admin/EditLevelGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/admin/EditLevelGUI.java @@ -596,10 +596,10 @@ public class EditLevelGUI extends CommonGUI () -> new LinkedHashMap<>(challengeList.size()))); // Open select gui - new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, value) -> { + new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, valueSet) -> { if (status) { - manager.addChallengeToLevel(value, this.challengeLevel); + valueSet.forEach(challenge -> manager.addChallengeToLevel(challenge, this.challengeLevel)); } this.build(); @@ -629,10 +629,10 @@ public class EditLevelGUI extends CommonGUI () -> new LinkedHashMap<>(challengeList.size()))); // Open select gui - new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, value) -> { + new SelectChallengeGUI(this.user, challengeDescriptionMap, lineLength, (status, valueSet) -> { if (status) { - manager.removeChallengeFromLevel(value, this.challengeLevel); + valueSet.forEach(challenge -> manager.removeChallengeFromLevel(challenge, this.challengeLevel)); } this.build(); diff --git a/src/main/java/world/bentobox/challenges/panel/admin/ListUsersGUI.java b/src/main/java/world/bentobox/challenges/panel/admin/ListUsersGUI.java index 49b5e49..69cf5ef 100644 --- a/src/main/java/world/bentobox/challenges/panel/admin/ListUsersGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/admin/ListUsersGUI.java @@ -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) { - manager.setChallengeComplete(User.getInstance(player), value); - } - else - { - this.build(); + valueSet.forEach(challenge -> manager.setChallengeComplete(player.getUniqueId(), challenge)); } + + this.build(); }); break; 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) { - manager.resetChallenge(User.getInstance(player), value); - } - else - { - this.build(); + valueSet.forEach(challenge -> manager.resetChallenge(player.getUniqueId(), challenge)); } + + this.build(); }); break; case RESET_ALL: diff --git a/src/main/java/world/bentobox/challenges/panel/util/SelectChallengeGUI.java b/src/main/java/world/bentobox/challenges/panel/util/SelectChallengeGUI.java index 3e1bad3..93fb352 100644 --- a/src/main/java/world/bentobox/challenges/panel/util/SelectChallengeGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/util/SelectChallengeGUI.java @@ -2,9 +2,8 @@ package world.bentobox.challenges.panel.util; import org.bukkit.Material; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import org.bukkit.event.inventory.ClickType; +import java.util.*; import java.util.function.BiConsumer; import world.bentobox.bentobox.api.panels.PanelItem; @@ -20,13 +19,14 @@ import world.bentobox.challenges.utils.GuiUtils; */ public class SelectChallengeGUI { - public SelectChallengeGUI(User user, Map> challengesDescriptionMap, int lineLength, BiConsumer consumer) + public SelectChallengeGUI(User user, Map> challengesDescriptionMap, int lineLength, BiConsumer> consumer) { this.consumer = consumer; this.user = user; this.challengesList = new ArrayList<>(challengesDescriptionMap.keySet()); this.challengesDescriptionMap = challengesDescriptionMap; this.lineLength = lineLength; + this.selectedChallenges = new HashSet<>(this.challengesList.size()); this.build(0); } @@ -129,14 +129,45 @@ public class SelectChallengeGUI */ private PanelItem createChallengeButton(Challenge challenge) { + List 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(). name(challenge.getFriendlyName()). - description(GuiUtils.stringSplit(this.challengesDescriptionMap.get(challenge), this.lineLength)). + description(GuiUtils.stringSplit(description, this.lineLength)). icon(challenge.getIcon()). 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; - }).build(); + }). + glow(this.selectedChallenges.contains(challenge)). + build(); } @@ -148,7 +179,7 @@ public class SelectChallengeGUI /** * This variable stores consumer. */ - private BiConsumer consumer; + private BiConsumer> consumer; /** * User who runs GUI. @@ -160,6 +191,11 @@ public class SelectChallengeGUI */ private List challengesList; + /** + * Selected challenges that will be returned to consumer. + */ + private Set selectedChallenges; + /** * Map that contains all challenge descriptions */