mcremove now works on offline users for flatfile and no longer kicks users

This commit is contained in:
nossr50 2012-04-06 09:49:20 -07:00
parent 9ef68d226a
commit 5a37e3e275
2 changed files with 44 additions and 26 deletions

View File

@ -13,6 +13,8 @@ Version 1.3.06-dev
+ Changed Archery's Daze to deal 4 DMG on proc (2 Hearts) + Changed Archery's Daze to deal 4 DMG on proc (2 Hearts)
+ Changed /addlevel command to work for offline users + Changed /addlevel command to work for offline users
= Fixed mmoupdate not being useable from console = 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 PlayerProfile constructor to always take a boolean
! Changed getPlayerProfile function to work for online & offline users ! Changed getPlayerProfile function to work for online & offline users

View File

@ -1,5 +1,9 @@
package com.gmail.nossr50.commands.mc; 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.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -14,6 +18,8 @@ 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";
@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; Player player = null;
@ -68,42 +74,52 @@ public class McremoveCommand implements CommandExecutor {
sender.sendMessage("User "+playerName+" removed from MySQL DB!"); //TODO: Needs more locale. sender.sendMessage("User "+playerName+" removed from MySQL DB!"); //TODO: Needs more locale.
} else { } else {
//FlatFile removal if(removeFlatFileUser(playerName)) {
//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. sender.sendMessage(ChatColor.GREEN+"[mcMMO] It worked! User was removed.");
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 { } else {
sender.sendMessage("[mcMMO] This command is not fully functional for FlatFile yet, the player needs to be online."); //TODO: Needs more locale. sender.sendMessage(ChatColor.RED+"[mcMMO] Couldn't find the user, remember its case sensitive!");
return true;
}
} 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;
} }
} }
//Force PlayerProfile stuff to update //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); Users.players.remove(playerName.toLowerCase());
if(targetPlayer.isOnline()) Users.addUser(Bukkit.getServer().getPlayer(playerName));
{
targetPlayer.kickPlayer("[mcMMO] Stats have been reset! Rejoin!"); //TODO: Needs more locale.
Users.removeUserByName(playerName);
} else {
Users.removeUser(targetPlayer);
}
} else {
Users.removeUserByName(playerName);
} }
sender.sendMessage("[mcMMO] mcremove operation completed."); //TODO: Needs more locale. sender.sendMessage("[mcMMO] mcremove operation completed."); //TODO: Needs more locale.
return true; 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;
}
}
} }