Class PlayerCommandPreprocessEvent

java.lang.Object
org.bukkit.event.Event
org.bukkit.event.player.PlayerEvent
org.bukkit.event.player.PlayerCommandPreprocessEvent
All Implemented Interfaces:
Cancellable
Direct Known Subclasses:
PlayerSignCommandPreprocessEvent

public class PlayerCommandPreprocessEvent extends PlayerEvent implements Cancellable
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 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.

  • Constructor Details

  • Method Details

    • isCancelled

      public boolean isCancelled()
      Description copied from interface: Cancellable
      Gets the cancellation state of this event. A cancelled event will not be executed in the server, but will still pass to other plugins
      Specified by:
      isCancelled in interface Cancellable
      Returns:
      true if this event is cancelled
    • setCancelled

      public void setCancelled(boolean cancel)
      Description copied from interface: Cancellable
      Sets the cancellation state of this event. A cancelled event will not be executed in the server, but will still pass to other plugins.
      Specified by:
      setCancelled in interface Cancellable
      Parameters:
      cancel - true if you wish to cancel this event
    • getMessage

      @NotNull public @NotNull String getMessage()
      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.

      Returns:
      Message the player is attempting to send
    • setMessage

      public void setMessage(@NotNull @NotNull String command) throws IllegalArgumentException
      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.

      Parameters:
      command - New message that the player will send
      Throws:
      IllegalArgumentException - if command is null or empty
    • setPlayer

      @Deprecated(forRemoval=true) public void setPlayer(@NotNull @NotNull Player player) throws IllegalArgumentException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Only works for sign commands; use Player.performCommand(String), including those cases
      Sets the player that this command will be executed as.
      Parameters:
      player - New player which this event will execute as
      Throws:
      IllegalArgumentException - if the player provided is null
    • getRecipients

      @NotNull @Deprecated(since="1.3.1", forRemoval=true) public @NotNull Set<Player> getRecipients()
      Deprecated, for removal: This API element is subject to removal in a future version.
      This is simply the online players. Modifications have no effect
      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 UnsupportedOperationException if the event caller provides an unmodifiable set.

      Returns:
      All Players who will see this chat message
    • getHandlers

      @NotNull public @NotNull HandlerList getHandlers()
      Specified by:
      getHandlers in class Event
    • getHandlerList

      @NotNull public static @NotNull HandlerList getHandlerList()