Parcourir la source

Fixed NPE with /mmoedit

bm01 il y a 13 ans
Parent
commit
050b794b42

+ 29 - 35
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;
-
-                Users.getProfile(modifiedPlayer).modifySkill(skill, newValue);
+            if (skill.equals(SkillType.ALL)) {
+                skillName = "all skills";
+            }
+            else {
+                skillName = Misc.getCapitalized(skill.toString());
+            }
 
-                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
-                }
+            newValue = Integer.valueOf(args[2]);
+            playerProfile = Users.getProfile(args[0]);
 
-                sender.sendMessage(message);
+            if (playerProfile != null) {
+                Player player = playerProfile.getPlayer();
 
-                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:

+ 3 - 5
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();
     }
 
     /**

+ 1 - 1
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()) {

+ 1 - 24
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<PlayerProfile> it = profiles.iterator() ; it.hasNext() ; ) {
-            if (it.next().getPlayerName().equals(playerName)) {
-                it.remove();
-                return;
-            }
-        }
-    }
-
     /**
      * Get the profile of a player.
      *