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; package world.bentobox.challenges.panel.admin;
import java.util.Locale;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
@ -252,7 +253,7 @@ public class AdminGUI extends CommonGUI
clickHandler = (panel, user, clickType, slot) -> { clickHandler = (panel, user, clickType, slot) -> {
this.getNewUniqueID(challenge -> { this.getNewUniqueID(challenge -> {
String newName = Utils.getGameMode(this.world) + "_" + challenge; String uniqueId = Utils.getGameMode(this.world) + "_" + challenge;
ChallengeTypeGUI.open(user, ChallengeTypeGUI.open(user,
this.addon.getChallengesSettings().getLoreLineLength(), this.addon.getChallengesSettings().getLoreLineLength(),
@ -260,15 +261,15 @@ public class AdminGUI extends CommonGUI
new EditChallengeGUI(this.addon, new EditChallengeGUI(this.addon,
this.world, this.world,
this.user, this.user,
this.addon.getChallengesManager().createChallenge(newName, type, requirements), this.addon.getChallengesManager().createChallenge(uniqueId, type, requirements),
this.topLabel, this.topLabel,
this.permissionPrefix, this.permissionPrefix,
this).build(); this).build();
}); });
}, },
input -> { input -> {
String newName = Utils.getGameMode(this.world) + "_" + input; String uniqueId = Utils.getGameMode(this.world) + "_" + input;
return !this.addon.getChallengesManager().containsChallenge(newName); return !this.addon.getChallengesManager().containsChallenge(uniqueId);
}, },
this.user.getTranslation("challenges.gui.questions.admin.unique-id") this.user.getTranslation("challenges.gui.questions.admin.unique-id")
); );
@ -659,7 +660,7 @@ public class AdminGUI extends CommonGUI
@Override @Override
protected boolean isInputValid(ConversationContext context, String input) 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, protected String getFailedValidationText(ConversationContext context,
String invalidInput) 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) protected Prompt acceptValidatedInput(ConversationContext context, String input)
{ {
// Add answer to consumer. // Add answer to consumer.
consumer.accept(input); consumer.accept(sanitizeInput(input));
// End conversation // End conversation
return Prompt.END_OF_CONVERSATION; return Prompt.END_OF_CONVERSATION;
} }
@ -710,4 +711,14 @@ public class AdminGUI extends CommonGUI
conversation.begin(); 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: admin:
number: "Write a number in the chat and press enter." 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." 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." level-name: "Write the display name in chat for the current level."