public void run() { if (conversation.getState() == Conversation.ConversationState.UNSTARTED) { startTimer(); } else if (conversation.getState() == Conversation.ConversationState.STARTED) { cancelling(conversation); conversation.abandon(new ConversationAbandonedEvent(conversation, InactivityConversationCanceller.this)); } } }, timeoutSeconds * 20);
/** * Sets the player input that, when received, will immediately terminate * the conversation. * * @param escapeSequence Input to terminate the conversation. * @return This object. */ public ConversationFactory withEscapeSequence(String escapeSequence) { return withConversationCanceller(new ExactMatchConversationCanceller(escapeSequence)); }
/** * Abandons and resets the current conversation. Restores the user's * normal chat behavior. */ public void abandon() { abandon(new ConversationAbandonedEvent(this, new ManuallyAbandonedConversationCanceller())); }
/** * Displays the next user prompt and abandons the conversation if the next * prompt is null. */ public void outputNextPrompt() { if (currentPrompt == null) { abandon(new ConversationAbandonedEvent(this)); } else { context.getForWhom().sendRawMessage(prefix.getPrefix(context) + currentPrompt.getPromptText(context)); if (!currentPrompt.blocksForInput(context)) { currentPrompt = currentPrompt.acceptInput(context, null); outputNextPrompt(); } } }
/** * Passes player input into the current prompt. The next prompt (as * determined by the current prompt) is then displayed to the user. * * @param input The user's chat text. */ public void acceptInput(String input) { if (currentPrompt != null) { // Echo the user's input if (localEchoEnabled) { context.getForWhom().sendRawMessage(prefix.getPrefix(context) + input); } // Test for conversation abandonment based on input for(ConversationCanceller canceller : cancellers) { if (canceller.cancelBasedOnInput(context, input)) { abandon(new ConversationAbandonedEvent(this, canceller)); return; } } // Not abandoned, output the next prompt currentPrompt = currentPrompt.acceptInput(context, input); outputNextPrompt(); } }
/** * Accepts and processes input from the user and validates it. If * validation fails, this prompt is returned for re-execution, otherwise * the next Prompt in the prompt graph is returned. * * @param context Context information about the conversation. * @param input The input text from the user. * @return This prompt or the next Prompt in the prompt graph. */ public Prompt acceptInput(ConversationContext context, String input) { if (isInputValid(context, input)) { return acceptValidatedInput(context, input); } else { String failPrompt = getFailedValidationText(context, input); if (failPrompt != null) { context.getForWhom().sendRawMessage(ChatColor.RED + failPrompt); } // Redisplay this prompt to the user to re-collect input return this; } }
/** * Abandons and resets the current conversation. Restores the user's * normal chat behavior. * * @param details Details about why the conversation was abandoned */ public synchronized void abandon(ConversationAbandonedEvent details) { if (!abandoned) { abandoned = true; currentPrompt = null; context.getForWhom().abandonConversation(this); for (ConversationAbandonedListener listener : abandonedListeners) { listener.conversationAbandoned(details); } } }
/** * Displays the first prompt of this conversation and begins redirecting * the user's chat responses. */ public void begin() { if (currentPrompt == null) { abandoned = false; currentPrompt = firstPrompt; context.getForWhom().beginConversation(this); } }
/** * Sets the number of inactive seconds to wait before automatically * abandoning all generated conversations. * <p> * The default is 600 seconds (5 minutes). * * @param timeoutSeconds The number of seconds to wait. * @return This object. */ public ConversationFactory withTimeout(int timeoutSeconds) { return withConversationCanceller(new InactivityConversationCanceller(plugin, timeoutSeconds)); }
public boolean cancelBasedOnInput(ConversationContext context, String input) { // Reset the inactivity timer stopTimer(); startTimer(); return false; }
@Override protected String getFailedValidationText(ConversationContext context, String invalidInput) { if (NumberUtils.isNumber(invalidInput)) { return getFailedValidationText(context, NumberUtils.createNumber(invalidInput)); } else { return getInputNotNumericText(context, invalidInput); } }
/** * Gets the entity for whom this conversation is mediating. * * @return The entity. */ public Conversable getForWhom() { return context.getForWhom(); }
public ConversationCanceller clone() { return new ExactMatchConversationCanceller(escapeSequence); } }
/** * Adds a {@link ConversationCanceller} to the cancellers collection. * * @param canceller The {@link ConversationCanceller} to add. */ void addConversationCanceller(ConversationCanceller canceller) { canceller.setConversation(this); this.cancellers.add(canceller); }
/** * Accepts and ignores any user input, returning the next prompt in the * prompt graph instead. * * @param context Context information about the conversation. * @param input Ignored. * @return The next prompt in the prompt graph. */ public Prompt acceptInput(ConversationContext context, String input) { return getNextPrompt(context); }
public ConversationAbandonedEvent(Conversation conversation, ConversationCanceller canceller) { super(conversation); this.context = conversation.getContext(); this.canceller = canceller; }
public ConversationCanceller clone() { return new InactivityConversationCanceller(plugin, timeoutSeconds); }
public void setConversation(Conversation conversation) { this.conversation = conversation; startTimer(); }
@Override protected Prompt acceptValidatedInput(ConversationContext context, String input) { try { return acceptValidatedInput(context, NumberUtils.createNumber(input)); } catch (NumberFormatException e) { return acceptValidatedInput(context, NumberUtils.INTEGER_ZERO); } }
@Override protected boolean isInputValid(ConversationContext context, String input) { return NumberUtils.isNumber(input) && isNumberValid(context, NumberUtils.createNumber(input)); }