类 PlayerCommandPreprocessEvent

java.lang.Object
org.bukkit.event.Event
org.bukkit.event.player.PlayerEvent
org.bukkit.event.player.PlayerCommandPreprocessEvent
所有已实现的接口:
Cancellable

public class PlayerCommandPreprocessEvent extends PlayerEvent implements Cancellable
这个事件是,当一个玩家执行一个命令的时候将会被触发(也就是在聊天框里面输入信息以/开头的时候,算作命令,就会触发此事件)。 且这个事件是早于插件的onCommand接收的命令的。 如果你此时调用setMessage(String)方法的话,其他插件收到的命令将会是你更改以后的命令。 警告:如果没必要,请避免使用此事件. 如果你不知道这个事件有啥用,下面的例子可能有助于你理解该事件的作用.
  • 接收到一个带有变量的命令,把它用对应要替换成的
  • 量来替换这个变量. 举个例子 ${nearbyPlayer} 是一个变量,然后我们需要把它替换成离发送命令最近的玩家的名字 又或者是替换成 @a@p 这样的变量 通过插件调用命令方块来执行它,而不是插件.
  • 你可以用这个事件来阻止其他插件的命令执行. 比 如, 阻止玩家在竞技场内使用 /home 这个命令.
  • 你可以简化原本复杂的命令,将它用简单的命令替代. 比如啊, 在一个玩家输 入 /calias cr gamemode creative 这个命令之后, 你可以将其拦截以后替换 成 /cr 这个命令, 然后再把这个命令直接 替换成 /gamemode creative. (全局性的命令别名依然 需要在Plugin.yml内注册.)

切忌这样使用,比如:

  • 强制去执行一个命令

如果该事件满足条件被取消掉,对应的命令将会无法发挥任何作用.

另外,命令内的getMessage()这个方法,返回的String会带有一个/ 切忌删除此/,否则将出现无法预料的错误.

原文: 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.

  • 构造器详细资料

    • PlayerCommandPreprocessEvent

      public PlayerCommandPreprocessEvent(Player player, String message)
    • PlayerCommandPreprocessEvent

      public PlayerCommandPreprocessEvent(Player player, String message, Set<Player> recipients)
  • 方法详细资料

    • isCancelled

      public boolean isCancelled()
      从接口复制的说明: 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

      指定者:
      isCancelled 在接口中 Cancellable
      返回:
      如果事件已经被取消,则为true
    • setCancelled

      public void setCancelled(boolean cancel)
      从接口复制的说明: 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.

      指定者:
      setCancelled 在接口中 Cancellable
      参数:
      cancel - 如果你想取消这个事件,则为true
    • getMessage

      public 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.

      返回:
      返回玩家所输入的命令所有字符串,包括/
    • setMessage

      public void setMessage(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.

      参数:
      command - 设置即将要发送的命令
      抛出:
      IllegalArgumentException - 如果这个命令为Null或者为空
    • setPlayer

      public void setPlayer(Player player) throws IllegalArgumentException
      设置这个命令的执行者

      原文:Sets the player that this command will be executed as.

      参数:
      player - 新的命令执行者
      抛出:
      IllegalArgumentException - 如果这个玩家为null
    • getRecipients

      @Deprecated public Set<Player> getRecipients()
      已过时。
      该方法无法保证在每个版本上的效果
      获取所有能看到这个消息的玩家

      本方法返回的集合不保证可以改变和访问时可能自动填充。 任何监听器访问这个返回的集合应该知道对于一个lazy set的实现可能会降低性能. 监听器应注意到如果事件传唤者提供了一个不可修改的Set集合的话修改这个列表可能会抛出UnsupportedOperationException异常。

      原文: 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.

      返回:
      所有看见该消息的玩家
    • getHandlers

      public HandlerList getHandlers()
      指定者:
      getHandlers 在类中 Event
    • getHandlerList

      public static HandlerList getHandlerList()