Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package org.bukkit.command;
2
3import org.bukkit.Bukkit;
4import org.bukkit.ChatColor;
5import org.bukkit.Server;
6import org.bukkit.permissions.Permissible;
7
8import java.util.ArrayList;
9import java.util.List;
10import java.util.Set;
11
12/**
13 * Represents a Command, which executes various tasks upon user input
14 */
15public abstract class Command {
16 private final String name;
17 private String nextLabel;
18 private String label;
19 private List<String> aliases;
20 private List<String> activeAliases;
21 private CommandMap commandMap = null;
22 protected String description = "";
23 protected String usageMessage;
24 private String permission;
25 private boolean isHidden;
26
27 protected Command(String name) {
28 this(name, "", "/" + name, new ArrayList<String>());
29 }
30
31 protected Command(String name, String description, String usageMessage, List<String> aliases) {
32 this.name = name;
33 this.nextLabel = name;
34 this.label = name;
35 this.description = description;
36 this.usageMessage = usageMessage;
37 this.aliases = aliases;
38 this.activeAliases = new ArrayList<String>(aliases);
39 }
40
41 /**
42 * Executes the command, returning its success
43 *
44 * @param sender Source object which is executing this command
45 * @param commandLabel The alias of the command used
46 * @param args All arguments passed to the command, split via ' '
47 * @return true if the command was successful, otherwise false
48 */
49 public abstract boolean execute(CommandSender sender, String commandLabel, String[] args);
50
51 /**
52 * Returns the name of this command
53 *
54 * @return Name of this command
55 */
56 public String getName() {
57 return name;
58 }
59
60 /**
61 * Gets the permission required by users to be able to perform this command
62 *
63 * @return Permission name, or null if none
64 */
65 public String getPermission() {
66 return permission;
67 }
68
69 /**
70 * Sets the permission required by users to be able to perform this command
71 *
72 * @param permission Permission name or null
73 */
74 public void setPermission(String permission) {
75 this.permission = permission;
76 }
77
78 /**
79 * Tests the given {@link CommandSender} to see if they can perform this command.
80 * <p>
81 * If they do not have permission, they will be informed that they cannot do this.
82 *
83 * @param target User to test
84 * @return true if they can use it, otherwise false
85 */
86 public boolean testPermission(CommandSender target) {
87 if ((permission == null) || (permission.length() == 0) || (target.hasPermission(permission)) || target.isOp()) {
88 return true;
89 }
90
91 target.sendMessage(ChatColor.RED + "I'm sorry, Dave. I'm afraid I can't do that.");
92 return false;
93 }
94
95 /**
96 * Returns the current lable for this command
97 *
98 * @return Label of this command or null if not registered
99 */
100 public String getLabel() {
101 return label;
102 }
103
104 /**
105 * Sets the label of this command
106 * If the command is currently registered the label change will only take effect after
107 * its been reregistered e.g. after a /reload
108 *
109 * @return returns true if the name change happened instantly or false if it was scheduled for reregistration
110 */
111 public boolean setLabel(String name) {
112 this.nextLabel = name;
113 if (!isRegistered()) {
114 this.label = name;
115 return true;
116 }
117 return false;
118 }
119
120 /**
121 * Registers this command to a CommandMap
122 * Once called it only allows changes the registered CommandMap
123 *
124 * @param commandMap the CommandMap to register this command to
125 * @return true if the registration was successful (the current registered CommandMap was the passed CommandMap or null) false otherwise
126 */
127 public boolean register(CommandMap commandMap) {
128 if (allowChangesFrom(commandMap)) {
129 this.commandMap = commandMap;
130 return true;
131 }
132
133 return false;
134 }
135
136 /**
137 * Unregisters this command from the passed CommandMap applying any outstanding changes
138 *
139 * @param commandMap the CommandMap to unregister
140 * @return true if the unregistration was successfull (the current registered CommandMap was the passed CommandMap or null) false otherwise
141 */
142 public boolean unregister(CommandMap commandMap) {
143 if (allowChangesFrom(commandMap)) {
144 this.commandMap = null;
145 this.activeAliases = new ArrayList<String>(this.aliases);
146 this.label = this.nextLabel;
147 return true;
148 }
149
150 return false;
151 }
152
153
154 private boolean allowChangesFrom(CommandMap commandMap) {
155 return (null == this.commandMap || this.commandMap == commandMap);
156 }
157
158 /**
159 * Returns the current registered state of this command
160 *
161 * @return true if this command is currently registered false otherwise
162 */
163 public boolean isRegistered() {
164 return (null != this.commandMap);
165 }
166
167 /**
168 * Returns a list of active aliases of this command
169 *
170 * @return List of aliases
171 */
172 public List<String> getAliases() {
173 return activeAliases;
174 }
175
176 /**
177 * Gets a brief description of this command
178 *
179 * @return Description of this command
180 */
181 public String getDescription() {
182 return description;
183 }
184
185 /**
186 * Gets an example usage of this command
187 *
188 * @return One or more example usages
189 */
190 public String getUsage() {
191 return usageMessage;
192 }
193
194 /**
195 * Sets the list of aliases to request on registration for this command
196 *
197 * @param aliases Aliases to register to this command
198 * @return This command object, for linking
199 */
200 public Command setAliases(List<String> aliases) {
201 this.aliases = aliases;
202 if (!isRegistered()) {
203 this.activeAliases = new ArrayList<String>(aliases);
204 }
205 return this;
206 }
207
208 /**
209 * Sets a brief description of this command
210 *
211 * @param description New command description
212 * @return This command object, for linking
213 */
214 public Command setDescription(String description) {
215 this.description = description;
216 return this;
217 }
218
219 /**
220 * Sets the example usage of this command
221 *
222 * @param usage New example usage
223 * @return This command object, for linking
224 */
225 public Command setUsage(String usage) {
226 this.usageMessage = usage;
227 return this;
228 }
229
230 public static void broadcastCommandMessage(CommandSender source, String message) {
231 Set<Permissible> users = Bukkit.getPluginManager().getPermissionSubscriptions(Server.BROADCAST_CHANNEL_ADMINISTRATIVE);
232 String result = source.getName() + ": " + message;
233 String colored = ChatColor.GRAY + "(" + result + ")";
234
235 if (!(source instanceof ConsoleCommandSender)) {
236 source.sendMessage(message);
237 }
238
239 for (Permissible user : users) {
240 if (user instanceof CommandSender) {
241 CommandSender target = (CommandSender) user;
242
243 if (target instanceof ConsoleCommandSender) {
244 target.sendMessage(result);
245 } else if (target != source) {
246 target.sendMessage(colored);
247 }
248 }
249 }
250 }
251
252 /**
253 * Returns the current hide state of this command (Hide from console)
254 *
255 * @return True if the current command is hidden and false otherwise
256 */
257 public boolean isHidden() {
258 return isHidden;
259 }
260
261 /**
262 * Sets the current hide state of this command (Hide from console)
263 *
264 * @param hidden New hide state of this command
265 */
266 public void setHidden(boolean hidden) {
267 isHidden = hidden;
268 }
269}