org.bukkit.event.player.PlayerChatEvent Maven / Gradle / Ivy
package org.bukkit.event.player;
import org.apache.commons.lang.Validate;
import org.bukkit.Warning;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import java.util.HashSet;
import java.util.Set;
/**
* Holds information for player chat and commands
*
* @deprecated This event will fire from the main thread and allows the use of
* all of the Bukkit API, unlike the {@link AsyncPlayerChatEvent}.
*
* Listening to this event forces chat to wait for the main thread which
* causes delays for chat. {@link AsyncPlayerChatEvent} is the encouraged
* alternative for thread safe implementations.
*/
@Deprecated
@Warning(reason = "Listening to this event forces chat to wait for the main thread, delaying chat messages.")
public class PlayerChatEvent extends PlayerEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final Set recipients;
private boolean cancel = false;
private String message;
private String format;
public PlayerChatEvent(final Player player, final String message) {
super(player);
this.message = message;
this.format = "<%1$s> %2$s";
this.recipients = new HashSet(player.getServer().getOnlinePlayers());
}
public PlayerChatEvent(final Player player, final String message, final String format, final Set recipients) {
super(player);
this.message = message;
this.format = format;
this.recipients = recipients;
}
public static HandlerList getHandlerList() {
return handlers;
}
public boolean isCancelled() {
return cancel;
}
public void setCancelled(boolean cancel) {
this.cancel = cancel;
}
/**
* Gets the message that the player is attempting to send
*
* @return Message the player is attempting to send
*/
public String getMessage() {
return message;
}
/**
* Sets the message that the player will send
*
* @param message New message that the player will send
*/
public void setMessage(String message) {
this.message = message;
}
/**
* Sets the player that this message will display as, or command will be
* executed as
*
* @param player New player which this event will execute as
*/
public void setPlayer(final Player player) {
Validate.notNull(player, "Player cannot be null");
this.player = player;
}
/**
* Gets the format to use to display this chat message
*
* @return String.Format compatible format string
*/
public String getFormat() {
return format;
}
/**
* Sets the format to use to display this chat message
*
* @param format String.Format compatible format string
*/
public void setFormat(final String format) {
// Oh for a better way to do this!
try {
String.format(format, player, message);
} catch (RuntimeException ex) {
ex.fillInStackTrace();
throw ex;
}
this.format = format;
}
/**
* Gets a set of recipients that this chat message will be displayed to
*
* @return All Players who will see this chat message
*/
public Set getRecipients() {
return recipients;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
}