Added a uniqueId sanitization when creating challenges/levels

This will help fixing issues with spaces, hyphens and accents in non-English languages.
This commit is contained in:
Florian CUNY 2020-02-08 22:28:57 +01:00
parent 0ae84ec850
commit db971d81ab
2 changed files with 19 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package world.bentobox.challenges.panel.admin;
import java.util.Locale;
import java.util.function.Consumer;
import java.util.function.Function;
@ -252,7 +253,7 @@ public class AdminGUI extends CommonGUI
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(),
@ -260,15 +261,15 @@ public class AdminGUI extends CommonGUI
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")
);
@ -659,7 +660,7 @@ public class AdminGUI extends CommonGUI
@Override
protected boolean isInputValid(ConversationContext context, String input)
{
return stringValidation.apply(input);
return stringValidation.apply(sanitizeInput(input));
}
@ -679,7 +680,7 @@ public class AdminGUI extends CommonGUI
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));
}
@ -699,7 +700,7 @@ public class AdminGUI extends CommonGUI
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;
}
@ -710,4 +711,14 @@ public class AdminGUI extends CommonGUI
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

@ -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."