001 package org.bukkit.command.defaults;
002
003 import java.util.List;
004
005 import org.apache.commons.lang.Validate;
006 import org.bukkit.Bukkit;
007 import org.bukkit.ChatColor;
008 import org.bukkit.command.Command;
009 import org.bukkit.command.CommandSender;
010 import org.bukkit.entity.Player;
011
012 import com.google.common.collect.ImmutableList;
013
014 public class ExpCommand extends VanillaCommand {
015 public ExpCommand() {
016 super("xp");
017 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.";
018 this.usageMessage = "/xp <amount> [player] OR /xp <amount>L [player]";
019 this.setPermission("bukkit.command.xp");
020 }
021
022 @Override
023 public boolean execute(CommandSender sender, String currentAlias, String[] args) {
024 if (!testPermission(sender)) return true;
025
026 if (args.length > 0) {
027 String inputAmount = args[0];
028 Player player = null;
029
030 boolean isLevel = inputAmount.endsWith("l") || inputAmount.endsWith("L");
031 if (isLevel && inputAmount.length() > 1) {
032 inputAmount = inputAmount.substring(0, inputAmount.length() - 1);
033 }
034
035 int amount = getInteger(sender, inputAmount, Integer.MIN_VALUE, Integer.MAX_VALUE);
036 boolean isTaking = amount < 0;
037
038 if (isTaking) {
039 amount *= -1;
040 }
041
042 if (args.length > 1) {
043 player = Bukkit.getPlayer(args[1]);
044 } else if (sender instanceof Player) {
045 player = (Player) sender;
046 }
047
048 if (player != null) {
049 if (isLevel) {
050 if (isTaking) {
051 player.giveExpLevels(-amount);
052 Command.broadcastCommandMessage(sender, "Taken " + amount + " level(s) from " + player.getName());
053 } else {
054 player.giveExpLevels(amount);
055 Command.broadcastCommandMessage(sender, "Given " + amount + " level(s) to " + player.getName());
056 }
057 } else {
058 if (isTaking) {
059 sender.sendMessage(ChatColor.RED + "Taking experience can only be done by levels, cannot give players negative experience points");
060 return false;
061 } else {
062 player.giveExp(amount);
063 Command.broadcastCommandMessage(sender, "Given " + amount + " experience to " + player.getName());
064 }
065 }
066 } else {
067 sender.sendMessage("Can't find player, was one provided?\n" + ChatColor.RED + "Usage: " + usageMessage);
068 return false;
069 }
070
071 return true;
072 }
073
074 sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
075 return false;
076 }
077
078 @Override
079 public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
080 Validate.notNull(sender, "Sender cannot be null");
081 Validate.notNull(args, "Arguments cannot be null");
082 Validate.notNull(alias, "Alias cannot be null");
083
084 if (args.length == 2) {
085 return super.tabComplete(sender, alias, args);
086 }
087 return ImmutableList.of();
088 }
089 }