McremoveCommand.java 4.2 KB

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