/src/ServerConsoleCommands.java

https://github.com/talmor/CanaryMod · Java · 280 lines · 222 code · 33 blank · 25 comment · 65 complexity · 2b601bd9c92a7697baa66b150b655fa6 MD5 · raw file

  1. import java.util.LinkedHashMap;
  2. import java.util.logging.Logger;
  3. public class ServerConsoleCommands {
  4. private static final Logger log = Logger.getLogger("Minecraft");
  5. private static ServerConsoleCommands instance;
  6. private final LinkedHashMap<String, BaseCommand> commands = new LinkedHashMap<String, BaseCommand>();
  7. private ServerConsoleCommands() {
  8. add("reload", reload);
  9. add("listplugins", listplugins);
  10. add("enableplugin", enableplugin);
  11. add("reloadplugin", reloadplugin);
  12. add("disableplugin", disableplugin);
  13. add("modify", modify);
  14. add("mp", modify);
  15. add("reservelist", reservelist);
  16. add("whitelist", whitelist);
  17. add("version", version);
  18. }
  19. /**
  20. * Add a command to the server list.
  21. *
  22. * @param name
  23. * @param cmd
  24. */
  25. public void add(String name, BaseCommand cmd) {
  26. if (name != null && cmd != null) {
  27. if (!commands.containsValue(cmd))
  28. etc.getInstance().addCommand("/" + name, cmd.tooltip);
  29. commands.put(name, cmd);
  30. }
  31. }
  32. /**
  33. * Remove a command from the server list.
  34. *
  35. * @param name
  36. */
  37. public void remove(String name) {
  38. if (name != null) {
  39. etc.getInstance().removeCommand(name);
  40. commands.remove(name);
  41. }
  42. }
  43. /**
  44. * Performs a lookup for a command of the given name and executes it if
  45. * found. Returns false if command not found.
  46. *
  47. * @param command
  48. * @param caller
  49. * @param args
  50. * @return
  51. */
  52. public static boolean parseServerConsoleCommand(MessageReceiver caller, String command, String[] args) {
  53. if (instance == null)
  54. instance = new ServerConsoleCommands();
  55. BaseCommand cmd = instance.getCommand(command);
  56. if (cmd != null) {
  57. cmd.parseCommand(caller, args);
  58. // Inform caller a matching command was found.
  59. return true;
  60. }
  61. return false;
  62. }
  63. public BaseCommand getCommand(String command) {
  64. return commands.get(command);
  65. }
  66. public static final BaseCommand reload = new BaseCommand("- Reloads CanaryMod") {
  67. @Override
  68. void execute(MessageReceiver caller, String[] parameters) {
  69. etc.getInstance().load();
  70. etc.getInstance().loadData();
  71. for (Player p : etc.getServer().getPlayerList())
  72. p.getUser().reloadPlayer();
  73. log.info("CanaryMod reloaded by " + caller.getName());
  74. caller.notify("Successfully reloaded config");
  75. }
  76. };
  77. public static final BaseCommand modify = new BaseCommand("[player] [key] [value] - Type /modify for more info", "Overriden onBadSyntax", 3) {
  78. @Override
  79. void execute(MessageReceiver caller, String[] parameters) {
  80. if (parameters.length > 2 && parameters[2].contains(":")) {
  81. for (int i = 3; i < parameters.length; i++)
  82. if (!parameters[i].contains(":")) {
  83. onBadSyntax(caller, null);
  84. return;
  85. }
  86. Player player = etc.getServer().matchPlayer(parameters[1]);
  87. if (player == null) {
  88. caller.notify("Player does not exist.");
  89. return;
  90. }
  91. for (int i = 2; i < parameters.length; i++) {
  92. if (parameters[i].split(":").length != 2) {
  93. caller.notify("This key:value pair is deformed... " + parameters[i]);
  94. return;
  95. }
  96. String key = parameters[i].split(":")[0];
  97. String value = parameters[i].split(":")[1];
  98. boolean newUser = false;
  99. if (!etc.getDataSource().doesPlayerExist(player.getName())) {
  100. if (!key.equalsIgnoreCase("groups") && !key.equalsIgnoreCase("g")) {
  101. caller.notify("When adding a new user, set their group(s) first.");
  102. return;
  103. }
  104. caller.notify("Adding new user.");
  105. newUser = true;
  106. player.setCanModifyWorld(true);
  107. }
  108. updatePlayerValues(player, key, value);
  109. saveChanges(player, newUser);
  110. log.info("Modifed user " + parameters[1] + ". " + key + " => " + value + " by " + caller.getName());
  111. }
  112. caller.notify("Modified user.");
  113. } else {
  114. if (parameters.length < 4) {
  115. onBadSyntax(caller, null);
  116. return;
  117. }
  118. Player player = etc.getServer().matchPlayer(parameters[1]);
  119. if (player == null) {
  120. caller.notify("Player does not exist.");
  121. return;
  122. }
  123. String key = parameters[2];
  124. String value = parameters[3];
  125. boolean newUser = false;
  126. if (!etc.getDataSource().doesPlayerExist(player.getName())) {
  127. if (!key.equalsIgnoreCase("groups") && !key.equalsIgnoreCase("g")) {
  128. caller.notify("When adding a new user, set their group(s) first.");
  129. return;
  130. }
  131. caller.notify("Adding new user.");
  132. newUser = true;
  133. }
  134. updatePlayerValues(player, key, value);
  135. saveChanges(player, newUser);
  136. caller.notify("Modified user.");
  137. // Send to server
  138. // log too,
  139. // regardless of
  140. // caller.
  141. log.info("Modifed user " + parameters[1] + ". " + key + " => " + value + " by " + caller.getName());
  142. }
  143. }
  144. private void saveChanges(Player player, boolean newUser) {
  145. if (newUser)
  146. etc.getDataSource().addPlayer(player);
  147. else
  148. etc.getDataSource().modifyPlayer(player);
  149. }
  150. private void updatePlayerValues(Player player, String key, String value) {
  151. if (key.equalsIgnoreCase("prefix") || key.equalsIgnoreCase("p"))
  152. player.setPrefix(value);
  153. else if (key.equalsIgnoreCase("commands") || key.equalsIgnoreCase("c"))
  154. player.setCommands(value.split(","));
  155. else if (key.equalsIgnoreCase("groups") || key.equalsIgnoreCase("g"))
  156. player.setGroups(value.split(","));
  157. else if (key.equalsIgnoreCase("ignoresrestrictions") || key.equalsIgnoreCase("ir"))
  158. player.setIgnoreRestrictions(value.equalsIgnoreCase("true") || value.equals("1"));
  159. else if (key.equalsIgnoreCase("admin") || key.equalsIgnoreCase("a"))
  160. player.setAdmin(value.equalsIgnoreCase("true") || value.equals("1"));
  161. else if (key.equalsIgnoreCase("modworld") || key.equalsIgnoreCase("mw"))
  162. player.setCanModifyWorld(value.equalsIgnoreCase("true") || value.equals("1"));
  163. }
  164. @Override
  165. public void onBadSyntax(MessageReceiver caller, String[] params) {
  166. caller.notify("Usage is: /modify [player] [key] [value]");
  167. caller.notify("Keys:");
  168. caller.notify("prefix: only the letter the color represents");
  169. caller.notify("commands: list seperated by comma");
  170. caller.notify("groups: list seperated by comma");
  171. caller.notify("ignoresrestrictions: true or false");
  172. caller.notify("admin: true or false");
  173. caller.notify("modworld: true or false");
  174. }
  175. };
  176. public final static BaseCommand whitelist = new BaseCommand("[operation (add or remove)] [player]", "whitelist [operation (toggle, add or remove)] <player>", 2) {
  177. @Override
  178. void execute(MessageReceiver caller, String[] parameters) {
  179. if (parameters[1].equalsIgnoreCase("toggle"))
  180. caller.notify((etc.getInstance().toggleWhitelist() ? "Whitelist enabled" : "Whitelist disabled"));
  181. else if (parameters.length == 3) {
  182. if (parameters[1].equalsIgnoreCase("add")) {
  183. etc.getDataSource().addToWhitelist(parameters[2]);
  184. caller.notify(parameters[2] + " added to whitelist");
  185. } else if (parameters[1].equalsIgnoreCase("remove")) {
  186. etc.getDataSource().removeFromWhitelist(parameters[2]);
  187. caller.notify(parameters[2] + " removed from whitelist");
  188. } else
  189. caller.notify("Invalid operation.");
  190. } else
  191. caller.notify("Invalid operation.");
  192. }
  193. };
  194. public final static BaseCommand reservelist = new BaseCommand("[operation (add or remove)] [player]", "reservelist [operation (add or remove)] [player]", 3, 3) {
  195. @Override
  196. void execute(MessageReceiver caller, String[] parameters) {
  197. if (parameters[1].equalsIgnoreCase("add")) {
  198. etc.getDataSource().addToReserveList(parameters[2]);
  199. caller.notify(parameters[2] + " added to reservelist");
  200. } else if (parameters[1].equalsIgnoreCase("remove")) {
  201. etc.getDataSource().removeFromReserveList(parameters[2]);
  202. caller.notify(parameters[2] + " removed from reservelist");
  203. } else
  204. caller.notify("Invalid operation.");
  205. }
  206. };
  207. public final static BaseCommand listplugins = new BaseCommand("- Lists all plugins") {
  208. @Override
  209. void execute(MessageReceiver caller, String[] parameters) {
  210. caller.notify("Plugins" + Colors.White + ": " + etc.getLoader().getPluginList());
  211. }
  212. };
  213. public final static BaseCommand reloadplugin = new BaseCommand("[plugin] - Reloads plugin", "Correct usage is: /reloadplugin [plugin]", 2) {
  214. @Override
  215. void execute(MessageReceiver caller, String[] parameters) {
  216. if (etc.getLoader().reloadPlugin(parameters[1]))
  217. caller.notify("Plugin reloaded.");
  218. else
  219. caller.notify("Unable to reload plugin. Check capitalization and/or server logfile.");
  220. }
  221. };
  222. public final static BaseCommand enableplugin = new BaseCommand("[plugin] - Enables plugin", "Correct usage is: /enableplugin [plugin]", 2) {
  223. @Override
  224. void execute(MessageReceiver caller, String[] parameters) {
  225. if (etc.getLoader().enablePlugin(parameters[1]))
  226. caller.notify("Plugin enabled.");
  227. else
  228. caller.notify("Unable to enable plugin. Check capitalization and/or server logfile.");
  229. }
  230. };
  231. public final static BaseCommand disableplugin = new BaseCommand("[plugin] - Disables plugin", "Correct usage is: /disableplugin [plugin]", 2) {
  232. @Override
  233. void execute(MessageReceiver caller, String[] parameters) {
  234. etc.getLoader().disablePlugin(parameters[1]);
  235. caller.notify("Plugin disabled.");
  236. }
  237. };
  238. public final static BaseCommand version = new BaseCommand("- Displays the server version") {
  239. @Override
  240. void execute(MessageReceiver caller, String[] parameters) {
  241. if (!etc.getInstance().getTainted())
  242. caller.notify(Colors.Gold + "CanaryMod Build " + etc.getInstance().getVersion());
  243. else
  244. caller.notify(Colors.Gold + "Unofficial CanaryMod Build " + etc.getInstance().getVersionStr());
  245. }
  246. };
  247. }