Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
InactivityConversationCanceller.java
1 package org.bukkit.conversations;
2 
3 import org.bukkit.Server;
4 import org.bukkit.plugin.Plugin;
5 
6 /**
7  * An InactivityConversationCanceller will cancel a {@link Conversation} after
8  * a period of inactivity by the user.
9  */
11  protected Plugin plugin;
12  protected int timeoutSeconds;
13  protected Conversation conversation;
14  private int taskId = -1;
15 
16  /**
17  * Creates an InactivityConversationCanceller.
18  *
19  * @param plugin The owning plugin.
20  * @param timeoutSeconds The number of seconds of inactivity to wait.
21  */
22  public InactivityConversationCanceller(Plugin plugin, int timeoutSeconds) {
23  this.plugin = plugin;
24  this.timeoutSeconds = timeoutSeconds;
25  }
26 
27  public void setConversation(Conversation conversation) {
28  this.conversation = conversation;
29  startTimer();
30  }
31 
32  public boolean cancelBasedOnInput(ConversationContext context, String input) {
33  // Reset the inactivity timer
34  stopTimer();
35  startTimer();
36  return false;
37  }
38 
40  return new InactivityConversationCanceller(plugin, timeoutSeconds);
41  }
42 
43  /**
44  * Starts an inactivity timer.
45  */
46  private void startTimer() {
47  taskId = plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
48  public void run() {
49  if (conversation.getState() == Conversation.ConversationState.UNSTARTED) {
50  startTimer();
51  } else if (conversation.getState() == Conversation.ConversationState.STARTED) {
52  cancelling(conversation);
53  conversation.abandon(new ConversationAbandonedEvent(conversation, InactivityConversationCanceller.this));
54  }
55  }
56  }, timeoutSeconds * 20);
57  }
58 
59  /**
60  * Stops the active inactivity timer.
61  */
62  private void stopTimer() {
63  if (taskId != -1) {
64  plugin.getServer().getScheduler().cancelTask(taskId);
65  taskId = -1;
66  }
67  }
68 
69  /**
70  * Subclasses of InactivityConversationCanceller can override this method
71  * to take additional actions when the inactivity timer abandons the
72  * conversation.
73  *
74  * @param conversation The conversation being abandoned.
75  */
76  protected void cancelling(Conversation conversation) {
77 
78  }
79 }
boolean cancelBasedOnInput(ConversationContext context, String input)
int scheduleSyncDelayedTask(Plugin plugin, Runnable task, long delay)
BukkitScheduler getScheduler()