From d3c8a5565a6a25189b3863d6e97239a94b037a2e Mon Sep 17 00:00:00 2001 From: GJ Date: Thu, 31 Jan 2013 13:29:42 -0500 Subject: [PATCH] Cleaning up from our deprecated permissions. Also cleaned up the unholy mess that was SkillResetCommand. --- .../commands/general/AddlevelsCommand.java | 26 ++-- .../commands/general/AddxpCommand.java | 21 +-- .../commands/general/MmoeditCommand.java | 13 +- .../commands/general/SkillResetCommand.java | 142 ++++++------------ .../database/commands/MmoupdateCommand.java | 6 +- .../nossr50/datatypes/PlayerProfile.java | 20 --- .../nossr50/skills/utilities/SkillTools.java | 2 +- .../com/gmail/nossr50/util/Permissions.java | 20 +++ 8 files changed, 97 insertions(+), 153 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/commands/general/AddlevelsCommand.java b/src/main/java/com/gmail/nossr50/commands/general/AddlevelsCommand.java index b8a3ece5f..0d694b0cb 100644 --- a/src/main/java/com/gmail/nossr50/commands/general/AddlevelsCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/general/AddlevelsCommand.java @@ -13,6 +13,7 @@ import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.skills.utilities.SkillTools; import com.gmail.nossr50.skills.utilities.SkillType; import com.gmail.nossr50.util.Misc; +import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Users; public class AddlevelsCommand implements CommandExecutor{ @@ -22,16 +23,14 @@ public class AddlevelsCommand implements CommandExecutor{ PlayerProfile profile; int levels; SkillType skill; - // DEPRECATED PERMISSION - boolean oldPermission = !CommandHelper.noCommandPermissions(sender, "mcmmo.tools.mmoedit"); String usage = LocaleLoader.getString("Commands.Usage.3", new Object[] {"addlevels", "[" + LocaleLoader.getString("Commands.Usage.Player") + "]", "<" + LocaleLoader.getString("Commands.Usage.Skill") + ">", "<" + LocaleLoader.getString("Commands.Usage.Level") + ">" }); - if (!oldPermission && CommandHelper.noCommandPermissions(sender, "mcmmo.commands.addlevels")) { - return true; - } - switch (args.length) { case 2: + if (CommandHelper.noCommandPermissions(sender, "mcmmo.commands.addlevels") && !Permissions.mmoedit((Player) sender)) { + return true; + } + if (sender instanceof Player) { if (!SkillTools.isSkill(args[0])) { sender.sendMessage(LocaleLoader.getString("Commands.Skill.Invalid")); @@ -61,20 +60,15 @@ public class AddlevelsCommand implements CommandExecutor{ return true; case 3: - if (!oldPermission && CommandHelper.noCommandPermissions(sender, "mcmmo.commands.addlevels.others")) { + if (CommandHelper.noCommandPermissions(sender, "mcmmo.commands.addlevels.others") && !Permissions.mmoedit((Player) sender)) { return true; } modifiedPlayer = mcMMO.p.getServer().getOfflinePlayer(args[0]); - String playerName = modifiedPlayer.getName(); profile = Users.getProfile(modifiedPlayer); - if (profile == null) { - sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist")); - return true; - } - - if (!profile.isLoaded()) { + // TODO:Not sure if we actually need a null check here + if (profile == null || !profile.isLoaded()) { sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist")); return true; } @@ -91,10 +85,10 @@ public class AddlevelsCommand implements CommandExecutor{ Users.getProfile(modifiedPlayer).addLevels(skill, levels); if (skill.equals(SkillType.ALL)) { - sender.sendMessage(LocaleLoader.getString("Commands.addlevels.AwardAll.2", new Object[] {playerName})); + sender.sendMessage(LocaleLoader.getString("Commands.addlevels.AwardAll.2", new Object[] {args[0]})); } else { - sender.sendMessage(LocaleLoader.getString("Commands.addlevels.AwardSkill.2", new Object[] {Misc.getCapitalized(skill.toString()), playerName})); + sender.sendMessage(LocaleLoader.getString("Commands.addlevels.AwardSkill.2", new Object[] {Misc.getCapitalized(skill.toString()), args[0]})); } if (modifiedPlayer.isOnline()) { diff --git a/src/main/java/com/gmail/nossr50/commands/general/AddxpCommand.java b/src/main/java/com/gmail/nossr50/commands/general/AddxpCommand.java index a771d3025..5443b2359 100644 --- a/src/main/java/com/gmail/nossr50/commands/general/AddxpCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/general/AddxpCommand.java @@ -13,6 +13,7 @@ import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.skills.utilities.SkillTools; import com.gmail.nossr50.skills.utilities.SkillType; import com.gmail.nossr50.util.Misc; +import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Users; //TODO: Any way we can make this work for offline use? @@ -22,16 +23,14 @@ public class AddxpCommand implements CommandExecutor { Player modifiedPlayer; int xp; SkillType skill; - // DEPRECATED PERMISSION - boolean oldPermission = !CommandHelper.noCommandPermissions(sender, "mcmmo.tools.mmoedit"); String usage = LocaleLoader.getString("Commands.Usage.3", new Object[] {"addxp", "[" + LocaleLoader.getString("Commands.Usage.Player") + "]", "<" + LocaleLoader.getString("Commands.Usage.Skill") + ">", "<" + LocaleLoader.getString("Commands.Usage.XP") + ">" }); - if (!oldPermission && CommandHelper.noCommandPermissions(sender, "mcmmo.commands.addxp")) { - return true; - } - switch (args.length) { case 2: + if (CommandHelper.noCommandPermissions(sender, "mcmmo.commands.addxp") && !Permissions.mmoedit((Player) sender)) { + return true; + } + if (sender instanceof Player) { if (!SkillTools.isSkill(args[1])) { sender.sendMessage(LocaleLoader.getString("Commands.Skill.Invalid")); @@ -72,7 +71,7 @@ public class AddxpCommand implements CommandExecutor { return true; case 3: - if (!oldPermission && CommandHelper.noCommandPermissions(sender, "mcmmo.commands.addxp")) { + if (CommandHelper.noCommandPermissions(sender, "mcmmo.commands.addxp.others") && !Permissions.mmoedit((Player) sender)) { return true; } @@ -81,12 +80,8 @@ public class AddxpCommand implements CommandExecutor { McMMOPlayer mcMMOPlayer = Users.getPlayer(modifiedPlayer); PlayerProfile profile = Users.getProfile(modifiedPlayer); - if (profile == null) { - sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist")); - return true; - } - - if (!profile.isLoaded()) { + // TODO: Not sure if we actually need a null check here + if (profile == null || !profile.isLoaded()) { sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist")); return true; } 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 3364583e9..02f9d4934 100644 --- a/src/main/java/com/gmail/nossr50/commands/general/MmoeditCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/general/MmoeditCommand.java @@ -12,6 +12,7 @@ import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.skills.utilities.SkillTools; import com.gmail.nossr50.skills.utilities.SkillType; import com.gmail.nossr50.util.Misc; +import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Users; public class MmoeditCommand implements CommandExecutor { @@ -21,16 +22,14 @@ public class MmoeditCommand implements CommandExecutor { int newValue; SkillType skill; String skillName; - // DEPRECATED PERMISSION - boolean oldPermission = !CommandHelper.noCommandPermissions(sender, "mcmmo.tools.mmoedit"); String usage = LocaleLoader.getString("Commands.Usage.3", new Object[] {"mmoedit", "[" + LocaleLoader.getString("Commands.Usage.Player") + "]", "<" + LocaleLoader.getString("Commands.Usage.Skill") + ">", "<" + LocaleLoader.getString("Commands.Usage.Level") + ">" }); - if (!oldPermission && CommandHelper.noCommandPermissions(sender, "mcmmo.commands.mmoedit")) { - return true; - } - switch (args.length) { case 2: + if (CommandHelper.noCommandPermissions(sender, "mcmmo.commands.mmoedit") && !Permissions.mmoedit((Player) sender)) { + return true; + } + if (sender instanceof Player) { if (!SkillTools.isSkill(args[0])) { sender.sendMessage(LocaleLoader.getString("Commands.Skill.Invalid")); @@ -64,7 +63,7 @@ public class MmoeditCommand implements CommandExecutor { return true; case 3: - if (!oldPermission && CommandHelper.noCommandPermissions(sender, "mcmmo.commands.mmoedit.others")) { + if (CommandHelper.noCommandPermissions(sender, "mcmmo.commands.mmoedit.others") && !Permissions.mmoedit((Player) sender)) { return true; } diff --git a/src/main/java/com/gmail/nossr50/commands/general/SkillResetCommand.java b/src/main/java/com/gmail/nossr50/commands/general/SkillResetCommand.java index d2983761c..90d996986 100644 --- a/src/main/java/com/gmail/nossr50/commands/general/SkillResetCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/general/SkillResetCommand.java @@ -10,131 +10,89 @@ import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.commands.CommandHelper; import com.gmail.nossr50.datatypes.PlayerProfile; import com.gmail.nossr50.locale.LocaleLoader; +import com.gmail.nossr50.skills.utilities.SkillTools; import com.gmail.nossr50.skills.utilities.SkillType; +import com.gmail.nossr50.util.Misc; +import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Users; public class SkillResetCommand implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if (CommandHelper.noConsoleUsage(sender)) { - return true; - } - - // DEPRECATED PERMISSION - boolean oldPermission = !CommandHelper.noCommandPermissions(sender, "mcmmo.skillreset"); + OfflinePlayer modifiedPlayer; + PlayerProfile profile; + SkillType skill; String usage = LocaleLoader.getString("Commands.Usage.3", new Object[] {"skillreset", "[" + LocaleLoader.getString("Commands.Usage.Player") + "]", "<" + LocaleLoader.getString("Commands.Usage.Skill") + ">"}); - String perm = "mcmmo.commands.skillreset"; - if (!oldPermission && CommandHelper.noCommandPermissions(sender, perm)) { - return true; - } - - SkillType skillType = null; //simple initialization - PlayerProfile profile = null; - switch (args.length) { case 1: - //make sure there's only one argument. output at least some kind of error if not - if (args.length == 0 || (args.length != 1 && args[0] != null)) { - + if (sender instanceof Player) { + if (!SkillTools.isSkill(args[0])) { + sender.sendMessage(LocaleLoader.getString("Commands.Skill.Invalid")); + return true; + } + + skill = SkillTools.getSkillType(args[0]); + + if (CommandHelper.noCommandPermissions((Player) sender, "mcmmo.commands.skillreset." + skill.toString().toLowerCase()) && !Permissions.skillReset((Player) sender)) { + return true; + } + + modifiedPlayer = (Player) sender; + profile = Users.getProfile(modifiedPlayer); + profile.modifySkill(skill, 0); + + if (skill == SkillType.ALL) { + sender.sendMessage(LocaleLoader.getString("Commands.Reset.All")); + } + else { + sender.sendMessage(LocaleLoader.getString("Commands.Reset.Single", new Object[] { Misc.getCapitalized(skill.toString()) })); + } } - - skillType = getSkillType(sender, args[0], perm, oldPermission); - - if (skillType == null) { - return true; + else { + sender.sendMessage(usage); } - //reset the values in the hash table and persist them - profile = Users.getProfile((Player)sender); - - if (profile == null) { - sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist")); - return true; - } - - profile.resetSkill(skillType); - profile.save(); - - //display a success message to the user - if (skillType == SkillType.ALL) - sender.sendMessage(LocaleLoader.getString("Commands.Reset.All")); - else - sender.sendMessage(LocaleLoader.getString("Commands.Reset.Single", new Object[] { args[0] })); - return true; - case 2: - perm += ".others"; - if (!oldPermission && CommandHelper.noCommandPermissions(sender, perm)) { - return true; - } - OfflinePlayer modifiedPlayer = mcMMO.p.getServer().getOfflinePlayer(args[0]); + case 2: + modifiedPlayer = mcMMO.p.getServer().getOfflinePlayer(args[0]); profile = Users.getProfile(modifiedPlayer); - if (profile == null) { + // TODO:Not sure if we actually need a null check here + if (profile == null || !profile.isLoaded()) { sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist")); return true; } - if (!profile.isLoaded()) { - sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist")); + if (!SkillTools.isSkill(args[1])) { + sender.sendMessage(LocaleLoader.getString("Commands.Skill.Invalid")); return true; } - skillType = getSkillType(sender, args[1], perm, oldPermission); + skill = SkillTools.getSkillType(args[1]); - if (skillType == null) { + if (CommandHelper.noCommandPermissions(sender, "mcmmo.commands.skillreset.others." + skill.toString().toLowerCase()) && !Permissions.skillReset((Player) sender)) { return true; } - //reset the values in the hash table and persist them - profile.resetSkill(skillType); - profile.save(); + profile.modifySkill(skill, 0); - //display a success message to the user - if (skillType == SkillType.ALL) - sender.sendMessage(LocaleLoader.getString("Commands.Reset.All")); - else - sender.sendMessage(LocaleLoader.getString("Commands.Reset.Single", new Object[] { args[1] })); + if (modifiedPlayer.isOnline()) { + if (skill == SkillType.ALL) { + ((Player)modifiedPlayer).sendMessage(LocaleLoader.getString("Commands.Reset.All")); + } + else { + ((Player)modifiedPlayer).sendMessage(LocaleLoader.getString("Commands.Reset.Single", new Object[] { Misc.getCapitalized(skill.toString()) })); + } + } + sender.sendMessage(LocaleLoader.getString("Commands.mmoedit.Modified.2", new Object[] { Misc.getCapitalized(skill.toString()), args[0] })); return true; - default: + + default: sender.sendMessage(usage); return true; - } - } - - private SkillType getSkillType(CommandSender sender, String name, String perm, boolean oldPermission) { - //parse the skilltype that they sent - try { - //ucase needed to match enum since it's case sensitive. trim to be nice - SkillType type = SkillType.valueOf(name.toUpperCase().trim()); - String lowerName = type.name().toLowerCase(); - - if (type == SkillType.ALL && !checkAll(sender, perm, oldPermission)) - return null; - else if (!oldPermission && CommandHelper.noCommandPermissions(sender, perm + "." + name)) - return null; - - return type; - } catch(IllegalArgumentException ex) { - sender.sendMessage(LocaleLoader.getString("Commands.Skill.Invalid")); - return null; } } - - private boolean checkAll(CommandSender sender, String perm, boolean oldPermission) { - for (SkillType type : SkillType.values()) { - if (type.name().equalsIgnoreCase("all")) - continue; - - String name = type.name().toLowerCase(); - - if (!oldPermission && CommandHelper.noCommandPermissions(sender, perm + "." + name)) - return false; - } - - return true; - } } diff --git a/src/main/java/com/gmail/nossr50/database/commands/MmoupdateCommand.java b/src/main/java/com/gmail/nossr50/database/commands/MmoupdateCommand.java index 3215d86b8..2c9709a14 100644 --- a/src/main/java/com/gmail/nossr50/database/commands/MmoupdateCommand.java +++ b/src/main/java/com/gmail/nossr50/database/commands/MmoupdateCommand.java @@ -10,15 +10,13 @@ import com.gmail.nossr50.commands.CommandHelper; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.database.runnables.SQLConversionTask; import com.gmail.nossr50.locale.LocaleLoader; +import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Users; public class MmoupdateCommand implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - // DEPRECATED PERMISSION - boolean oldPermission = !CommandHelper.noCommandPermissions(sender, "mcmmo.admin"); - - if (!oldPermission && CommandHelper.noCommandPermissions(sender, "mcmmo.commands.mmoupdate")) { + if (CommandHelper.noCommandPermissions(sender, "mcmmo.commands.mmoupdate") && !Permissions.admin((Player) sender)) { return true; } diff --git a/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java b/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java index 6dd48fae7..5d02270d3 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java +++ b/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java @@ -953,26 +953,6 @@ public class PlayerProfile { save(false); } - public void resetSkill(SkillType skillType) - { - if (skillType.isChildSkill()) { - return; - } - - //do a single skilltype - if (skillType != SkillType.ALL) - skills.put(skillType, 0); - else //do them all - { - for (SkillType skill : SkillType.values()) //iterate over all items in the enumeration - { - if (skill != SkillType.ALL) // skip the "all" value - skills.put(skill, 0); - } - } - save(false); - } - // /** // * Adds XP to the player, doesn't calculate for XP Rate // * diff --git a/src/main/java/com/gmail/nossr50/skills/utilities/SkillTools.java b/src/main/java/com/gmail/nossr50/skills/utilities/SkillTools.java index edff90519..872d3753d 100644 --- a/src/main/java/com/gmail/nossr50/skills/utilities/SkillTools.java +++ b/src/main/java/com/gmail/nossr50/skills/utilities/SkillTools.java @@ -301,7 +301,7 @@ public class SkillTools { */ public static SkillType getSkillType(String skillName) { for (SkillType x : SkillType.values()) { - if (x.toString().equals(skillName.toUpperCase())) { + if (x.toString().equalsIgnoreCase(skillName)) { return x; } } diff --git a/src/main/java/com/gmail/nossr50/util/Permissions.java b/src/main/java/com/gmail/nossr50/util/Permissions.java index 2f7c9eb56..2e37336de 100644 --- a/src/main/java/com/gmail/nossr50/util/Permissions.java +++ b/src/main/java/com/gmail/nossr50/util/Permissions.java @@ -20,6 +20,10 @@ public final class Permissions { return hasPermission(player, "mcmmo.motd"); } + /** + * @deprecated Use the permission "mcmmo.all" instead. + */ + @Deprecated public static boolean admin(Player player) { return hasPermission(player, "mcmmo.admin"); } @@ -71,6 +75,10 @@ public final class Permissions { return hasPermission(player, "mcmmo.tools.mcremove"); } + /** + * @deprecated Use {@link #mmoeditCommand(player)} instead. + */ + @Deprecated public static boolean mmoedit(Player player) { return hasPermission(player, "mcmmo.tools.mmoedit"); } @@ -548,6 +556,14 @@ public final class Permissions { * MCMMO.COMMANDS.* */ + public static boolean mmoeditCommand(Player player) { + return (hasPermission(player, "mcmmo.commands.mmoedit") || mmoedit(player)); + } + + public static boolean skillResetCommand(Player player) { + return (hasPermission(player, "mcmmo.commands.skillreset") || skillReset(player)); + } + public static boolean mcAbility(Player player) { return hasPermission(player, "mcmmo.commands.ability"); } @@ -564,6 +580,10 @@ public final class Permissions { return hasPermission(player, "mcmmo.commands.party"); } + /** + * @deprecated Use {@link #skillResetCommand(player)} instead. + */ + @Deprecated public static boolean skillReset(Player player) { return hasPermission(player, "mcmmo.skillreset"); }