Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
PlayerChatEvent.java
1 package org.bukkit.event.player;
2 
3 import java.util.Arrays;
4 import java.util.HashSet;
5 import java.util.Set;
6 
7 import org.apache.commons.lang.Validate;
8 import org.bukkit.Warning;
9 import org.bukkit.entity.Player;
12 
13 /**
14  * Holds information for player chat and commands
15  *
16  * @deprecated This event will fire from the main thread and allows the use of
17  * all of the Bukkit API, unlike the {@link AsyncPlayerChatEvent}.
18  * <p>
19  * Listening to this event forces chat to wait for the main thread which
20  * causes delays for chat. {@link AsyncPlayerChatEvent} is the encouraged
21  * alternative for thread safe implementations.
22  */
23 @Deprecated
24 @Warning(reason="Listening to this event forces chat to wait for the main thread, delaying chat messages.")
25 public class PlayerChatEvent extends PlayerEvent implements Cancellable {
26  private static final HandlerList handlers = new HandlerList();
27  private boolean cancel = false;
28  private String message;
29  private String format;
30  private final Set<Player> recipients;
31 
32  public PlayerChatEvent(final Player player, final String message) {
33  super(player);
34  this.message = message;
35  this.format = "<%1$s> %2$s";
36  this.recipients = new HashSet<Player>(Arrays.asList(player.getServer().getOnlinePlayers()));
37  }
38 
39  public PlayerChatEvent(final Player player, final String message, final String format, final Set<Player> recipients) {
40  super(player);
41  this.message = message;
42  this.format = format;
43  this.recipients = recipients;
44  }
45 
46  public boolean isCancelled() {
47  return cancel;
48  }
49 
50  public void setCancelled(boolean cancel) {
51  this.cancel = cancel;
52  }
53 
54  /**
55  * Gets the message that the player is attempting to send
56  *
57  * @return Message the player is attempting to send
58  */
59  public String getMessage() {
60  return message;
61  }
62 
63  /**
64  * Sets the message that the player will send
65  *
66  * @param message New message that the player will send
67  */
68  public void setMessage(String message) {
69  this.message = message;
70  }
71 
72  /**
73  * Sets the player that this message will display as, or command will be
74  * executed as
75  *
76  * @param player New player which this event will execute as
77  */
78  public void setPlayer(final Player player) {
79  Validate.notNull(player, "Player cannot be null");
80  this.player = player;
81  }
82 
83  /**
84  * Gets the format to use to display this chat message
85  *
86  * @return String.Format compatible format string
87  */
88  public String getFormat() {
89  return format;
90  }
91 
92  /**
93  * Sets the format to use to display this chat message
94  *
95  * @param format String.Format compatible format string
96  */
97  public void setFormat(final String format) {
98  // Oh for a better way to do this!
99  try {
100  String.format(format, player, message);
101  } catch (RuntimeException ex) {
102  ex.fillInStackTrace();
103  throw ex;
104  }
105 
106  this.format = format;
107  }
108 
109  /**
110  * Gets a set of recipients that this chat message will be displayed to
111  *
112  * @return All Players who will see this chat message
113  */
114  public Set<Player> getRecipients() {
115  return recipients;
116  }
117 
118  @Override
119  public HandlerList getHandlers() {
120  return handlers;
121  }
122 
123  public static HandlerList getHandlerList() {
124  return handlers;
125  }
126 }
Player[] getOnlinePlayers()