Update ExpCommand with levels support. Fixes BUKKIT-2683 and partially fixes BUKKIT-2671

This commit is contained in:
EvilSeph 2012-10-29 05:19:20 -04:00
parent 2c8199c1f3
commit 8b5440766c
2 changed files with 57 additions and 20 deletions

View File

@ -14,21 +14,31 @@ import com.google.common.collect.ImmutableList;
public class ExpCommand extends VanillaCommand { public class ExpCommand extends VanillaCommand {
public ExpCommand() { public ExpCommand() {
super("xp"); super("xp");
this.description = "Gives the specified player a certain amount of experience"; 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]"; this.usageMessage = "/xp <amount> [player] OR /xp <amount>L [player]";
this.setPermission("bukkit.command.xp"); this.setPermission("bukkit.command.xp");
} }
@Override @Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) { public boolean execute(CommandSender sender, String currentAlias, String[] args) {
if (!testPermission(sender)) return true; if (!testPermission(sender)) return true;
if (args.length < 1) {
sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); if (args.length > 0) {
return false; String inputAmount = args[0];
}
int exp = getInteger(sender, args[0], 0, 5000);
Player player = null; 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) { if (args.length > 1) {
player = Bukkit.getPlayer(args[1]); player = Bukkit.getPlayer(args[1]);
} else if (sender instanceof Player) { } else if (sender instanceof Player) {
@ -36,15 +46,35 @@ public class ExpCommand extends VanillaCommand {
} }
if (player != null) { if (player != null) {
player.giveExp(exp); if (isLevel) {
Command.broadcastCommandMessage(sender, "Given " + exp + " exp to " + player.getName()); 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 { } else {
sender.sendMessage("Can't find user, was one provided?\n" + ChatColor.RED + "Usage: " + usageMessage); sender.sendMessage("Can't find user, was one provided?\n" + ChatColor.RED + "Usage: " + usageMessage);
return false;
} }
return true; return true;
} }
sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
return false;
}
@Override @Override
public boolean matches(String input) { public boolean matches(String input) {
return input.equalsIgnoreCase("xp"); return input.equalsIgnoreCase("xp");

View File

@ -370,6 +370,13 @@ public interface Player extends HumanEntity, Conversable, CommandSender, Offline
*/ */
public void giveExp(int amount); 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. * Gets the players current experience points towards the next level.
* <p /> * <p />