McremoveCommand.java 4.1 KB

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