Skip to content

Commit

Permalink
Added a uniqueId sanitization when creating challenges/levels
Browse files Browse the repository at this point in the history
This will help fixing issues with spaces, hyphens and accents in non-English languages.
  • Loading branch information
Poslovitch committed Feb 8, 2020
1 parent 0ae84ec commit db971d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
25 changes: 18 additions & 7 deletions src/main/java/world/bentobox/challenges/panel/admin/AdminGUI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package world.bentobox.challenges.panel.admin;


import java.util.Locale;
import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -252,23 +253,23 @@ private PanelItem createButton(Button button)
clickHandler = (panel, user, clickType, slot) -> {

this.getNewUniqueID(challenge -> {
String newName = Utils.getGameMode(this.world) + "_" + 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(newName, type, requirements),
this.addon.getChallengesManager().createChallenge(uniqueId, type, requirements),
this.topLabel,
this.permissionPrefix,
this).build();
});
},
input -> {
String newName = Utils.getGameMode(this.world) + "_" + input;
return !this.addon.getChallengesManager().containsChallenge(newName);
String uniqueId = Utils.getGameMode(this.world) + "_" + input;
return !this.addon.getChallengesManager().containsChallenge(uniqueId);
},
this.user.getTranslation("challenges.gui.questions.admin.unique-id")
);
Expand Down Expand Up @@ -659,7 +660,7 @@ public String getPromptText(ConversationContext context)
@Override
protected boolean isInputValid(ConversationContext context, String input)
{
return stringValidation.apply(input);
return stringValidation.apply(sanitizeInput(input));
}


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


Expand All @@ -699,7 +700,7 @@ protected String getFailedValidationText(ConversationContext context,
protected Prompt acceptValidatedInput(ConversationContext context, String input)
{
// Add answer to consumer.
consumer.accept(input);
consumer.accept(sanitizeInput(input));
// End conversation
return Prompt.END_OF_CONVERSATION;
}
Expand All @@ -710,4 +711,14 @@ protected Prompt acceptValidatedInput(ConversationContext context, String input)

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("-", "_");
}
}
2 changes: 1 addition & 1 deletion src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ challenges:

admin:
number: "Write a number in the chat and press enter."
unique-id: "Write the object's unique name and press enter."
unique-id: "Write the object's unique id and press enter."
challenge-name: "Write the display name in the chat for the current challenge."
level-name: "Write the display name in chat for the current level."

Expand Down

0 comments on commit db971d8

Please sign in to comment.