Bukkit  1.5.2-R1.0
 All Classes Namespaces Files Functions Variables Enumerator Pages
Conversation.java
Go to the documentation of this file.
1 package org.bukkit.conversations;
2 
3 import org.bukkit.plugin.Plugin;
4 
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9 
29 public class Conversation {
30 
31  private Prompt firstPrompt;
32  private boolean abandoned;
33  protected Prompt currentPrompt;
35  protected boolean modal;
36  protected boolean localEchoEnabled;
38  protected List<ConversationCanceller> cancellers;
39  protected List<ConversationAbandonedListener> abandonedListeners;
40 
47  public Conversation(Plugin plugin, Conversable forWhom, Prompt firstPrompt) {
48  this(plugin, forWhom, firstPrompt, new HashMap<Object, Object>());
49  }
50 
58  public Conversation(Plugin plugin, Conversable forWhom, Prompt firstPrompt, Map<Object, Object> initialSessionData) {
59  this.firstPrompt = firstPrompt;
60  this.context = new ConversationContext(plugin, forWhom, initialSessionData);
61  this.modal = true;
62  this.localEchoEnabled = true;
63  this.prefix = new NullConversationPrefix();
64  this.cancellers = new ArrayList<ConversationCanceller>();
65  this.abandonedListeners = new ArrayList<ConversationAbandonedListener>();
66  }
67 
73  return context.getForWhom();
74  }
75 
81  public boolean isModal() {
82  return modal;
83  }
84 
90  void setModal(boolean modal) {
91  this.modal = modal;
92  }
93 
99  public boolean isLocalEchoEnabled() {
100  return localEchoEnabled;
101  }
102 
108  public void setLocalEchoEnabled(boolean localEchoEnabled) {
109  this.localEchoEnabled = localEchoEnabled;
110  }
111 
117  return prefix;
118  }
119 
124  void setPrefix(ConversationPrefix prefix) {
125  this.prefix = prefix;
126  }
127 
132  void addConversationCanceller(ConversationCanceller canceller) {
133  canceller.setConversation(this);
134  this.cancellers.add(canceller);
135  }
136 
141  public List<ConversationCanceller> getCancellers() {
142  return cancellers;
143  }
144 
150  return context;
151  }
152 
156  public void begin() {
157  if (currentPrompt == null) {
158  abandoned = false;
159  currentPrompt = firstPrompt;
161  }
162  }
163 
169  if (currentPrompt != null) {
170  return ConversationState.STARTED;
171  } else if (abandoned) {
173  } else {
175  }
176  }
177 
183  public void acceptInput(String input) {
184  if (currentPrompt != null) {
185 
186  // Echo the user's input
187  if (localEchoEnabled) {
189  }
190 
191  // Test for conversation abandonment based on input
192  for(ConversationCanceller canceller : cancellers) {
193  if (canceller.cancelBasedOnInput(context, input)) {
194  abandon(new ConversationAbandonedEvent(this, canceller));
195  return;
196  }
197  }
198 
199  // Not abandoned, output the next prompt
202  }
203  }
204 
210  abandonedListeners.add(listener);
211  }
212 
218  abandonedListeners.remove(listener);
219  }
220 
224  public void abandon() {
226  }
227 
232  public synchronized void abandon(ConversationAbandonedEvent details) {
233  if (!abandoned) {
234  abandoned = true;
235  currentPrompt = null;
238  listener.conversationAbandoned(details);
239  }
240  }
241  }
242 
246  public void outputNextPrompt() {
247  if (currentPrompt == null) {
249  } else {
254  }
255  }
256  }
257 
258  public enum ConversationState {
261  ABANDONED
262  }
263 }