Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
ValidatingPrompt.java
1 package org.bukkit.conversations;
2 
3 import org.bukkit.ChatColor;
4 
5 /**
6  * ValidatingPrompt is the base class for any prompt that requires validation.
7  * ValidatingPrompt will keep replaying the prompt text until the user enters
8  * a valid response.
9  */
10 public abstract class ValidatingPrompt implements Prompt {
11  public ValidatingPrompt() {
12  super();
13  }
14 
15  /**
16  * Accepts and processes input from the user and validates it. If
17  * validation fails, this prompt is returned for re-execution, otherwise
18  * the next Prompt in the prompt graph is returned.
19  *
20  * @param context Context information about the conversation.
21  * @param input The input text from the user.
22  * @return This prompt or the next Prompt in the prompt graph.
23  */
24  public Prompt acceptInput(ConversationContext context, String input) {
25  if (isInputValid(context, input)) {
26  return acceptValidatedInput(context, input);
27  } else {
28  String failPrompt = getFailedValidationText(context, input);
29  if (failPrompt != null) {
30  context.getForWhom().sendRawMessage(ChatColor.RED + failPrompt);
31  }
32  // Redisplay this prompt to the user to re-collect input
33  return this;
34  }
35  }
36 
37  /**
38  * Ensures that the prompt waits for the user to provide input.
39  *
40  * @param context Context information about the conversation.
41  * @return True.
42  */
43  public boolean blocksForInput(ConversationContext context) {
44  return true;
45  }
46 
47  /**
48  * Override this method to check the validity of the player's input.
49  *
50  * @param context Context information about the conversation.
51  * @param input The player's raw console input.
52  * @return True or false depending on the validity of the input.
53  */
54  protected abstract boolean isInputValid(ConversationContext context, String input);
55 
56  /**
57  * Override this method to accept and processes the validated input from
58  * the user. Using the input, the next Prompt in the prompt graph should
59  * be returned.
60  *
61  * @param context Context information about the conversation.
62  * @param input The validated input text from the user.
63  * @return The next Prompt in the prompt graph.
64  */
65  protected abstract Prompt acceptValidatedInput(ConversationContext context, String input);
66 
67  /**
68  * Optionally override this method to display an additional message if the
69  * user enters an invalid input.
70  *
71  * @param context Context information about the conversation.
72  * @param invalidInput The invalid input provided by the user.
73  * @return A message explaining how to correct the input.
74  */
75  protected String getFailedValidationText(ConversationContext context, String invalidInput) {
76  return null;
77  }
78 }
String getFailedValidationText(ConversationContext context, String invalidInput)
abstract Prompt acceptValidatedInput(ConversationContext context, String input)
Prompt acceptInput(ConversationContext context, String input)
void sendRawMessage(String message)
abstract boolean isInputValid(ConversationContext context, String input)
boolean blocksForInput(ConversationContext context)