001 package org.bukkit.event.player;
002
003 import java.util.HashSet;
004 import java.util.Set;
005
006 import org.apache.commons.lang.Validate;
007 import org.bukkit.Warning;
008 import org.bukkit.entity.Player;
009 import org.bukkit.event.Cancellable;
010 import org.bukkit.event.HandlerList;
011
012 /**
013 * Holds information for player chat and commands
014 *
015 * @deprecated This event will fire from the main thread and allows the use of
016 * all of the Bukkit API, unlike the {@link AsyncPlayerChatEvent}.
017 * <p>
018 * Listening to this event forces chat to wait for the main thread which
019 * causes delays for chat. {@link AsyncPlayerChatEvent} is the encouraged
020 * alternative for thread safe implementations.
021 */
022 @Deprecated
023 @Warning(reason="Listening to this event forces chat to wait for the main thread, delaying chat messages.")
024 public class PlayerChatEvent extends PlayerEvent implements Cancellable {
025 private static final HandlerList handlers = new HandlerList();
026 private boolean cancel = false;
027 private String message;
028 private String format;
029 private final Set<Player> recipients;
030
031 public PlayerChatEvent(final Player player, final String message) {
032 super(player);
033 this.message = message;
034 this.format = "<%1$s> %2$s";
035 this.recipients = new HashSet<Player>(player.getServer().getOnlinePlayers());
036 }
037
038 public PlayerChatEvent(final Player player, final String message, final String format, final Set<Player> recipients) {
039 super(player);
040 this.message = message;
041 this.format = format;
042 this.recipients = recipients;
043 }
044
045 public boolean isCancelled() {
046 return cancel;
047 }
048
049 public void setCancelled(boolean cancel) {
050 this.cancel = cancel;
051 }
052
053 /**
054 * Gets the message that the player is attempting to send
055 *
056 * @return Message the player is attempting to send
057 */
058 public String getMessage() {
059 return message;
060 }
061
062 /**
063 * Sets the message that the player will send
064 *
065 * @param message New message that the player will send
066 */
067 public void setMessage(String message) {
068 this.message = message;
069 }
070
071 /**
072 * Sets the player that this message will display as, or command will be
073 * executed as
074 *
075 * @param player New player which this event will execute as
076 */
077 public void setPlayer(final Player player) {
078 Validate.notNull(player, "Player cannot be null");
079 this.player = player;
080 }
081
082 /**
083 * Gets the format to use to display this chat message
084 *
085 * @return String.Format compatible format string
086 */
087 public String getFormat() {
088 return format;
089 }
090
091 /**
092 * Sets the format to use to display this chat message
093 *
094 * @param format String.Format compatible format string
095 */
096 public void setFormat(final String format) {
097 // Oh for a better way to do this!
098 try {
099 String.format(format, player, message);
100 } catch (RuntimeException ex) {
101 ex.fillInStackTrace();
102 throw ex;
103 }
104
105 this.format = format;
106 }
107
108 /**
109 * Gets a set of recipients that this chat message will be displayed to
110 *
111 * @return All Players who will see this chat message
112 */
113 public Set<Player> getRecipients() {
114 return recipients;
115 }
116
117 @Override
118 public HandlerList getHandlers() {
119 return handlers;
120 }
121
122 public static HandlerList getHandlerList() {
123 return handlers;
124 }
125 }