Bukkit  1.5.2-R1.0
 All Classes Namespaces Files Functions Variables Enumerator Pages
ConversationFactory.java
Go to the documentation of this file.
1 package org.bukkit.conversations;
2 
3 import org.bukkit.entity.Player;
4 import org.bukkit.plugin.Plugin;
5 
6 import java.util.ArrayList;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10 
18 public class ConversationFactory {
19 
20  protected Plugin plugin;
21  protected boolean isModal;
22  protected boolean localEchoEnabled;
24  protected Prompt firstPrompt;
25  protected Map<Object, Object> initialSessionData;
26  protected String playerOnlyMessage;
27  protected List<ConversationCanceller> cancellers;
28  protected List<ConversationAbandonedListener> abandonedListeners;
29 
35  {
36  this.plugin = plugin;
37  isModal = true;
38  localEchoEnabled = true;
41  initialSessionData = new HashMap<Object, Object>();
42  playerOnlyMessage = null;
43  cancellers = new ArrayList<ConversationCanceller>();
44  abandonedListeners = new ArrayList<ConversationAbandonedListener>();
45  }
46 
55  public ConversationFactory withModality(boolean modal)
56  {
57  isModal = modal;
58  return this;
59  }
60 
68  this.localEchoEnabled = localEchoEnabled;
69  return this;
70  }
71 
80  this.prefix = prefix;
81  return this;
82  }
83 
91  public ConversationFactory withTimeout(int timeoutSeconds) {
93  }
94 
103  this.firstPrompt = firstPrompt;
104  return this;
105  }
106 
113  this.initialSessionData = initialSessionData;
114  return this;
115  }
116 
122  public ConversationFactory withEscapeSequence(String escapeSequence) {
124  }
125 
126 
133  cancellers.add(canceller);
134  return this;
135  }
136 
143  this.playerOnlyMessage = playerOnlyMessage;
144  return this;
145  }
146 
153  abandonedListeners.add(listener);
154  return this;
155  }
156 
163  //Abort conversation construction if we aren't supposed to talk to non-players
164  if (playerOnlyMessage != null && !(forWhom instanceof Player)) {
165  return new Conversation(plugin, forWhom, new NotPlayerMessagePrompt());
166  }
167 
168  //Clone any initial session data
169  Map<Object, Object> copiedInitialSessionData = new HashMap<Object, Object>();
170  copiedInitialSessionData.putAll(initialSessionData);
171 
172  //Build and return a conversation
173  Conversation conversation = new Conversation(plugin, forWhom, firstPrompt, copiedInitialSessionData);
174  conversation.setModal(isModal);
176  conversation.setPrefix(prefix);
177 
178  //Clone the conversation cancellers
179  for (ConversationCanceller canceller : cancellers) {
180  conversation.addConversationCanceller(canceller.clone());
181  }
182 
183  //Add the ConversationAbandonedListeners
185  conversation.addConversationAbandonedListener(listener);
186  }
187 
188  return conversation;
189  }
190 
191  private class NotPlayerMessagePrompt extends MessagePrompt {
192 
193  public String getPromptText(ConversationContext context) {
194  return playerOnlyMessage;
195  }
196 
197  @Override
198  protected Prompt getNextPrompt(ConversationContext context) {
199  return Prompt.END_OF_CONVERSATION;
200  }
201  }
202 }