
org.bukkit.event.player.PlayerCommandPreprocessEvent Maven / Gradle / Ivy
package org.bukkit.event.player;
import org.apache.commons.lang.Validate;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import java.util.HashSet;
import java.util.Set;
/**
* This event is called whenever a player runs a command (by placing a slash
* at the start of their message). It is called early in the command handling
* process, and modifications in this event (via {@link #setMessage(String)})
* will be shown in the behavior.
*
* Many plugins will have no use for this event, and you should
* attempt to avoid using it if it is not necessary.
*
* Some examples of valid uses for this event are:
*
* - Logging executed commands to a separate file
*
- Variable substitution. For example, replacing
*
${nearbyPlayer}
with the name of the nearest other
* player, or simulating the @a
and @p
* decorators used by Command Blocks in plugins that do not handle it.
* - Conditionally blocking commands belonging to other plugins. For
* example, blocking the use of the
/home
command in a
* combat arena.
* - Per-sender command aliases. For example, after a player runs the
* command
/calias cr gamemode creative
, the next time they
* run /cr
, it gets replaced into
* /gamemode creative
. (Global command aliases should be
* done by registering the alias.)
*
*
* Examples of incorrect uses are:
*
* - Using this event to run command logic
*
*
* If the event is cancelled, processing of the command will halt.
*
* The state of whether or not there is a slash (/
) at the
* beginning of the message should be preserved. If a slash is added or
* removed, unexpected behavior may result.
*/
public class PlayerCommandPreprocessEvent 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 = "<%1$s> %2$s";
public PlayerCommandPreprocessEvent(final Player player, final String message) {
super(player);
this.recipients = new HashSet(player.getServer().getOnlinePlayers());
this.message = message;
}
public PlayerCommandPreprocessEvent(final Player player, final String message, final Set recipients) {
super(player);
this.recipients = recipients;
this.message = message;
}
public static HandlerList getHandlerList() {
return handlers;
}
public boolean isCancelled() {
return cancel;
}
public void setCancelled(boolean cancel) {
this.cancel = cancel;
}
/**
* Gets the command that the player is attempting to send.
*
* All commands begin with a special character; implementations do not
* consider the first character when executing the content.
*
* @return Message the player is attempting to send
*/
public String getMessage() {
return message;
}
/**
* Sets the command that the player will send.
*
* All commands begin with a special character; implementations do not
* consider the first character when executing the content.
*
* @param command New message that the player will send
* @throws IllegalArgumentException if command is null or empty
*/
public void setMessage(String command) throws IllegalArgumentException {
Validate.notNull(command, "Command cannot be null");
Validate.notEmpty(command, "Command cannot be empty");
this.message = command;
}
/**
* Sets the player that this command will be executed as.
*
* @param player New player which this event will execute as
* @throws IllegalArgumentException if the player provided is null
*/
public void setPlayer(final Player player) throws IllegalArgumentException {
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
* @deprecated This method is provided for backward compatibility with no
* guarantee to the use of the format.
*/
@Deprecated
public String getFormat() {
return format;
}
/**
* Sets the format to use to display this chat message
*
* @param format String.Format compatible format string
* @deprecated This method is provided for backward compatibility with no
* guarantee to the effect of modifying the format.
*/
@Deprecated
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.
*
* The set returned is not guaranteed to be mutable and may auto-populate
* on access. Any listener accessing the returned set should be aware that
* it may reduce performance for a lazy set implementation. Listeners
* should be aware that modifying the list may throw {@link
* UnsupportedOperationException} if the event caller provides an
* unmodifiable set.
*
* @return All Players who will see this chat message
* @deprecated This method is provided for backward compatibility with no
* guarantee to the effect of viewing or modifying the set.
*/
@Deprecated
public Set getRecipients() {
return recipients;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
}