Skip to content

Commit

Permalink
Add option to quit from conversation by writing "cancel" in chat.
Browse files Browse the repository at this point in the history
Move sanitizeInput to a GuiUtil class.
  • Loading branch information
BONNe committed Apr 21, 2020
1 parent 3fd0aa7 commit 5a0d63a
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 145 deletions.
4 changes: 4 additions & 0 deletions src/main/java/world/bentobox/challenges/panel/CommonGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,10 @@ public Prompt acceptInput(ConversationContext conversationContext, String answer
}
}).
withLocalEcho(false).
// On cancel conversation will be closed.
withEscapeSequence("cancel").
// Use null value in consumer to detect if user has abandoned conversation.
addConversationAbandonedListener(abandonedEvent -> consumer.accept(null)).
withPrefix(context -> user.getTranslation("challenges.gui.questions.prefix")).
buildConversation(user.getPlayer());

Expand Down
88 changes: 47 additions & 41 deletions src/main/java/world/bentobox/challenges/panel/admin/AdminGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@

import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.conversations.Conversation;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.ConversationFactory;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.ValidatingPrompt;
import org.bukkit.conversations.*;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.NonNull;

Expand Down Expand Up @@ -252,20 +248,27 @@ private PanelItem createButton(Button button)
icon = new ItemStack(Material.BOOK);
clickHandler = (panel, user, clickType, slot) -> {

this.getNewUniqueID(challenge -> {
String uniqueId = Utils.getGameMode(this.world) + "_" + challenge;

ChallengeTypeGUI.open(user,
this.addon.getChallengesSettings().getLoreLineLength(),
(type, requirements) -> {
new EditChallengeGUI(this.addon,
this.world,
this.user,
this.addon.getChallengesManager().createChallenge(uniqueId, type, requirements),
this.topLabel,
this.permissionPrefix,
this).build();
});
this.getNewUniqueID(challenge ->
{
if (challenge == null)
{
// Build Admin Gui if input is null.
this.build();
}
else
{
String uniqueId = Utils.getGameMode(this.world) + "_" + challenge;

ChallengeTypeGUI.open(user,
this.addon.getChallengesSettings().getLoreLineLength(),
(type, requirements) -> new EditChallengeGUI(this.addon,
this.world,
this.user,
this.addon.getChallengesManager().createChallenge(uniqueId, type, requirements),
this.topLabel,
this.permissionPrefix,
this).build());
}
},
input -> {
String uniqueId = Utils.getGameMode(this.world) + "_" + input;
Expand All @@ -287,16 +290,25 @@ private PanelItem createButton(Button button)
icon = new ItemStack(Material.BOOK);
clickHandler = (panel, user, clickType, slot) -> {

this.getNewUniqueID(level -> {
String newName = Utils.getGameMode(this.world) + "_" + level;
this.getNewUniqueID(level ->
{
if (level == null)
{
// Build Admin Gui if input is null.
this.build();
}
else
{
String newName = Utils.getGameMode(this.world) + "_" + level;

new EditLevelGUI(this.addon,
this.world,
this.user,
this.addon.getChallengesManager().createLevel(newName, this.world),
this.topLabel,
this.permissionPrefix,
this).build();
new EditLevelGUI(this.addon,
this.world,
this.user,
this.addon.getChallengesManager().createLevel(newName, this.world),
this.topLabel,
this.permissionPrefix,
this).build();
}
},
input -> {
String newName = Utils.getGameMode(this.world) + "_" + input;
Expand Down Expand Up @@ -660,7 +672,7 @@ public String getPromptText(ConversationContext context)
@Override
protected boolean isInputValid(ConversationContext context, String input)
{
return stringValidation.apply(sanitizeInput(input));
return stringValidation.apply(GuiUtils.sanitizeInput(input));
}


Expand All @@ -680,7 +692,7 @@ protected boolean isInputValid(ConversationContext context, String input)
protected String getFailedValidationText(ConversationContext context,
String invalidInput)
{
return user.getTranslation("challenges.errors.unique-id", "[id]", sanitizeInput(invalidInput));
return user.getTranslation("challenges.errors.unique-id", "[id]", GuiUtils.sanitizeInput(invalidInput));
}


Expand All @@ -700,25 +712,19 @@ protected String getFailedValidationText(ConversationContext context,
protected Prompt acceptValidatedInput(ConversationContext context, String input)
{
// Add answer to consumer.
consumer.accept(sanitizeInput(input));
consumer.accept(GuiUtils.sanitizeInput(input));
// End conversation
return Prompt.END_OF_CONVERSATION;
}
}).
// On cancel conversation will be closed.
withEscapeSequence("cancel").
// Use null value in consumer to detect if user has abandoned conversation.
addConversationAbandonedListener(abandonedEvent -> consumer.accept(null)).
withLocalEcho(false).
withPrefix(context -> user.getTranslation("challenges.gui.questions.prefix")).
buildConversation(user.getPlayer());

conversation.begin();
}

/**
* Sanitizes the provided input.
* It replaces spaces and hyphens with underscores and lowercases the input.
* @param input input to sanitize
* @return sanitized input
*/
private String sanitizeInput(String input) {
return input.toLowerCase(Locale.ENGLISH).replace(" ", "_").replace("-", "_");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,11 @@ private PanelItem createButton(Button button)
clickHandler = (panel, user, clickType, slot) -> {

this.getFriendlyName(reply -> {
this.challenge.setFriendlyName(reply);
if (reply != null)
{
this.challenge.setFriendlyName(reply);
}

this.build();
},
this.user.getTranslation("challenges.gui.questions.admin.challenge-name"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,13 @@ private PanelItem createButton(Button button)
icon = new ItemStack(Material.DROPPER);
clickHandler = (panel, user, clickType, slot) -> {

this.getFriendlyName(reply -> {
this.challengeLevel.setFriendlyName(reply);
this.build();
this.getFriendlyName(reply ->
{
if (reply != null)
{
this.challengeLevel.setFriendlyName(reply);
}
this.build();
},
this.user.getTranslation("challenges.gui.questions.admin.level-name"),
this.challengeLevel.getFriendlyName()
Expand Down

0 comments on commit 5a0d63a

Please sign in to comment.