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 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);
}

View File

@ -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();

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)
{
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:

View File

@ -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<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);
}
@ -129,14 +129,45 @@ public class SelectChallengeGUI
*/
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();
}
@ -148,7 +179,7 @@ public class SelectChallengeGUI
/**
* This variable stores consumer.
*/
private BiConsumer<Boolean, Challenge> consumer;
private BiConsumer<Boolean, Set<Challenge>> consumer;
/**
* User who runs GUI.
@ -160,6 +191,11 @@ public class SelectChallengeGUI
*/
private List<Challenge> challengesList;
/**
* Selected challenges that will be returned to consumer.
*/
private Set<Challenge> selectedChallenges;
/**
* Map that contains all challenge descriptions
*/