Skip to content

Commit

Permalink
Fix confusing Select Challenge GUI. #86
Browse files Browse the repository at this point in the history
Left Click on challenge will immediately return to previous panel (status = true), right click will select challenge.
  • Loading branch information
BONNe committed Feb 14, 2019
1 parent 14660dd commit 8f9aa78
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 29 deletions.
35 changes: 30 additions & 5 deletions src/main/java/world/bentobox/challenges/ChallengesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,25 @@ public boolean isLevelUnlocked(UUID user, ChallengeLevel level)
* @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);
}


Expand All @@ -627,11 +640,23 @@ public void setChallengeComplete(User user, Challenge challenge)
* @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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,10 @@ private PanelItem createButton(Button button)
() -> 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();
Expand Down Expand Up @@ -629,10 +629,10 @@ private PanelItem createButton(Button button)
() -> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,13 @@ private PanelItem createPlayerIcon(Player player)
}
}

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:
Expand All @@ -218,15 +216,13 @@ private PanelItem createPlayerIcon(Player player)
}
}

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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@


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;
Expand All @@ -20,13 +19,14 @@
*/
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.user = user;
this.challengesList = new ArrayList<>(challengesDescriptionMap.keySet());
this.challengesDescriptionMap = challengesDescriptionMap;
this.lineLength = lineLength;
this.selectedChallenges = new HashSet<>(this.challengesList.size());

this.build(0);
}
Expand Down Expand Up @@ -129,14 +129,45 @@ else if (pageIndex > (this.challengesList.size() / MAX_ELEMENTS))
*/
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().
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();
}


Expand All @@ -148,7 +179,7 @@ private PanelItem createChallengeButton(Challenge challenge)
/**
* This variable stores consumer.
*/
private BiConsumer<Boolean, Challenge> consumer;
private BiConsumer<Boolean, Set<Challenge>> consumer;

/**
* User who runs GUI.
Expand All @@ -160,6 +191,11 @@ private PanelItem createChallengeButton(Challenge challenge)
*/
private List<Challenge> challengesList;

/**
* Selected challenges that will be returned to consumer.
*/
private Set<Challenge> selectedChallenges;

/**
* Map that contains all challenge descriptions
*/
Expand Down

0 comments on commit 8f9aa78

Please sign in to comment.