Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
AsyncPlayerChatEvent.java
1 package org.bukkit.event.player;
2 
3 import java.util.IllegalFormatException;
4 import java.util.Set;
5 
6 import org.bukkit.entity.Player;
9 
10 /**
11  * This event will sometimes fire synchronously, depending on how it was
12  * triggered.
13  * <p>
14  * The constructor provides a boolean to indicate if the event was fired
15  * synchronously or asynchronously. When asynchronous, this event can be
16  * called from any thread, sans the main thread, and has limited access to the
17  * API.
18  * <p>
19  * If a player is the direct cause of this event by an incoming packet, this
20  * event will be asynchronous. If a plugin triggers this event by compelling a
21  * player to chat, this event will be synchronous.
22  * <p>
23  * Care should be taken to check {@link #isAsynchronous()} and treat the event
24  * appropriately.
25  */
26 public class AsyncPlayerChatEvent extends PlayerEvent implements Cancellable {
27  private static final HandlerList handlers = new HandlerList();
28  private boolean cancel = false;
29  private String message;
30  private String format = "<%1$s> %2$s";
31  private final Set<Player> recipients;
32 
33  /**
34  *
35  * @param async This changes the event to a synchronous state.
36  * @param who the chat sender
37  * @param message the message sent
38  * @param players the players to receive the message. This may be a lazy
39  * or unmodifiable collection.
40  */
41  public AsyncPlayerChatEvent(final boolean async, final Player who, final String message, final Set<Player> players) {
42  super(who, async);
43  this.message = message;
44  recipients = players;
45  }
46 
47  /**
48  * Gets the message that the player is attempting to send. This message
49  * will be used with {@link #getFormat()}.
50  *
51  * @return Message the player is attempting to send
52  */
53  public String getMessage() {
54  return message;
55  }
56 
57  /**
58  * Sets the message that the player will send. This message will be used
59  * with {@link #getFormat()}.
60  *
61  * @param message New message that the player will send
62  */
63  public void setMessage(String message) {
64  this.message = message;
65  }
66 
67  /**
68  * Gets the format to use to display this chat message.
69  * <p>
70  * When this event finishes execution, the first format parameter is the
71  * {@link Player#getDisplayName()} and the second parameter is {@link
72  * #getMessage()}
73  *
74  * @return {@link String#format(String, Object...)} compatible format
75  * string
76  */
77  public String getFormat() {
78  return format;
79  }
80 
81  /**
82  * Sets the format to use to display this chat message.
83  * <p>
84  * When this event finishes execution, the first format parameter is the
85  * {@link Player#getDisplayName()} and the second parameter is {@link
86  * #getMessage()}
87  *
88  * @param format {@link String#format(String, Object...)} compatible
89  * format string
90  * @throws IllegalFormatException if the underlying API throws the
91  * exception
92  * @throws NullPointerException if format is null
93  * @see String#format(String, Object...)
94  */
95  public void setFormat(final String format) throws IllegalFormatException, NullPointerException {
96  // Oh for a better way to do this!
97  try {
98  String.format(format, player, message);
99  } catch (RuntimeException ex) {
100  ex.fillInStackTrace();
101  throw ex;
102  }
103 
104  this.format = format;
105  }
106 
107  /**
108  * Gets a set of recipients that this chat message will be displayed to.
109  * <p>
110  * The set returned is not guaranteed to be mutable and may auto-populate
111  * on access. Any listener accessing the returned set should be aware that
112  * it may reduce performance for a lazy set implementation.
113  * <p>
114  * Listeners should be aware that modifying the list may throw {@link
115  * UnsupportedOperationException} if the event caller provides an
116  * unmodifiable set.
117  *
118  * @return All Players who will see this chat message
119  */
120  public Set<Player> getRecipients() {
121  return recipients;
122  }
123 
124  public boolean isCancelled() {
125  return cancel ;
126  }
127 
128  public void setCancelled(boolean cancel) {
129  this.cancel = cancel;
130  }
131 
132  @Override
133  public HandlerList getHandlers() {
134  return handlers;
135  }
136 
137  public static HandlerList getHandlerList() {
138  return handlers;
139  }
140 }
AsyncPlayerChatEvent(final boolean async, final Player who, final String message, final Set< Player > players)