Even more locale.

This commit is contained in:
GJ 2012-04-20 00:03:03 -04:00
parent ea350c30c8
commit 46d7212d1b
2 changed files with 83 additions and 77 deletions

View File

@ -13,42 +13,40 @@ import org.bukkit.entity.Player;
import com.gmail.nossr50.Users; import com.gmail.nossr50.Users;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.mcPermissions; import com.gmail.nossr50.commands.CommandHelper;
import com.gmail.nossr50.config.LoadProperties; import com.gmail.nossr50.config.LoadProperties;
import com.gmail.nossr50.locale.mcLocale; import com.gmail.nossr50.locale.mcLocale;
public class McremoveCommand implements CommandExecutor { public class McremoveCommand implements CommandExecutor {
String location = "plugins/mcMMO/FlatFileStuff/mcmmo.users"; private final String LOCATION = "plugins/mcMMO/FlatFileStuff/mcmmo.users";
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
Player player = null; String playerName;
String usage = ChatColor.RED + "Correct usage is /mcremove <playername>"; //TODO: Needs more locale.
String success;
if (sender instanceof Player) { if (CommandHelper.noCommandPermissions(sender, "mcmmo.tools.mcremove")) {
player = (Player) sender;
}
if (player != null && !mcPermissions.getInstance().mcremove(player)) {
player.sendMessage(ChatColor.YELLOW + "[mcMMO] " + ChatColor.DARK_RED + mcLocale.getString("mcPlayerListener.NoPermission"));
return true; return true;
} }
if(args.length == 0) switch (args.length) {
{ case 1:
sender.sendMessage("Correct usage is /mcremove [Player Name]"); //TODO: Needs more locale. playerName = args[0];
success = ChatColor.GREEN + playerName + "was successfully removed from the database!"; //TODO: Locale
break;
default:
sender.sendMessage(usage);
return true; return true;
} }
String playerName = args[0]; //Player that we are going to remove /* MySQL */
if (LoadProperties.useMySQL) {
//If the server is using MySQL
if(LoadProperties.useMySQL)
{
int userId = 0; int userId = 0;
userId = mcMMO.database.getInt("SELECT id FROM " + LoadProperties.MySQLtablePrefix + "users WHERE user = '" + playerName + "'"); userId = mcMMO.database.getInt("SELECT id FROM " + LoadProperties.MySQLtablePrefix + "users WHERE user = '" + playerName + "'");
if (userId > 0) { if (userId > 0) {
//Remove user from tables
mcMMO.database.write("DELETE FROM " mcMMO.database.write("DELETE FROM "
+ LoadProperties.MySQLdbName + "." + LoadProperties.MySQLdbName + "."
+ LoadProperties.MySQLtablePrefix + "users WHERE " + LoadProperties.MySQLtablePrefix + "users WHERE "
@ -74,43 +72,49 @@ public class McremoveCommand implements CommandExecutor {
+ LoadProperties.MySQLtablePrefix + "experience WHERE " + LoadProperties.MySQLtablePrefix + "experience WHERE "
+ LoadProperties.MySQLtablePrefix + "experience.user_id=" + userId); + LoadProperties.MySQLtablePrefix + "experience.user_id=" + userId);
sender.sendMessage("User "+playerName+" removed from MySQL DB!"); //TODO: Needs more locale. sender.sendMessage(success);
} else {
sender.sendMessage("Unable to find player named "+playerName+" in the database!");
} }
} else { else {
sender.sendMessage(mcLocale.getString("Commands.DoesNotExist"));
}
}
else {
if (removeFlatFileUser(playerName)) { if (removeFlatFileUser(playerName)) {
sender.sendMessage(ChatColor.GREEN+"[mcMMO] It worked! User was removed."); sender.sendMessage(success);
} else { }
sender.sendMessage(ChatColor.RED+"[mcMMO] Couldn't find the user, remember its case sensitive!"); else {
sender.sendMessage(mcLocale.getString("Commands.DoesNotExist"));
} }
} }
//Force PlayerProfile stuff to update //Force PlayerProfile stuff to update
if(Bukkit.getServer().getPlayer(playerName) != null && Users.players.containsKey(playerName.toLowerCase())) Player player = Bukkit.getServer().getPlayer(playerName);
{
Users.players.remove(playerName.toLowerCase());
Users.addUser(Bukkit.getServer().getPlayer(playerName));
}
sender.sendMessage("[mcMMO] mcremove operation completed."); //TODO: Needs more locale. if (player != null && Users.players.containsKey(playerName.toLowerCase())) {
Users.removeUser(player);
Users.addUser(player);
}
return true; return true;
} }
private boolean removeFlatFileUser(String playerName) { private boolean removeFlatFileUser(String playerName) {
boolean worked = false; boolean worked = false;
try { try {
FileReader file = new FileReader(location); FileReader file = new FileReader(LOCATION);
BufferedReader in = new BufferedReader(file); BufferedReader in = new BufferedReader(file);
StringBuilder writer = new StringBuilder(); StringBuilder writer = new StringBuilder();
String line = ""; String line = "";
while ((line = in.readLine()) != null) { while ((line = in.readLine()) != null) {
/* Write out the same file but when we get to the player we want to remove we skip his line */
if(!line.split(":")[0].equalsIgnoreCase(playerName)) /* Write out the same file but when we get to the player we want to remove, we skip his line. */
{ if (!line.split(":")[0].equalsIgnoreCase(playerName)) {
writer.append(line).append("\r\n"); writer.append(line).append("\r\n");
} else { }
else {
System.out.println("User found, removing..."); System.out.println("User found, removing...");
worked = true; worked = true;
continue; //Skip the player continue; //Skip the player
@ -118,12 +122,14 @@ public class McremoveCommand implements CommandExecutor {
} }
in.close(); in.close();
FileWriter out = new FileWriter(location); //Write out the new file FileWriter out = new FileWriter(LOCATION); //Write out the new file
out.write(writer.toString()); out.write(writer.toString());
out.close(); out.close();
return worked; return worked;
} catch (Exception e) { }
Bukkit.getLogger().severe("Exception while reading " + location + " (Are you sure you formatted it correctly?)" + e.toString()); catch (Exception e) {
Bukkit.getLogger().severe("Exception while reading " + LOCATION + " (Are you sure you formatted it correctly?)" + e.toString());
return worked; return worked;
} }
} }