Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
NumericPrompt.java
1 package org.bukkit.conversations;
2 
3 import org.apache.commons.lang.math.NumberUtils;
4 
5 /**
6  * NumericPrompt is the base class for any prompt that requires a {@link
7  * Number} response from the user.
8  */
9 public abstract class NumericPrompt extends ValidatingPrompt{
10  public NumericPrompt() {
11  super();
12  }
13 
14  @Override
15  protected boolean isInputValid(ConversationContext context, String input) {
16  return NumberUtils.isNumber(input) && isNumberValid(context, NumberUtils.createNumber(input));
17  }
18 
19  /**
20  * Override this method to do further validation on the numeric player
21  * input after the input has been determined to actually be a number.
22  *
23  * @param context Context information about the conversation.
24  * @param input The number the player provided.
25  * @return The validity of the player's input.
26  */
27  protected boolean isNumberValid(ConversationContext context, Number input) {
28  return true;
29  }
30 
31  @Override
32  protected Prompt acceptValidatedInput(ConversationContext context, String input) {
33  try
34  {
35  return acceptValidatedInput(context, NumberUtils.createNumber(input));
36  } catch (NumberFormatException e) {
37  return acceptValidatedInput(context, NumberUtils.INTEGER_ZERO);
38  }
39  }
40 
41  /**
42  * Override this method to perform some action with the user's integer
43  * response.
44  *
45  * @param context Context information about the conversation.
46  * @param input The user's response as a {@link Number}.
47  * @return The next {@link Prompt} in the prompt graph.
48  */
49  protected abstract Prompt acceptValidatedInput(ConversationContext context, Number input);
50 
51  @Override
52  protected String getFailedValidationText(ConversationContext context, String invalidInput) {
53  if (NumberUtils.isNumber(invalidInput)) {
54  return getFailedValidationText(context, NumberUtils.createNumber(invalidInput));
55  } else {
56  return getInputNotNumericText(context, invalidInput);
57  }
58  }
59 
60  /**
61  * Optionally override this method to display an additional message if the
62  * user enters an invalid number.
63  *
64  * @param context Context information about the conversation.
65  * @param invalidInput The invalid input provided by the user.
66  * @return A message explaining how to correct the input.
67  */
68  protected String getInputNotNumericText(ConversationContext context, String invalidInput) {
69  return null;
70  }
71 
72  /**
73  * Optionally override this method to display an additional message if the
74  * user enters an invalid numeric input.
75  *
76  * @param context Context information about the conversation.
77  * @param invalidInput The invalid input provided by the user.
78  * @return A message explaining how to correct the input.
79  */
80  protected String getFailedValidationText(ConversationContext context, Number invalidInput) {
81  return null;
82  }
83 }
String getInputNotNumericText(ConversationContext context, String invalidInput)
boolean isNumberValid(ConversationContext context, Number input)
String getFailedValidationText(ConversationContext context, Number invalidInput)