Add option to quit from conversation by writing "cancel" in chat.

Move sanitizeInput to a GuiUtil class.
This commit is contained in:
BONNe 2020-04-22 01:12:46 +03:00
parent 3fd0aa7219
commit 5a0d63a272
7 changed files with 200 additions and 135 deletions

View File

@ -1082,6 +1082,10 @@ public abstract class CommonGUI
}
}).
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());

View File

@ -7,11 +7,7 @@ import java.util.function.Function;
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;
@ -252,20 +248,27 @@ public class AdminGUI extends CommonGUI
icon = new ItemStack(Material.BOOK);
clickHandler = (panel, user, clickType, slot) -> {
this.getNewUniqueID(challenge -> {
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,
(type, requirements) -> new EditChallengeGUI(this.addon,
this.world,
this.user,
this.addon.getChallengesManager().createChallenge(uniqueId, type, requirements),
this.topLabel,
this.permissionPrefix,
this).build();
});
this).build());
}
},
input -> {
String uniqueId = Utils.getGameMode(this.world) + "_" + input;
@ -287,7 +290,15 @@ public class AdminGUI extends CommonGUI
icon = new ItemStack(Material.BOOK);
clickHandler = (panel, user, clickType, slot) -> {
this.getNewUniqueID(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,
@ -297,6 +308,7 @@ public class AdminGUI extends CommonGUI
this.topLabel,
this.permissionPrefix,
this).build();
}
},
input -> {
String newName = Utils.getGameMode(this.world) + "_" + input;
@ -660,7 +672,7 @@ public class AdminGUI extends CommonGUI
@Override
protected boolean isInputValid(ConversationContext context, String input)
{
return stringValidation.apply(sanitizeInput(input));
return stringValidation.apply(GuiUtils.sanitizeInput(input));
}
@ -680,7 +692,7 @@ public class AdminGUI extends CommonGUI
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));
}
@ -700,25 +712,19 @@ public class AdminGUI extends CommonGUI
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("-", "_");
}
}

View File

@ -500,7 +500,11 @@ public class EditChallengeGUI extends CommonGUI
clickHandler = (panel, user, clickType, slot) -> {
this.getFriendlyName(reply -> {
if (reply != null)
{
this.challenge.setFriendlyName(reply);
}
this.build();
},
this.user.getTranslation("challenges.gui.questions.admin.challenge-name"),

View File

@ -338,8 +338,12 @@ public class EditLevelGUI extends CommonGUI
icon = new ItemStack(Material.DROPPER);
clickHandler = (panel, user, clickType, slot) -> {
this.getFriendlyName(reply -> {
this.getFriendlyName(reply ->
{
if (reply != null)
{
this.challengeLevel.setFriendlyName(reply);
}
this.build();
},
this.user.getTranslation("challenges.gui.questions.admin.level-name"),

View File

@ -148,7 +148,12 @@ public class NumberGUI
clickHandler = (panel, user, clickType, slot) -> {
this.getNumberInput(number -> {
if (number != null)
{
// Null value is passed if user write cancel.
this.value = number.intValue();
}
this.build();
},
this.user.getTranslation("challenges.gui.questions.admin.number"));
@ -374,8 +379,8 @@ public class NumberGUI
new NumericPrompt()
{
/**
* Override this method to perform some action
* with the user's integer response.
* Override this method to perform some action with
* the user's integer response.
*
* @param context Context information about the
* conversation.
@ -397,10 +402,12 @@ public class NumberGUI
/**
* Override this method to do further validation on the numeric player
* input after the input has been determined to actually be a number.
* Override this method to do further validation on
* the numeric player input after the input has been
* determined to actually be a number.
*
* @param context Context information about the conversation.
* @param context Context information about the
* conversation.
* @param input The number the player provided.
* @return The validity of the player's input.
*/
@ -413,28 +420,37 @@ public class NumberGUI
/**
* Optionally override this method to display an additional message if the
* user enters an invalid number.
* Optionally override this method to display an
* additional message if the user enters an invalid
* number.
*
* @param context Context information about the conversation.
* @param invalidInput The invalid input provided by the user.
* @return A message explaining how to correct the input.
* @param context Context information about the
* conversation.
* @param invalidInput The invalid input provided by
* the user.
* @return A message explaining how to correct the
* input.
*/
@Override
protected String getInputNotNumericText(ConversationContext context,
String invalidInput)
{
return NumberGUI.this.user.getTranslation("challenges.errors.not-a-integer", "[value]", invalidInput);
return NumberGUI.this.user
.getTranslation("challenges.errors.not-a-integer", "[value]", invalidInput);
}
/**
* Optionally override this method to display an additional message if the
* user enters an invalid numeric input.
* Optionally override this method to display an
* additional message if the user enters an invalid
* numeric input.
*
* @param context Context information about the conversation.
* @param invalidInput The invalid input provided by the user.
* @return A message explaining how to correct the input.
* @param context Context information about the
* conversation.
* @param invalidInput The invalid input provided by
* the user.
* @return A message explaining how to correct the
* input.
*/
@Override
protected String getFailedValidationText(ConversationContext context,
@ -461,6 +477,10 @@ public class NumberGUI
}
}).
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 ->
NumberGUI.this.user.getTranslation("challenges.gui.questions.prefix")).
buildConversation(user.getPlayer());

View File

@ -156,7 +156,15 @@ public class StringListGUI
icon = new ItemStack(Material.WHITE_STAINED_GLASS_PANE);
clickHandler = (panel, user, clickType, slot) -> {
this.getStringInput(value -> this.value.add(value),
this.getStringInput(value -> {
if (value != null)
{
this.value.add(value);
}
// Reopen GUI.
this.build();
},
this.user.getTranslation("challenges.gui.descriptions.admin.add-text-line"));
return true;
@ -215,7 +223,15 @@ public class StringListGUI
clickHandler((panel, user1, clickType, i) -> {
this.getStringInput(
value -> this.value.set(stringIndex, value),
value -> {
if (value != null)
{
this.value.set(stringIndex, value);
}
// Reopen GUI
this.build();
},
this.user.getTranslation("challenges.gui.descriptions.admin.edit-text-line"),
element);
@ -282,12 +298,14 @@ public class StringListGUI
{
// Add answer to consumer.
consumer.accept(answer);
// Reopen GUI
StringListGUI.this.build();
// 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());

View File

@ -1,10 +1,7 @@
package world.bentobox.challenges.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.*;
import org.apache.commons.lang.WordUtils;
import org.bukkit.ChatColor;
@ -424,4 +421,16 @@ public class GuiUtils
stringList.stream().map(string -> GuiUtils.stringSplit(string, warpLength)).forEach(newList::addAll);
return newList;
}
/**
* Sanitizes the provided input.
* It replaces spaces and hyphens with underscores and lower cases the input.
* @param input input to sanitize
* @return sanitized input
*/
public static String sanitizeInput(String input)
{
return input.toLowerCase(Locale.ENGLISH).replace(" ", "_").replace("-", "_");
}
}