McremoveCommand.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.gmail.nossr50.database.commands;
  2. import java.io.BufferedReader;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import org.bukkit.command.Command;
  7. import org.bukkit.command.CommandExecutor;
  8. import org.bukkit.command.CommandSender;
  9. import com.gmail.nossr50.mcMMO;
  10. import com.gmail.nossr50.commands.CommandHelper;
  11. import com.gmail.nossr50.config.Config;
  12. import com.gmail.nossr50.database.Database;
  13. import com.gmail.nossr50.locale.LocaleLoader;
  14. public class McremoveCommand implements CommandExecutor {
  15. @Override
  16. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  17. String playerName;
  18. String tablePrefix = Config.getInstance().getMySQLTablePrefix();
  19. //String databaseName = Config.getInstance().getMySQLDatabaseName();
  20. String usage = LocaleLoader.getString("Commands.Usage.1", "mcremove", "<" + LocaleLoader.getString("Commands.Usage.Player") + ">");
  21. String success;
  22. if (CommandHelper.noCommandPermissions(sender, "mcmmo.commands.mcremove")) {
  23. return true;
  24. }
  25. switch (args.length) {
  26. case 1:
  27. playerName = args[0];
  28. success = LocaleLoader.getString("Commands.mcremove.Success", playerName);
  29. break;
  30. default:
  31. sender.sendMessage(usage);
  32. return true;
  33. }
  34. /* MySQL */
  35. if (Config.getInstance().getUseMySQL()) {
  36. int affected = 0;
  37. affected = Database.update("DELETE FROM " + tablePrefix + "users WHERE " + tablePrefix + "users.user = '" + playerName + "'");
  38. if (affected > 0) {
  39. sender.sendMessage(success);
  40. } else {
  41. sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist"));
  42. }
  43. }
  44. else {
  45. if (removeFlatFileUser(playerName)) {
  46. sender.sendMessage(success);
  47. }
  48. else {
  49. sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist"));
  50. }
  51. }
  52. Database.profileCleanup(playerName);
  53. return true;
  54. }
  55. private boolean removeFlatFileUser(String playerName) {
  56. boolean worked = false;
  57. BufferedReader in = null;
  58. FileWriter out = null;
  59. String usersFilePath = mcMMO.getUsersFilePath();
  60. try {
  61. FileReader file = new FileReader(usersFilePath);
  62. in = new BufferedReader(file);
  63. StringBuilder writer = new StringBuilder();
  64. String line = "";
  65. while ((line = in.readLine()) != null) {
  66. /* Write out the same file but when we get to the player we want to remove, we skip his line. */
  67. if (!line.split(":")[0].equalsIgnoreCase(playerName)) {
  68. writer.append(line).append("\r\n");
  69. }
  70. else {
  71. System.out.println("User found, removing...");
  72. worked = true;
  73. continue; //Skip the player
  74. }
  75. }
  76. out = new FileWriter(usersFilePath); //Write out the new file
  77. out.write(writer.toString());
  78. }
  79. catch (Exception e) {
  80. mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
  81. }
  82. finally {
  83. if (in != null) {
  84. try {
  85. in.close();
  86. }
  87. catch (IOException ex) {
  88. ex.printStackTrace();
  89. }
  90. }
  91. if (out != null) {
  92. try {
  93. out.close();
  94. }
  95. catch (IOException ex) {
  96. ex.printStackTrace();
  97. }
  98. }
  99. }
  100. return worked;
  101. }
  102. }