PageRenderTime 58ms CodeModel.GetById 31ms 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. package com.tehcodr.util;
  29. import java.util.List;
  30. import java.util.logging.Logger;
  31. import java.rmi.NoSuchObjectException;
  32. import com.tehcodr.util.CommandManager;
  33. import org.bukkit.command.CommandSender;
  34. import org.bukkit.entity.Player;
  35. /**
  36. * The framework for minecraft commands by TehCodr, loosely based off of sk89q's command system.
  37. *
  38. * @author Omri Barak, TehCodr
  39. * @version 1.0
  40. */
  41. public abstract class Command {
  42. /**
  43. * For the logging of exceptions.
  44. */
  45. protected Logger log = Logger.getLogger("Minecraft");
  46. /**
  47. * The CommandManager that events are registered to.
  48. */
  49. protected CommandManager parent;
  50. /**
  51. * Possible names for the command - name[0] is the main name of the command.
  52. * Names past names[0] are aliases.
  53. */
  54. protected List<String> name;
  55. /**
  56. * Possible flags for the command.
  57. * When called, flags already have the dash behind them.
  58. */
  59. protected List<String> flags;
  60. /**
  61. * Text block for how to use a command.
  62. * Format variables -
  63. * \$c - Command name (has forward slash)
  64. * \$f[0] || \$f - Flag name (Index number (Array) optional).
  65. */
  66. protected String usage;
  67. /**
  68. * A short description of the command.
  69. */
  70. protected String description;
  71. /**
  72. * Minimum number of flags when calling the command.
  73. */
  74. protected int min;
  75. /**
  76. * Maximum number of flags when calling the command. -1 means unlimited.
  77. */
  78. protected int max;
  79. /**
  80. * The person that sent the command.
  81. */
  82. protected CommandSender caller;
  83. /**
  84. * Constructor class
  85. *
  86. * @param parent The CommmandManager instance used to initialize the command.
  87. * @param name REQUIRED - Possible names for the command.
  88. * @param flags Possible flags for the command.
  89. * @param usage Text block for how to use a command.
  90. * @param description A short description of the command.
  91. * @param min Minimum number of flags when calling the command.
  92. * @param max Maximum number of flags when calling the command.
  93. * @return true if name[0] is filled out, otherwise false.
  94. *
  95. * @see #parent
  96. * @see #name
  97. * @see #flags
  98. * @see #usage
  99. * @see #description
  100. * @see #min
  101. * @see #max
  102. */
  103. public Command(CommandManager parent, List<String> name, List<String> flags, String usage, String description, int min, int max) {
  104. assert(name.indexOf((String)"") == -1);
  105. setName(name);
  106. setFlags(flags);
  107. if(usage == "") usage = "Usage: $c <flags> \nAvailable Flags: $f[0 -1]";
  108. setUsage(usage);
  109. if (description == "") description = "/" + name.get(0);
  110. setDesc(description);
  111. setMinFlags(min);
  112. setMaxFlags(max);
  113. try {
  114. parent.register(this);
  115. }
  116. catch (UnactivatedManagerException e) {
  117. log.info("ERROR: CommandManager has not been activate()'ed yet!");
  118. }
  119. }
  120. /**
  121. * Function to call when the slash command is typed.
  122. *
  123. * @param caller person who called the command
  124. * @param args the flags used
  125. */
  126. public abstract boolean execute(CommandSender caller, String[] args);
  127. /**
  128. * Gets the player that calls the given command
  129. * @return caller
  130. * @see #caller
  131. */
  132. public final CommandSender getCaller() {
  133. return caller;
  134. }
  135. /**
  136. * Gets the description string from the command.
  137. *
  138. * @return description
  139. * @see #description
  140. */
  141. public final String getDescription() {
  142. return description;
  143. }
  144. /**
  145. * Gets the description string from the command. An alternative name to {@link #getDescription()}
  146. *
  147. * @return description
  148. * @see getDescription()
  149. * @see #description
  150. */
  151. public final String getDesc() {
  152. return description;
  153. }
  154. /**
  155. * Gets the name of the flag, at a certain point in the array.
  156. *
  157. * @param i
  158. * @return flags.get(i)
  159. * @see #flags
  160. */
  161. public final String getFlag(int i) {
  162. return flags.get(i);
  163. }
  164. /**
  165. * Gets all of the flags in the list flags.
  166. *
  167. * @return flags
  168. * @see #flags
  169. */
  170. public final List<String> getFlags() {
  171. return flags;
  172. }
  173. /**
  174. * Gets the flags in an array, from a beginning point/ending point, all-inclusive.
  175. *
  176. * @param index array item number
  177. * @param first beginning index or ending index.
  178. * @return flags.subList(...) list of flags, or all if something happens.
  179. * @see #flags
  180. */
  181. public final List<String> getFlags(int index, boolean first) {
  182. if(first) {
  183. return flags.subList(index, flags.size() +1);
  184. }
  185. else if (!first) {
  186. return flags.subList(0, index + 1);
  187. }
  188. return flags;
  189. }
  190. /**
  191. * Gets the names in an array, from a beginning point, to an end point.
  192. *
  193. * @param beginIndex first item in the array index
  194. * @param endIndex last item in the array index
  195. * @return flags.subList(...) list of names starting from the beginIndex, and ending at the endIndex.
  196. * @see #flags
  197. */
  198. public final List<String> getFlags(int beginIndex, int endIndex) {
  199. assert(flags.size() >= beginIndex && flags.size() >= endIndex && endIndex >= beginIndex);
  200. return flags.subList(beginIndex, endIndex);
  201. }
  202. /**
  203. * Gets the logger for the game.
  204. * @return log
  205. * @see #log
  206. */
  207. public final Logger getLogger() {
  208. return log;
  209. }
  210. /**
  211. * Gets the maximum amount of callable flags per command.
  212. *
  213. * @return maximum flags allowed
  214. * @see #max
  215. */
  216. public final int getMaxFlags() {
  217. return max;
  218. }
  219. /**
  220. * Gets the minimum amount of callable flags per command.
  221. *
  222. * @return minimum flags allowed
  223. * @see #min
  224. */
  225. public final int getMinFlags() {
  226. return min;
  227. }
  228. /**
  229. * Gets the command's name.
  230. *
  231. * @return the first item in the name list
  232. * @see #name
  233. */
  234. public final String getName() {
  235. return name.get(0);
  236. }
  237. /**
  238. * Gets the name of the command, at a certain point in the array.
  239. *
  240. * @param i index of the array (list).
  241. * @return name
  242. * @see #name
  243. */
  244. public final String getName(int i) {
  245. return name.get(i);
  246. }
  247. /**
  248. * Gets all of the names in the list name.
  249. *
  250. * @return name
  251. * @see #name
  252. */
  253. public final List<String> getNames() {
  254. return name;
  255. }
  256. /**
  257. * Gets the names in an array, from a beginning point/ending point, all-inclusive.
  258. *
  259. * @param index array item number
  260. * @param first beginning index or ending index.
  261. * @return name.subList() list of names, or all if something happens.
  262. * @see #name
  263. */
  264. public final List<String> getNames(int index, boolean first) {
  265. if(first) {
  266. return name.subList(index, name.size() +1);
  267. }
  268. else if(!first) {
  269. return name.subList(0, index + 1);
  270. }
  271. return name;
  272. }
  273. /**
  274. * Gets the names in an array, from a beginning point, to an end point.
  275. *
  276. * @param beginIndex
  277. * @param endIndex
  278. * @return name.subList() list of names starting from the beginIndex, and ending at the endIndex.
  279. * @see #name
  280. */
  281. public final List<String> getNames(int beginIndex, int endIndex) {
  282. assert(name.size() >= beginIndex && name.size() >= endIndex && endIndex >= beginIndex);
  283. return name.subList(beginIndex, endIndex);
  284. }
  285. /**
  286. * Gets the CommandManager parent.
  287. *
  288. * @return parent the CommandManager parent.
  289. * @see #parent
  290. */
  291. public final CommandManager getParent() {
  292. return parent;
  293. }
  294. /**
  295. * Gets the parsed usage string.
  296. * TODO: Make a function to retrieve an unparsed usage string.
  297. *
  298. * @return usage the usage string.
  299. * @see #usage
  300. */
  301. public final String getUsage() {
  302. return usage;
  303. }
  304. /**
  305. * Parses the usage string for commands.
  306. *
  307. * @see #usage
  308. */
  309. public final void parseUsageString() {
  310. /* Put flags list into an ordered list, separated by commas */
  311. String FlagList = "";
  312. for (int i=0; i < flags.size(); i++) {
  313. FlagList += flags.get(i).concat(", ");
  314. }
  315. usage.replaceAll("$f", FlagList);
  316. /* Flag index indexing */
  317. for(int i=0; i < flags.size(); i++) {
  318. usage.replaceAll("$f["+ i + "]", flags.get(i));
  319. }
  320. usage.replaceAll("$c", name.get(0));
  321. }
  322. /**
  323. *
  324. */
  325. public final void printHelp() {
  326. caller.sendMessage(getDesc() + "\n" + getUsage());
  327. }
  328. /**
  329. * Register the command, if the command didn't register in the initializer.
  330. *
  331. * @param parent the CommandManager parent that's used to register the command.
  332. */
  333. public final void register(CommandManager parent) {
  334. try {
  335. parent.register(this);
  336. }
  337. catch (UnactivatedManagerException e) {
  338. log.info("ERROR: CommandManager has not been activate()'ed yet!");
  339. }
  340. }
  341. /**
  342. * Register the command, if the command didn't register in the initializer.
  343. */
  344. public final void register() {
  345. try {
  346. parent.register(this);
  347. }
  348. catch (UnactivatedManagerException e) {
  349. log.info("ERROR: CommandManager has not been activate()'ed yet!");
  350. }
  351. }
  352. /**
  353. * Sets the Player that calls any given command.
  354. *
  355. * @param newCaller pretty self-explanatory.
  356. * @see #caller
  357. */
  358. private final void setCaller(CommandSender newCaller) {
  359. caller = newCaller;
  360. }
  361. /**
  362. * Sets the command's description.
  363. *
  364. * @param newDesc pretty self-explanatory.
  365. * @see #description
  366. */
  367. private final void setDescription(String newDesc) {
  368. description = newDesc;
  369. }
  370. private final void setDesc(String newDesc) {
  371. setDescription(newDesc);
  372. }
  373. /**
  374. * Sets the command's flags.
  375. *
  376. * @param newFlags pretty self-explanatory.
  377. * @see #flags
  378. */
  379. private final void setFlags(List<String> newFlags) {
  380. flags = newFlags;
  381. }
  382. /**
  383. * Sets the logger for the game.
  384. *
  385. * @param newLog
  386. * @see #log
  387. */
  388. private final void setLogger(Logger newLog) {
  389. log = newLog;
  390. }
  391. /**
  392. * Sets the maximum allowed callable flags.
  393. *
  394. * @param newMax pretty self-explanatory.
  395. * @see #max
  396. */
  397. private final void setMaxFlags(int newMax) {
  398. max = newMax;
  399. }
  400. /**
  401. * Sets the minimum allowed callable flags.
  402. *
  403. * @param newMin pretty self-explanatory.
  404. * @see #min
  405. */
  406. private final void setMinFlags(int newMin) {
  407. min = newMin;
  408. }
  409. /**
  410. * Sets the command's name.
  411. *
  412. * @param newName pretty self-explanatory.
  413. * @see #name
  414. */
  415. private final void setName(List<String> newName) {
  416. assert(newName.indexOf((String)"") == -1);
  417. name = newName;
  418. }
  419. /**
  420. * Sets the CommandManager parent.
  421. *
  422. * @param newParent pretty self-explanatory.
  423. * @see #parent
  424. */
  425. private final void setParent(CommandManager newParent) {
  426. parent = newParent;
  427. }
  428. /**
  429. * Sets the command's usage string.
  430. *
  431. * @param newUsage pretty self-explanatory.
  432. * @see #usage
  433. */
  434. private final void setUsage(String newUsage) {
  435. usage = newUsage;
  436. parseUsageString();
  437. }
  438. }