From 5a37e3e27597494be42a481e08f2ae3ade2ce6a3 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Fri, 6 Apr 2012 09:49:20 -0700 Subject: [PATCH] mcremove now works on offline users for flatfile and no longer kicks users --- Changelog.txt | 2 + .../nossr50/commands/mc/McremoveCommand.java | 68 ++++++++++++------- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/Changelog.txt b/Changelog.txt index eaa2fb5ca..7d7ea479b 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -13,6 +13,8 @@ Version 1.3.06-dev + Changed Archery's Daze to deal 4 DMG on proc (2 Hearts) + Changed /addlevel command to work for offline users = Fixed mmoupdate not being useable from console + ! Changed mcremove to no longer kick players when they are removed from database + ! Changed mcremove to work on offline users for FlatFile ! Changed PlayerProfile constructor to always take a boolean ! Changed getPlayerProfile function to work for online & offline users diff --git a/src/main/java/com/gmail/nossr50/commands/mc/McremoveCommand.java b/src/main/java/com/gmail/nossr50/commands/mc/McremoveCommand.java index 3d4d86977..4cdefe196 100644 --- a/src/main/java/com/gmail/nossr50/commands/mc/McremoveCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/mc/McremoveCommand.java @@ -1,5 +1,9 @@ package com.gmail.nossr50.commands.mc; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.FileWriter; + import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; @@ -14,6 +18,8 @@ import com.gmail.nossr50.config.LoadProperties; import com.gmail.nossr50.locale.mcLocale; public class McremoveCommand implements CommandExecutor { + String location = "plugins/mcMMO/FlatFileStuff/mcmmo.users"; + @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { Player player = null; @@ -68,42 +74,52 @@ public class McremoveCommand implements CommandExecutor { sender.sendMessage("User "+playerName+" removed from MySQL DB!"); //TODO: Needs more locale. } else { - //FlatFile removal - //TODO: Properly remove users from FlatFile, it's going to be a huge bitch with how our FlatFile system works. Let's adopt SQLite support. - if(Bukkit.getServer().getPlayer(playerName) != null) - { - Player targetPlayer = Bukkit.getServer().getPlayer(playerName); - if(targetPlayer.isOnline()) - { - Users.getProfile(targetPlayer).resetAllData(); - sender.sendMessage("User "+playerName+" removed from FlatFile DB!"); //TODO: Needs more locale. - } else { - sender.sendMessage("[mcMMO] This command is not fully functional for FlatFile yet, the player needs to be online."); //TODO: Needs more locale. - return true; - } + if(removeFlatFileUser(playerName)) { + sender.sendMessage(ChatColor.GREEN+"[mcMMO] It worked! User was removed."); } else { - sender.sendMessage("[mcMMO] This command is not fully functional for FlatFile yet, the player needs to be online."); //TODO: Needs more locale. - return true; + sender.sendMessage(ChatColor.RED+"[mcMMO] Couldn't find the user, remember its case sensitive!"); } } //Force PlayerProfile stuff to update - if(Bukkit.getServer().getPlayer(playerName) != null) + if(Bukkit.getServer().getPlayer(playerName) != null && Users.players.containsKey(playerName.toLowerCase())) { - Player targetPlayer = Bukkit.getServer().getPlayer(playerName); - if(targetPlayer.isOnline()) - { - targetPlayer.kickPlayer("[mcMMO] Stats have been reset! Rejoin!"); //TODO: Needs more locale. - Users.removeUserByName(playerName); - } else { - Users.removeUser(targetPlayer); - } - } else { - Users.removeUserByName(playerName); + Users.players.remove(playerName.toLowerCase()); + Users.addUser(Bukkit.getServer().getPlayer(playerName)); } sender.sendMessage("[mcMMO] mcremove operation completed."); //TODO: Needs more locale. return true; } + + private boolean removeFlatFileUser(String playerName) { + boolean worked = false; + try { + FileReader file = new FileReader(location); + BufferedReader in = new BufferedReader(file); + StringBuilder writer = new StringBuilder(); + String line = ""; + 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)) + { + writer.append(line).append("\r\n"); + } else { + System.out.println("User found, removing..."); + worked = true; + continue; //Skip the player + } + } + + in.close(); + FileWriter out = new FileWriter(location); //Write out the new file + out.write(writer.toString()); + out.close(); + return worked; + } catch (Exception e) { + Bukkit.getLogger().severe("Exception while reading " + location + " (Are you sure you formatted it correctly?)" + e.toString()); + return worked; + } + } } \ No newline at end of file