PageRenderTime 87ms CodeModel.GetById 34ms app.highlight 48ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/tehcodr/util/Command.java

https://github.com/TehCodr/Personal-Chest-Shop
Java | 486 lines | 163 code | 48 blank | 275 comment | 18 complexity | cba953b2a354dcc50f3f20d2cca2af99 MD5 | raw file
  1/**  
  2 * Command.java
  3 * 
  4 * @section LICENSE
  5 * 
  6 * Permission is hereby granted, free of charge, to
  7 * any person obtaining a copy of this software and
  8 * associated documentation files (the "Software"),
  9 * to deal in the Software without restriction,
 10 * including without limitation the rights to use,
 11 * copy, modify, merge, publish, distribute, sublicense,
 12 * and/or sell copies of the Software, and to permit
 13 * persons to whom the Software is furnished to do
 14 * so, subject to the following conditions:
 15 * 
 16 * The above copyright notice and this permission notice shall be
 17 * included in all copies or substantial portions of the Software.
 18 * 
 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 22 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 24 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 26 * 
 27 */
 28
 29package com.tehcodr.util;
 30
 31import java.util.List;
 32import java.util.logging.Logger;
 33import java.rmi.NoSuchObjectException;
 34
 35import com.tehcodr.util.CommandManager;
 36
 37import org.bukkit.command.CommandSender;
 38import org.bukkit.entity.Player;
 39
 40/**
 41 * The framework for minecraft commands by TehCodr, loosely based off of sk89q's command system.
 42 * 
 43 * @author Omri Barak, TehCodr
 44 * @version 1.0
 45 */
 46public abstract class Command {
 47	
 48	/**
 49	 * For the logging of exceptions.
 50	 */
 51	protected Logger log = Logger.getLogger("Minecraft");
 52	
 53	/**
 54	 * The CommandManager that events are registered to. 
 55	 */
 56	protected CommandManager parent;
 57	
 58	/**
 59	 * Possible names for the command - name[0] is the main name of the command.
 60	 * Names past names[0] are aliases.
 61	 */
 62	protected List<String> name;
 63	
 64	/**
 65	 * Possible flags for the command.
 66	 * When called, flags already have the dash behind them.
 67	 */
 68	protected List<String> flags;
 69	
 70	/**
 71	 * Text block for how to use a command.
 72	 * Format variables -
 73	 * \$c - Command name (has forward slash)
 74	 * \$f[0] || \$f - Flag name (Index number (Array) optional).
 75	 */
 76	protected String usage;
 77	
 78	/**
 79	 * A short description of the command.
 80	 */
 81	protected String description;
 82	
 83	/**
 84	 * Minimum number of flags when calling the command.
 85	 */
 86	protected int min;
 87	
 88	/**
 89	 * Maximum number of flags when calling the command. -1 means unlimited.
 90	 */
 91	protected int max;
 92	
 93	/**
 94	 * The person that sent the command.
 95	 */
 96	protected CommandSender caller;
 97	
 98	/**
 99	 * Constructor class
100	 * 
101	 * @param parent       The CommmandManager instance used to initialize the command.
102	 * @param name         REQUIRED - Possible names for the command.
103	 * @param flags        Possible flags for the command.
104	 * @param usage        Text block for how to use a command.
105	 * @param description  A short description of the command.
106	 * @param min          Minimum number of flags when calling the command.
107	 * @param max          Maximum number of flags when calling the command.
108	 * @return true if name[0] is filled out, otherwise false.
109	 * 
110	 * @see #parent
111	 * @see #name
112	 * @see #flags
113	 * @see #usage
114	 * @see #description
115	 * @see #min
116	 * @see #max
117	 */
118	public Command(CommandManager parent, List<String> name, List<String> flags, String usage, String description, int min, int max) {
119		assert(name.indexOf((String)"") == -1);
120		setName(name);
121		setFlags(flags);
122		if(usage == "") usage = "Usage: $c <flags> \nAvailable Flags: $f[0 -1]";
123		setUsage(usage);
124		if (description == "") description = "/" + name.get(0);
125		setDesc(description);
126		setMinFlags(min);
127		setMaxFlags(max);
128		
129		try {
130			parent.register(this);
131		}
132		catch (UnactivatedManagerException e) {
133			log.info("ERROR: CommandManager has not been activate()'ed yet!");
134		}
135	}
136	
137	/**
138	 * Function to call when the slash command is typed.
139	 * 
140	 * @param caller person who called the command
141	 * @param args the flags used
142	 */
143	public abstract boolean execute(CommandSender caller, String[] args);
144	
145	/**
146	 * Gets the player that calls the given command
147	 * @return caller
148	 * @see #caller
149	 */
150	public final CommandSender getCaller() {
151		return caller;
152	}
153	
154	/**
155	 * Gets the description string from the command.
156	 * 
157	 * @return description
158	 * @see #description
159	 */
160	public final String getDescription() {
161		return description;
162	}
163	
164	/**
165	 * Gets the description string from the command. An alternative name to {@link #getDescription()}
166	 * 
167	 * @return description
168	 * @see getDescription()
169	 * @see #description
170	 */
171	public final String getDesc() {
172		return description;
173	}
174	
175	/**
176	 * Gets the name of the flag, at a certain point in the array.
177	 * 
178	 * @param i
179	 * @return flags.get(i)
180	 * @see #flags
181	 */
182	public final String getFlag(int i) {
183		return flags.get(i);
184	}
185	
186	/**
187	 * Gets all of the flags in the list flags.
188	 * 
189	 * @return flags
190	 * @see #flags
191	 */
192	public final List<String> getFlags() {
193		return flags;
194	}
195	
196	/**
197	 * Gets the flags in an array, from a beginning point/ending point, all-inclusive.
198	 * 
199	 * @param index array item number
200	 * @param first beginning index or ending index.
201	 * @return flags.subList(...) list of flags, or all if something happens. 
202	 * @see #flags
203	 */
204	public final List<String> getFlags(int index, boolean first) {
205		if(first) {
206			return flags.subList(index, flags.size() +1);
207		}
208		else if (!first) {
209			return flags.subList(0, index + 1);
210		}
211		return flags;
212	}
213	
214	/**
215	 * Gets the names in an array, from a beginning point, to an end point.
216	 * 
217	 * @param beginIndex first item in the array index
218	 * @param endIndex last item in the array index
219	 * @return flags.subList(...) list of names starting from the beginIndex, and ending at the endIndex.
220	 * @see #flags 
221	 */
222	public final List<String> getFlags(int beginIndex, int endIndex) {
223		assert(flags.size() >= beginIndex && flags.size() >= endIndex && endIndex >= beginIndex);
224		return flags.subList(beginIndex, endIndex);
225	}
226	
227	/**
228	 * Gets the logger for the game.
229	 * @return log
230	 * @see #log
231	 */
232	public final Logger getLogger() {
233		return log;
234	}
235	
236	/**
237	 * Gets the maximum amount of callable flags per command.
238	 * 
239	 * @return maximum flags allowed
240	 * @see #max
241	 */
242	public final int getMaxFlags() {
243		return max;
244	}
245	
246	/**
247	 * Gets the minimum amount of callable flags per command.
248	 * 
249	 * @return minimum flags allowed
250	 * @see #min
251	 */
252	public final int getMinFlags() {
253		return min;
254	}
255	
256	/**
257	 * Gets the command's name.
258	 * 
259	 * @return the first item in the name list
260	 * @see #name
261	 */
262	public final String getName() {
263		return name.get(0);
264	}
265	
266	/**
267	 * Gets the name of the command, at a certain point in the array.
268	 * 
269	 * @param i index of the array (list).
270	 * @return name
271	 * @see #name
272	 */
273	public final String getName(int i) {
274		return name.get(i);
275	}
276	
277	/**
278	 * Gets all of the names in the list name.
279	 * 
280	 * @return name
281	 * @see #name
282	 */
283	public final List<String> getNames() {
284		return name;
285	}
286	
287	/**
288	 * Gets the names in an array, from a beginning point/ending point, all-inclusive.
289	 * 
290	 * @param index array item number
291	 * @param first beginning index or ending index.
292	 * @return name.subList() list of names, or all if something happens.
293	 * @see #name
294	 */
295	public final List<String> getNames(int index, boolean first) {
296		if(first) {
297			return name.subList(index, name.size() +1);
298		}
299		else if(!first) {
300			return name.subList(0, index + 1);
301		}
302		return name;
303	}
304	
305	/**
306	 * Gets the names in an array, from a beginning point, to an end point.
307	 * 
308	 * @param beginIndex
309	 * @param endIndex
310	 * @return name.subList() list of names starting from the beginIndex, and ending at the endIndex.
311	 * @see #name 
312	 */
313	public final List<String> getNames(int beginIndex, int endIndex) {
314		assert(name.size() >= beginIndex && name.size() >= endIndex && endIndex >= beginIndex);
315		return name.subList(beginIndex, endIndex);
316	}
317
318	/**
319	 * Gets the CommandManager parent.
320	 * 
321	 * @return parent the CommandManager parent.
322	 * @see #parent
323	 */
324	public final CommandManager getParent() {
325		return parent;
326	}
327	
328	/**
329	 * Gets the parsed usage string.
330	 * TODO: Make a function to retrieve an unparsed usage string.
331	 * 
332	 * @return usage the usage string.
333	 * @see #usage
334	 */
335	public final String getUsage() {
336		return usage;
337	}
338	
339	/**
340	 * Parses the usage string for commands.
341	 * 
342	 * @see #usage
343	 */
344	public final void parseUsageString() {
345		/* Put flags list into an ordered list, separated by commas */
346		String FlagList = "";
347		for (int i=0; i < flags.size(); i++) {
348			FlagList += flags.get(i).concat(", ");
349		}
350		usage.replaceAll("$f", FlagList);
351		/* Flag index indexing */
352		for(int i=0; i < flags.size(); i++) {
353			usage.replaceAll("$f["+ i + "]", flags.get(i));
354		}
355		usage.replaceAll("$c", name.get(0));
356	}
357	
358	/**
359	 * 
360	 */
361	public final void printHelp() {
362		caller.sendMessage(getDesc() + "\n" + getUsage());
363	}
364	
365	/**
366	 * Register the command, if the command didn't register in the initializer.
367	 * 
368	 * @param parent the CommandManager parent that's used to register the command.
369	 */
370	public final void register(CommandManager parent) {
371		try {
372			parent.register(this);
373		}
374		catch (UnactivatedManagerException e) {
375			log.info("ERROR: CommandManager has not been activate()'ed yet!");
376		}
377	}
378	
379	/**
380	 * Register the command, if the command didn't register in the initializer.
381	 */
382	public final void register() {
383		try {
384			parent.register(this);
385		}
386		catch (UnactivatedManagerException e) {
387			log.info("ERROR: CommandManager has not been activate()'ed yet!");
388		}
389	}
390	
391	/**
392	 * Sets the Player that calls any given command.
393	 * 
394	 * @param newCaller pretty self-explanatory.
395	 * @see #caller
396	 */
397	private final void setCaller(CommandSender newCaller) {
398		caller = newCaller;
399	}
400	
401	/**
402	 * Sets the command's description.
403	 * 
404	 * @param newDesc pretty self-explanatory.
405	 * @see #description
406	 */
407	private final void setDescription(String newDesc) {
408		description = newDesc;
409	}
410	
411	private final void setDesc(String newDesc) {
412		setDescription(newDesc);
413	}
414	
415	/**
416	 * Sets the command's flags.
417	 * 
418	 * @param newFlags pretty self-explanatory.
419	 * @see #flags
420	 */
421	private final void setFlags(List<String> newFlags) {
422		flags = newFlags;
423	}
424	
425	/**
426	 * Sets the logger for the game.
427	 * 
428	 * @param newLog
429	 * @see #log
430	 */
431	private final void setLogger(Logger newLog) {
432		log = newLog;
433	}
434	
435	/**
436	 * Sets the maximum allowed callable flags.
437	 * 
438	 * @param newMax pretty self-explanatory.
439	 * @see #max
440	 */
441	private final void setMaxFlags(int newMax) {
442		max = newMax;
443	}
444	
445	/**
446	 * Sets the minimum allowed callable flags.
447	 * 
448	 * @param newMin pretty self-explanatory.
449	 * @see #min
450	 */
451	private final void setMinFlags(int newMin) {
452		min = newMin;
453	}
454	
455	/**
456	 * Sets the command's name.
457	 * 
458	 * @param newName pretty self-explanatory.
459	 * @see #name
460	 */
461	private final void setName(List<String> newName) {
462		assert(newName.indexOf((String)"") == -1);
463		name = newName;
464	}
465	
466	/**
467	 * Sets the CommandManager parent.
468	 * 
469	 * @param newParent pretty self-explanatory.
470	 * @see #parent
471	 */
472	private final void setParent(CommandManager newParent) {
473		parent = newParent;
474	}
475	
476	/**
477	 * Sets the command's usage string.
478	 * 
479	 * @param newUsage pretty self-explanatory.
480	 * @see #usage
481	 */
482	private final void setUsage(String newUsage) {
483		usage = newUsage;
484		parseUsageString();
485	}
486}