From 050b794b4273239b27dede50ca75732b4ba798c0 Mon Sep 17 00:00:00 2001 From: bm01 Date: Tue, 12 Jun 2012 22:51:57 +0200 Subject: [PATCH] Fixed NPE with /mmoedit --- .../commands/general/MmoeditCommand.java | 64 +++++++++---------- .../nossr50/datatypes/PlayerProfile.java | 8 +-- src/main/java/com/gmail/nossr50/mcMMO.java | 2 +- .../java/com/gmail/nossr50/util/Users.java | 25 +------- 4 files changed, 34 insertions(+), 65 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/commands/general/MmoeditCommand.java b/src/main/java/com/gmail/nossr50/commands/general/MmoeditCommand.java index 4e0f3d679..c2d12440c 100644 --- a/src/main/java/com/gmail/nossr50/commands/general/MmoeditCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/general/MmoeditCommand.java @@ -7,7 +7,6 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.commands.CommandHelper; import com.gmail.nossr50.datatypes.PlayerProfile; import com.gmail.nossr50.datatypes.SkillType; @@ -17,16 +16,10 @@ import com.gmail.nossr50.util.Skills; import com.gmail.nossr50.util.Users; public class MmoeditCommand implements CommandExecutor { - private final mcMMO plugin; - - public MmoeditCommand (mcMMO plugin) { - this.plugin = plugin; - } - @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { OfflinePlayer modifiedPlayer; - PlayerProfile PP; + PlayerProfile playerProfile; int newValue; SkillType skill; String skillName; @@ -48,7 +41,7 @@ public class MmoeditCommand implements CommandExecutor { modifiedPlayer = (Player) sender; newValue = Integer.valueOf(args[1]); skill = Skills.getSkillType(args[0]); - PP = Users.getProfile(modifiedPlayer); + playerProfile = Users.getProfile(modifiedPlayer); if (skill.equals(SkillType.ALL)) { skillName = "all skills"; @@ -57,7 +50,7 @@ public class MmoeditCommand implements CommandExecutor { skillName = Misc.getCapitalized(skill.toString()); } - PP.modifySkill(skill, newValue); + playerProfile.modifySkill(skill, newValue); sender.sendMessage(ChatColor.GREEN + "Your level in " + skillName + " was set to " + newValue + "!"); //TODO: Needs more locale. } else { @@ -71,46 +64,47 @@ public class MmoeditCommand implements CommandExecutor { return true; case 3: - modifiedPlayer = plugin.getServer().getOfflinePlayer(args[0]); - String playerName = modifiedPlayer.getName(); - PP = Users.getProfile(modifiedPlayer); - - if (!PP.isLoaded()) { - sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist")); + if (!Misc.isInt(args[2])) { + sender.sendMessage(usage); return true; } - if (!Skills.isSkill(args[1])) { + skill = Skills.getSkillType(args[1]); + + if (skill == null) { sender.sendMessage(LocaleLoader.getString("Commands.Skill.Invalid")); return true; } - if (Misc.isInt(args[2])) { - newValue = Integer.valueOf(args[2]); - skill = Skills.getSkillType(args[1]); - String message; + if (skill.equals(SkillType.ALL)) { + skillName = "all skills"; + } + else { + skillName = Misc.getCapitalized(skill.toString()); + } - Users.getProfile(modifiedPlayer).modifySkill(skill, newValue); + newValue = Integer.valueOf(args[2]); + playerProfile = Users.getProfile(args[0]); - if (skill.equals(SkillType.ALL)) { - skillName = "all skills"; - message = ChatColor.RED + "All skills have been modified for " + playerName + "."; //TODO: Use locale - } - else { - skillName = Misc.getCapitalized(skill.toString()); - message = ChatColor.RED + skillName + " has been modified for " + playerName + "."; //TODO: Use locale - } + if (playerProfile != null) { + Player player = playerProfile.getPlayer(); - sender.sendMessage(message); - - if (modifiedPlayer.isOnline()) { - ((Player) modifiedPlayer).sendMessage(ChatColor.GREEN + "Your level in " + skillName + " was set to " + newValue + "!"); //TODO: Needs more locale. + if (player.isOnline()) { + player.sendMessage(ChatColor.GREEN + "Your level in " + skillName + " was set to " + newValue + "!"); //TODO: Needs more locale. } } else { - sender.sendMessage(usage); + playerProfile = new PlayerProfile(null, args[0], false); + + if (!playerProfile.isLoaded()) { + sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist")); + return true; + } } + sender.sendMessage(ChatColor.RED + skillName + " has been modified for " + args[0] + "."); //TODO: Use locale + playerProfile.modifySkill(skill, newValue); + playerProfile.save(); return true; default: diff --git a/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java b/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java index b3da36869..9e8573f52 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java +++ b/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java @@ -63,10 +63,10 @@ public class PlayerProfile { private String playerName; private final static String location = mcMMO.usersFile; - public PlayerProfile(Player player, boolean addNew) { + public PlayerProfile(Player player, String playerName, boolean addNew) { hud = SpoutConfig.getInstance().defaulthud; this.player = player; - this.playerName = player.getName(); + this.playerName = playerName; party = PartyManager.getInstance().getPlayerParty(playerName); @@ -109,7 +109,7 @@ public class PlayerProfile { public boolean loadMySQL() { userid = mcMMO.database.getInt("SELECT id FROM " + Config.getInstance().getMySQLTablePrefix() + "users WHERE user = '" + playerName + "'"); - if (userid <= 0) { + if (userid == 0) { return false; } else { @@ -1067,8 +1067,6 @@ public class PlayerProfile { skills.put(skillType, newValue); skillsXp.put(skillType, 0); } - - save(); } /** diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index a1b3f1f5b..19baaf094 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -396,7 +396,7 @@ public class mcMMO extends JavaPlugin { } if (configInstance.getCommandMmoeditEnabled()) { - getCommand("mmoedit").setExecutor(new MmoeditCommand(this)); + getCommand("mmoedit").setExecutor(new MmoeditCommand()); } if (configInstance.getCommandInspectEnabled()) { diff --git a/src/main/java/com/gmail/nossr50/util/Users.java b/src/main/java/com/gmail/nossr50/util/Users.java index c043b944e..852b61976 100644 --- a/src/main/java/com/gmail/nossr50/util/Users.java +++ b/src/main/java/com/gmail/nossr50/util/Users.java @@ -56,7 +56,7 @@ public class Users { } //New player, or already removed from the list - PlayerProfile playerProfile = new PlayerProfile(player, true); + PlayerProfile playerProfile = new PlayerProfile(player, playerName, true); profiles.add(playerProfile); return playerProfile; @@ -78,29 +78,6 @@ public class Users { return profiles; } - /** - * Remove a user from the database. - * - * @param player The player to remove - */ - public static void removeUser(OfflinePlayer player) { - removeUser(player.getName()); - } - - /** - * Remove a user from the DB by name. - * - * @param playerName The name of the player to remove - */ - public static void removeUser(String playerName) { - for (Iterator it = profiles.iterator() ; it.hasNext() ; ) { - if (it.next().getPlayerName().equals(playerName)) { - it.remove(); - return; - } - } - } - /** * Get the profile of a player. *