diff options
author | EvilSeph <evilseph@gmail.com> | 2012-10-29 05:19:20 -0400 |
---|---|---|
committer | EvilSeph <evilseph@gmail.com> | 2012-10-29 23:18:18 -0400 |
commit | 8b5440766c2670dae1f8a0b075c7d0fe42e15df1 (patch) | |
tree | 2cee1e7ae57351be7bbd094ef874bcea194773e0 /src/main | |
parent | 2c8199c1f360b0afbdfbdc3b4d04d3f07de7bae6 (diff) | |
download | bukkit-8b5440766c2670dae1f8a0b075c7d0fe42e15df1.tar bukkit-8b5440766c2670dae1f8a0b075c7d0fe42e15df1.tar.gz bukkit-8b5440766c2670dae1f8a0b075c7d0fe42e15df1.tar.lz bukkit-8b5440766c2670dae1f8a0b075c7d0fe42e15df1.tar.xz bukkit-8b5440766c2670dae1f8a0b075c7d0fe42e15df1.zip |
Update ExpCommand with levels support. Fixes BUKKIT-2683 and partially fixes BUKKIT-2671
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/org/bukkit/command/defaults/ExpCommand.java | 68 | ||||
-rw-r--r-- | src/main/java/org/bukkit/entity/Player.java | 7 |
2 files changed, 56 insertions, 19 deletions
diff --git a/src/main/java/org/bukkit/command/defaults/ExpCommand.java b/src/main/java/org/bukkit/command/defaults/ExpCommand.java index 791d2e14..0c92e7fb 100644 --- a/src/main/java/org/bukkit/command/defaults/ExpCommand.java +++ b/src/main/java/org/bukkit/command/defaults/ExpCommand.java @@ -14,35 +14,65 @@ import com.google.common.collect.ImmutableList; public class ExpCommand extends VanillaCommand { public ExpCommand() { super("xp"); - this.description = "Gives the specified player a certain amount of experience"; - this.usageMessage = "/xp <amount> [player]"; + this.description = "Gives the specified player a certain amount of experience. Specify <amount>L to give levels instead, with a negative amount resulting in taking levels."; + this.usageMessage = "/xp <amount> [player] OR /xp <amount>L [player]"; this.setPermission("bukkit.command.xp"); } @Override public boolean execute(CommandSender sender, String currentAlias, String[] args) { if (!testPermission(sender)) return true; - if (args.length < 1) { - sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); - return false; - } - int exp = getInteger(sender, args[0], 0, 5000); - Player player = null; - if (args.length > 1) { - player = Bukkit.getPlayer(args[1]); - } else if (sender instanceof Player) { - player = (Player) sender; - } + if (args.length > 0) { + String inputAmount = args[0]; + Player player = null; + + boolean isLevel = inputAmount.endsWith("l") || inputAmount.endsWith("L"); + if (isLevel && inputAmount.length() > 1) { + inputAmount = inputAmount.substring(0, inputAmount.length() - 1); + } + + int amount = getInteger(sender, inputAmount, Integer.MIN_VALUE, Integer.MAX_VALUE); + boolean isTaking = amount < 0; + + if (isTaking) { + amount *= -1; + } + + if (args.length > 1) { + player = Bukkit.getPlayer(args[1]); + } else if (sender instanceof Player) { + player = (Player) sender; + } + + if (player != null) { + if (isLevel) { + if (isTaking) { + player.giveExpLevels(-amount); + sender.sendMessage("Taken " + amount + " level(s) from " + player.getName()); + } else { + player.giveExpLevels(amount); + sender.sendMessage("Given " + amount + " level(s) to " + player.getName()); + } + } else { + if (isTaking) { + sender.sendMessage(ChatColor.RED + "Taking experience can only be done by levels, cannot give players negative experience points"); + return false; + } else { + player.giveExp(amount); + sender.sendMessage("Given " + amount + " experience to " + player.getName()); + } + } + } else { + sender.sendMessage("Can't find user, was one provided?\n" + ChatColor.RED + "Usage: " + usageMessage); + return false; + } - if (player != null) { - player.giveExp(exp); - Command.broadcastCommandMessage(sender, "Given " + exp + " exp to " + player.getName()); - } else { - sender.sendMessage("Can't find user, was one provided?\n" + ChatColor.RED + "Usage: " + usageMessage); + return true; } - return true; + sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); + return false; } @Override diff --git a/src/main/java/org/bukkit/entity/Player.java b/src/main/java/org/bukkit/entity/Player.java index 7f667f6c..73a9ea4e 100644 --- a/src/main/java/org/bukkit/entity/Player.java +++ b/src/main/java/org/bukkit/entity/Player.java @@ -371,6 +371,13 @@ public interface Player extends HumanEntity, Conversable, CommandSender, Offline public void giveExp(int amount); /** + * Gives the player the amount of experience levels specified. Levels can be taken by specifying a negative amount. + * + * @param amount amount of experience levels to give or take + */ + public void giveExpLevels(int amount); + + /** * Gets the players current experience points towards the next level. * <p /> * This is a percentage value. 0 is "no progress" and 1 is "next level". |