Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
ExpCommand.java
1 package org.bukkit.command.defaults;
2 
3 import java.util.List;
4 
5 import org.apache.commons.lang.Validate;
6 import org.bukkit.Bukkit;
7 import org.bukkit.ChatColor;
10 import org.bukkit.entity.Player;
11 
12 import com.google.common.collect.ImmutableList;
13 
14 public class ExpCommand extends VanillaCommand {
15  public ExpCommand() {
16  super("xp");
17  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.";
18  this.usageMessage = "/xp <amount> [player] OR /xp <amount>L [player]";
19  this.setPermission("bukkit.command.xp");
20  }
21 
22  @Override
23  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
24  if (!testPermission(sender)) return true;
25 
26  if (args.length > 0) {
27  String inputAmount = args[0];
28  Player player = null;
29 
30  boolean isLevel = inputAmount.endsWith("l") || inputAmount.endsWith("L");
31  if (isLevel && inputAmount.length() > 1) {
32  inputAmount = inputAmount.substring(0, inputAmount.length() - 1);
33  }
34 
35  int amount = getInteger(sender, inputAmount, Integer.MIN_VALUE, Integer.MAX_VALUE);
36  boolean isTaking = amount < 0;
37 
38  if (isTaking) {
39  amount *= -1;
40  }
41 
42  if (args.length > 1) {
43  player = Bukkit.getPlayer(args[1]);
44  } else if (sender instanceof Player) {
45  player = (Player) sender;
46  }
47 
48  if (player != null) {
49  if (isLevel) {
50  if (isTaking) {
51  player.giveExpLevels(-amount);
52  Command.broadcastCommandMessage(sender, "Taken " + amount + " level(s) from " + player.getName());
53  } else {
54  player.giveExpLevels(amount);
55  Command.broadcastCommandMessage(sender, "Given " + amount + " level(s) to " + player.getName());
56  }
57  } else {
58  if (isTaking) {
59  sender.sendMessage(ChatColor.RED + "Taking experience can only be done by levels, cannot give players negative experience points");
60  return false;
61  } else {
62  player.giveExp(amount);
63  Command.broadcastCommandMessage(sender, "Given " + amount + " experience to " + player.getName());
64  }
65  }
66  } else {
67  sender.sendMessage("Can't find player, was one provided?\n" + ChatColor.RED + "Usage: " + usageMessage);
68  return false;
69  }
70 
71  return true;
72  }
73 
74  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
75  return false;
76  }
77 
78  @Override
79  public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
80  Validate.notNull(sender, "Sender cannot be null");
81  Validate.notNull(args, "Arguments cannot be null");
82  Validate.notNull(alias, "Alias cannot be null");
83 
84  if (args.length == 2) {
85  return super.tabComplete(sender, alias, args);
86  }
87  return ImmutableList.of();
88  }
89 }
boolean testPermission(CommandSender target)
void giveExpLevels(int amount)
void giveExp(int amount)
void sendMessage(String message)
void setPermission(String permission)
static Player getPlayer(String name)
Definition: Bukkit.java:186