001    package org.bukkit.conversations;
002    
003    import org.bukkit.ChatColor;
004    
005    /**
006     * ValidatingPrompt is the base class for any prompt that requires validation.
007     * ValidatingPrompt will keep replaying the prompt text until the user enters
008     * a valid response.
009     */
010    public abstract class ValidatingPrompt implements Prompt {
011        public ValidatingPrompt() {
012            super();
013        }
014    
015        /**
016         * Accepts and processes input from the user and validates it. If
017         * validation fails, this prompt is returned for re-execution, otherwise
018         * the next Prompt in the prompt graph is returned.
019         *
020         * @param context Context information about the conversation.
021         * @param input The input text from the user.
022         * @return This prompt or the next Prompt in the prompt graph.
023         */
024        public Prompt acceptInput(ConversationContext context, String input) {
025            if (isInputValid(context, input)) {
026                return acceptValidatedInput(context, input);
027            } else {
028                String failPrompt = getFailedValidationText(context, input);
029                if (failPrompt != null) {
030                    context.getForWhom().sendRawMessage(ChatColor.RED + failPrompt);
031                }
032                // Redisplay this prompt to the user to re-collect input
033                return this;
034            }
035        }
036    
037        /**
038         * Ensures that the prompt waits for the user to provide input.
039         *
040         * @param context Context information about the conversation.
041         * @return True.
042         */
043        public boolean blocksForInput(ConversationContext context) {
044            return true;
045        }
046    
047        /**
048         * Override this method to check the validity of the player's input.
049         *
050         * @param context Context information about the conversation.
051         * @param input The player's raw console input.
052         * @return True or false depending on the validity of the input.
053         */
054        protected abstract boolean isInputValid(ConversationContext context, String input);
055    
056        /**
057         * Override this method to accept and processes the validated input from
058         * the user. Using the input, the next Prompt in the prompt graph should
059         * be returned.
060         *
061         * @param context Context information about the conversation.
062         * @param input The validated input text from the user.
063         * @return The next Prompt in the prompt graph.
064         */
065        protected abstract Prompt acceptValidatedInput(ConversationContext context, String input);
066    
067        /**
068         * Optionally override this method to display an additional message if the
069         * user enters an invalid input.
070         *
071         * @param context Context information about the conversation.
072         * @param invalidInput The invalid input provided by the user.
073         * @return A message explaining how to correct the input.
074         */
075        protected String getFailedValidationText(ConversationContext context, String invalidInput) {
076            return null;
077        }
078    }