PageRenderTime 1017ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ServerConsoleCommands.java

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