Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
PlayerCommandPreprocessEvent.java
1 package org.bukkit.event.player;
2 
3 import java.util.Arrays;
4 import java.util.HashSet;
5 import java.util.Set;
6 
7 import org.apache.commons.lang.Validate;
8 import org.bukkit.entity.Player;
11 
12 /**
13  * This event is called whenever a player runs a command (by placing a slash
14  * at the start of their message). It is called early in the command handling
15  * process, and modifications in this event (via {@link #setMessage(String)})
16  * will be shown in the behavior.
17  * <p>
18  * Many plugins will have <b>no use for this event</b>, and you should
19  * attempt to avoid using it if it is not necessary.
20  * <p>
21  * Some examples of valid uses for this event are:
22  * <ul>
23  * <li>Logging executed commands to a separate file
24  * <li>Variable substitution. For example, replacing
25  * <code>${nearbyPlayer}</code> with the name of the nearest other
26  * player, or simulating the <code>@a</code> and <code>@p</code>
27  * decorators used by Command Blocks in plugins that do not handle it.
28  * <li>Conditionally blocking commands belonging to other plugins. For
29  * example, blocking the use of the <code>/home</code> command in a
30  * combat arena.
31  * <li>Per-sender command aliases. For example, after a player runs the
32  * command <code>/calias cr gamemode creative</code>, the next time they
33  * run <code>/cr</code>, it gets replaced into
34  * <code>/gamemode creative</code>. (Global command aliases should be
35  * done by registering the alias.)
36  * </ul>
37  * <p>
38  * Examples of incorrect uses are:
39  * <ul>
40  * <li>Using this event to run command logic
41  * </ul>
42  * <p>
43  * If the event is cancelled, processing of the command will halt.
44  * <p>
45  * The state of whether or not there is a slash (<code>/</code>) at the
46  * beginning of the message should be preserved. If a slash is added or
47  * removed, unexpected behavior may result.
48  */
49 public class PlayerCommandPreprocessEvent extends PlayerEvent implements Cancellable {
50  private static final HandlerList handlers = new HandlerList();
51  private boolean cancel = false;
52  private String message;
53  private String format = "<%1$s> %2$s";
54  private final Set<Player> recipients;
55 
56  public PlayerCommandPreprocessEvent(final Player player, final String message) {
57  super(player);
58  this.recipients = new HashSet<Player>(Arrays.asList(player.getServer().getOnlinePlayers()));
59  this.message = message;
60  }
61 
62  public PlayerCommandPreprocessEvent(final Player player, final String message, final Set<Player> recipients) {
63  super(player);
64  this.recipients = recipients;
65  this.message = message;
66  }
67 
68  public boolean isCancelled() {
69  return cancel;
70  }
71 
72  public void setCancelled(boolean cancel) {
73  this.cancel = cancel;
74  }
75 
76  /**
77  * Gets the command that the player is attempting to send.
78  * <p>
79  * All commands begin with a special character; implementations do not
80  * consider the first character when executing the content.
81  *
82  * @return Message the player is attempting to send
83  */
84  public String getMessage() {
85  return message;
86  }
87 
88  /**
89  * Sets the command that the player will send.
90  * <p>
91  * All commands begin with a special character; implementations do not
92  * consider the first character when executing the content.
93  *
94  * @param command New message that the player will send
95  * @throws IllegalArgumentException if command is null or empty
96  */
97  public void setMessage(String command) throws IllegalArgumentException {
98  Validate.notNull(command, "Command cannot be null");
99  Validate.notEmpty(command, "Command cannot be empty");
100  this.message = command;
101  }
102 
103  /**
104  * Sets the player that this command will be executed as.
105  *
106  * @param player New player which this event will execute as
107  * @throws IllegalArgumentException if the player provided is null
108  */
109  public void setPlayer(final Player player) throws IllegalArgumentException {
110  Validate.notNull(player, "Player cannot be null");
111  this.player = player;
112  }
113 
114  /**
115  * Gets the format to use to display this chat message
116  *
117  * @deprecated This method is provided for backward compatibility with no
118  * guarantee to the use of the format.
119  * @return String.Format compatible format string
120  */
121  @Deprecated
122  public String getFormat() {
123  return format;
124  }
125 
126  /**
127  * Sets the format to use to display this chat message
128  *
129  * @deprecated This method is provided for backward compatibility with no
130  * guarantee to the effect of modifying the format.
131  * @param format String.Format compatible format string
132  */
133  @Deprecated
134  public void setFormat(final String format) {
135  // Oh for a better way to do this!
136  try {
137  String.format(format, player, message);
138  } catch (RuntimeException ex) {
139  ex.fillInStackTrace();
140  throw ex;
141  }
142 
143  this.format = format;
144  }
145 
146  /**
147  * Gets a set of recipients that this chat message will be displayed to.
148  * <p>
149  * The set returned is not guaranteed to be mutable and may auto-populate
150  * on access. Any listener accessing the returned set should be aware that
151  * it may reduce performance for a lazy set implementation. Listeners
152  * should be aware that modifying the list may throw {@link
153  * UnsupportedOperationException} if the event caller provides an
154  * unmodifiable set.
155  *
156  * @deprecated This method is provided for backward compatibility with no
157  * guarantee to the effect of viewing or modifying the set.
158  * @return All Players who will see this chat message
159  */
160  @Deprecated
161  public Set<Player> getRecipients() {
162  return recipients;
163  }
164 
165  @Override
166  public HandlerList getHandlers() {
167  return handlers;
168  }
169 
170  public static HandlerList getHandlerList() {
171  return handlers;
172  }
173 }
Player[] getOnlinePlayers()