PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/me/coolblinger/remoteadmin/RemoteAdmin.java

https://github.com/coolblinger/RemoteAdmin
Java | 249 lines | 175 code | 14 blank | 60 comment | 41 complexity | 0c02842ba539d2f0d794dafde7794f3f MD5 | raw file
  1. package me.coolblinger.remoteadmin;
  2. import me.coolblinger.remoteadmin.listeners.RemoteAdminBlockListener;
  3. import me.coolblinger.remoteadmin.listeners.RemoteAdminPlayerListener;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.command.Command;
  6. import org.bukkit.command.CommandSender;
  7. import org.bukkit.event.Event;
  8. import org.bukkit.plugin.PluginManager;
  9. import org.bukkit.plugin.java.JavaPlugin;
  10. import org.bukkit.util.config.Configuration;
  11. import sun.misc.BASE64Encoder;
  12. import java.io.UnsupportedEncodingException;
  13. import java.security.MessageDigest;
  14. import java.security.NoSuchAlgorithmException;
  15. import java.util.logging.Logger;
  16. /**
  17. * <strong>Note:</strong><br />
  18. * Please excuse me for the somewhat broken English in the
  19. * javadoc stubs.
  20. */
  21. public class RemoteAdmin extends JavaPlugin {
  22. public final Logger log = Logger.getLogger("Minecraft");
  23. final RemoteAdminServer server = new RemoteAdminServer(this);
  24. private final RemoteAdminPlayerListener playerlistener = new RemoteAdminPlayerListener(this);
  25. private final RemoteAdminBlockListener blockListener = new RemoteAdminBlockListener(this);
  26. public void onDisable() {
  27. server.stopServer();
  28. }
  29. public void onEnable() {
  30. configuration();
  31. PluginManager pm = this.getServer().getPluginManager();
  32. pm.registerEvent(Event.Type.PLAYER_CHAT, playerlistener, Event.Priority.Monitor, this);
  33. pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerlistener, Event.Priority.Monitor, this);
  34. pm.registerEvent(Event.Type.PLAYER_INTERACT, playerlistener, Event.Priority.Monitor, this);
  35. pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener, Event.Priority.Monitor, this);
  36. pm.registerEvent(Event.Type.BLOCK_PLACE, blockListener, Event.Priority.Monitor, this);
  37. pm.registerEvent(Event.Type.PLAYER_JOIN, playerlistener, Event.Priority.Monitor, this);
  38. pm.registerEvent(Event.Type.PLAYER_QUIT, playerlistener, Event.Priority.Monitor, this);
  39. server.start();
  40. }
  41. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  42. if (command.getName().equals("ra")) {
  43. if (sender.hasPermission("remoteadmin.manage")) {
  44. if (!(sender instanceof Admin)) {
  45. if (args.length == 0) {
  46. sender.sendMessage(ChatColor.GOLD + "RemoteAdmin");
  47. sender.sendMessage(ChatColor.GOLD + "--------------");
  48. sender.sendMessage(ChatColor.AQUA + "/ra add <username> <password>" + ChatColor.WHITE + " - " + ChatColor.GOLD + "Register a new account");
  49. sender.sendMessage(ChatColor.AQUA + "/ra remove <username>" + ChatColor.WHITE + " - " + ChatColor.GOLD + "Remove an account");
  50. sender.sendMessage(ChatColor.AQUA + "/ra changepass <username> <newpass>" + ChatColor.WHITE + " - " + ChatColor.GOLD + "Change an account's password");
  51. } else {
  52. if (args[0].equalsIgnoreCase("add")) {
  53. if (args.length >= 3) {
  54. if (createAccount(args[1], args[2])) {
  55. sender.sendMessage(ChatColor.GREEN + "Account successfully created.");
  56. } else {
  57. sender.sendMessage(ChatColor.RED + "An account with that name already exists.");
  58. }
  59. } else {
  60. sender.sendMessage(ChatColor.RED + "Please specify username and password.");
  61. }
  62. } else if (args[0].equalsIgnoreCase("remove")) {
  63. if (args.length >= 2) {
  64. if (removeAccount(args[1])) {
  65. sender.sendMessage(ChatColor.GREEN + "Account removed successfully.");
  66. } else {
  67. sender.sendMessage(ChatColor.RED + "The specified account does not exist.");
  68. }
  69. } else {
  70. sender.sendMessage(ChatColor.RED + "Please specify the name of the account you wish to remove.");
  71. }
  72. } else if (args[0].equalsIgnoreCase("changepass")) {
  73. if (args.length >= 3) {
  74. if (changePassword(args[1], args[2], false)) {
  75. sender.sendMessage(ChatColor.GREEN + "Password changed successfully.");
  76. } else {
  77. sender.sendMessage(ChatColor.RED + "The specified account does not exist.");
  78. }
  79. } else {
  80. sender.sendMessage(ChatColor.RED + "Please specify an username and a new password.");
  81. }
  82. } else {
  83. sender.sendMessage(ChatColor.GOLD + "RemoteAdmin");
  84. sender.sendMessage(ChatColor.GOLD + "--------------");
  85. sender.sendMessage(ChatColor.AQUA + "/ra add <username> <password>" + ChatColor.WHITE + " - " + ChatColor.GOLD + "Register a new account");
  86. sender.sendMessage(ChatColor.AQUA + "/ra remove <username>" + ChatColor.WHITE + " - " + ChatColor.GOLD + "Remove an account");
  87. sender.sendMessage(ChatColor.AQUA + "/ra changepass <username> <newpass>" + ChatColor.WHITE + " - " + ChatColor.GOLD + "Change an account's password");
  88. }
  89. }
  90. } else {
  91. sender.sendMessage("You can only manage account using the console or in-game.");
  92. }
  93. } else {
  94. sender.sendMessage(ChatColor.RED + "You don't have permission to do this.");
  95. }
  96. return true;
  97. }
  98. return false;
  99. }
  100. /**
  101. * Other plugins can send data to the clients using this method.
  102. *
  103. * @param s The string that will be send.
  104. */
  105. public void send(String s) {
  106. server.list.add(s);
  107. }
  108. /**
  109. * This method will put some settings in the <code>config.yml</code> file.
  110. */
  111. private void configuration() {
  112. Configuration config = getConfiguration();
  113. config.load();
  114. config.setHeader("#Throttle is the 'speed' of the server,\n#the server will respond faster with lower\n#throttles, but will use more CPU.");
  115. if (config.getProperty("port") == null) {
  116. config.setProperty("port", 7001);
  117. config.save();
  118. }
  119. if (config.getProperty("throttle") == null) {
  120. config.setProperty("throttle", 150);
  121. config.save();
  122. }
  123. }
  124. /**
  125. * This will return the value of the node <code>path</code>.
  126. *
  127. * @param path The location of the node.
  128. * @return The value of the node <code>path</code>.
  129. */
  130. Object getConfig(String path) {
  131. Configuration config = getConfiguration();
  132. config.load();
  133. return config.getProperty(path);
  134. }
  135. /**
  136. * This method will create an account with username <code>user</code>
  137. * and password <code>pass</code>.
  138. *
  139. * @param user The username of the account.
  140. * @param pass The password of the account, encrypted in MD5 format.
  141. * @return Whether the account has been created or not. Will return <code>false</code>
  142. * if an account with username <code>user</code> already exists.
  143. */
  144. boolean createAccount(String user, String pass) {
  145. Configuration config = getConfiguration();
  146. config.load();
  147. if (config.getProperty("accounts." + user) == null) {
  148. config.setProperty("accounts." + user, encrypt(pass));
  149. config.save();
  150. return true;
  151. }
  152. return false;
  153. }
  154. /**
  155. * This method will try to remove the account with username <code>user</code>.
  156. *
  157. * @param user The username of the account.
  158. * @return Whether the account has been deleted or not. Will return <code>false</code>
  159. * if the account does not exist.
  160. */
  161. boolean removeAccount(String user) {
  162. Configuration config = getConfiguration();
  163. config.load();
  164. if (config.getProperty("accounts." + user) != null) {
  165. config.removeProperty("accounts." + user);
  166. config.save();
  167. return true;
  168. }
  169. return false;
  170. }
  171. /**
  172. * This method will try to change the password of account <code>user</code> to
  173. * <code>newpass</code>.
  174. *
  175. * @param user The username of the account.
  176. * @param newPass The new password, encrypted in MD5 format.
  177. * @param preEncrypted The password won't be encrypted if <code>preEncrypted</code> is true.
  178. * @return Whethher the password change was successfull or not, will return
  179. * <code>false</code> if the account does not exist.
  180. */
  181. boolean changePassword(String user, String newPass, boolean preEncrypted) {
  182. Configuration config = getConfiguration();
  183. config.load();
  184. if (config.getProperty("accounts." + user) != null) {
  185. if (preEncrypted) {
  186. config.setProperty("accounts." + user, newPass);
  187. } else {
  188. config.setProperty("accounts." + user, encrypt(newPass));
  189. }
  190. config.save();
  191. return true;
  192. }
  193. return false;
  194. }
  195. /**
  196. * Will check whether an account with username <code>user</code> and
  197. * password <code>pass</code> exists. Used for loggin in.
  198. *
  199. * @param user The username of the account.
  200. * @param pass The password of the account, encrypted in MD5 format.
  201. * @return Whether an account with username <code>user</code> and password
  202. * <code>pass</code> exists.
  203. */
  204. boolean accountCheck(String user, String pass) {
  205. Configuration config = getConfiguration();
  206. config.load();
  207. if (config.getProperty("accounts." + user) != null) {
  208. if (config.getProperty("accounts." + user).equals(pass)) {
  209. return true;
  210. }
  211. }
  212. return false;
  213. }
  214. /**
  215. * This will encrypt the string <code>unencrypted</code> in MD5 format.
  216. *
  217. * @param unencrypted The unencrypted string.
  218. * @return <code>unencrypted</code> encrypted in MD5 format.
  219. */
  220. String encrypt(String unencrypted) {
  221. MessageDigest md = null;
  222. try {
  223. md = MessageDigest.getInstance("MD5");
  224. } catch (NoSuchAlgorithmException e) {
  225. e.printStackTrace();
  226. }
  227. try {
  228. md.update(unencrypted.getBytes("UTF-8"));
  229. } catch (UnsupportedEncodingException e) {
  230. e.printStackTrace();
  231. }
  232. byte[] bytes = md.digest();
  233. return (new BASE64Encoder()).encode(bytes);
  234. }
  235. }