Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
AchievementCommand.java
1 package org.bukkit.command.defaults;
2 
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6 
7 import org.apache.commons.lang.Validate;
8 import org.bukkit.Achievement;
9 import org.bukkit.Bukkit;
10 import org.bukkit.ChatColor;
11 import org.bukkit.Statistic;
12 import org.bukkit.Material;
13 import org.bukkit.Statistic.Type;
14 import org.bukkit.command.Command;
17 import org.bukkit.entity.Player;
20 
21 import com.google.common.collect.ImmutableList;
22 
23 public class AchievementCommand extends VanillaCommand {
24  public AchievementCommand() {
25  super("achievement");
26  this.description = "Gives the specified player an achievement or changes a statistic value. Use '*' to give all achievements.";
27  this.usageMessage = "/achievement give <stat_name> [player]";
28  this.setPermission("bukkit.command.achievement");
29  }
30 
31  @Override
32  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
33  if (!testPermission(sender)) return true;
34 
35  if (args.length < 2) {
36  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
37  return false;
38  }
39 
40  if (!args[0].equalsIgnoreCase("give")) {
41  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
42  return false;
43  }
44 
45  String statisticString = args[1];
46  Player player = null;
47 
48  if (args.length > 2) {
49  player = Bukkit.getPlayer(args[1]);
50  } else if (sender instanceof Player) {
51  player = (Player) sender;
52  }
53 
54  if (player == null) {
55  sender.sendMessage("You must specify which player you wish to perform this action on.");
56  return true;
57  }
58 
59  if (statisticString.equals("*")) {
60  for (Achievement achievement : Achievement.values()) {
61  if (player.hasAchievement(achievement)) {
62  continue;
63  }
64  PlayerAchievementAwardedEvent event = new PlayerAchievementAwardedEvent(player, achievement);
66  if (!event.isCancelled()) {
67  player.awardAchievement(achievement);
68  }
69  }
70  Command.broadcastCommandMessage(sender, String.format("Successfully given all achievements to %s", player.getName()));
71  return true;
72  }
73 
74  Achievement achievement = Bukkit.getUnsafe().getAchievementFromInternalName(statisticString);
75  Statistic statistic = Bukkit.getUnsafe().getStatisticFromInternalName(statisticString);
76 
77  if (achievement != null) {
78  if (player.hasAchievement(achievement)) {
79  sender.sendMessage(String.format("%s already has achievement %s", player.getName(), statisticString));
80  return true;
81  }
82 
83  PlayerAchievementAwardedEvent event = new PlayerAchievementAwardedEvent(player, achievement);
85  if (event.isCancelled()) {
86  sender.sendMessage(String.format("Unable to award %s the achievement %s", player.getName(), statisticString));
87  return true;
88  }
89  player.awardAchievement(achievement);
90 
91  Command.broadcastCommandMessage(sender, String.format("Successfully given %s the stat %s", player.getName(), statisticString));
92  return true;
93  }
94 
95  if (statistic == null) {
96  sender.sendMessage(String.format("Unknown achievement or statistic '%s'", statisticString));
97  return true;
98  }
99 
100  if (statistic.getType() == Type.UNTYPED) {
101  PlayerStatisticIncrementEvent event = new PlayerStatisticIncrementEvent(player, statistic, player.getStatistic(statistic), player.getStatistic(statistic) + 1);
103  if (event.isCancelled()) {
104  sender.sendMessage(String.format("Unable to increment %s for %s", statisticString, player.getName()));
105  return true;
106  }
107  player.incrementStatistic(statistic);
108  Command.broadcastCommandMessage(sender, String.format("Successfully given %s the stat %s", player.getName(), statisticString));
109  return true;
110  }
111 
112  if (statistic.getType() == Type.ENTITY) {
113  EntityType entityType = EntityType.fromName(statisticString.substring(statisticString.lastIndexOf(".") + 1));
114 
115  if (entityType == null) {
116  sender.sendMessage(String.format("Unknown achievement or statistic '%s'", statisticString));
117  return true;
118  }
119 
120  PlayerStatisticIncrementEvent event = new PlayerStatisticIncrementEvent(player, statistic, player.getStatistic(statistic), player.getStatistic(statistic) + 1, entityType);
122  if (event.isCancelled()) {
123  sender.sendMessage(String.format("Unable to increment %s for %s", statisticString, player.getName()));
124  return true;
125  }
126 
127  try {
128  player.incrementStatistic(statistic, entityType);
129  } catch (IllegalArgumentException e) {
130  sender.sendMessage(String.format("Unknown achievement or statistic '%s'", statisticString));
131  return true;
132  }
133  } else {
134  int id;
135  try {
136  id = getInteger(sender, statisticString.substring(statisticString.lastIndexOf(".") + 1), 0, Integer.MAX_VALUE, true);
137  } catch (NumberFormatException e) {
138  sender.sendMessage(e.getMessage());
139  return true;
140  }
141 
142  Material material = Material.getMaterial(id);
143 
144  if (material == null) {
145  sender.sendMessage(String.format("Unknown achievement or statistic '%s'", statisticString));
146  return true;
147  }
148 
149  PlayerStatisticIncrementEvent event = new PlayerStatisticIncrementEvent(player, statistic, player.getStatistic(statistic), player.getStatistic(statistic) + 1, material);
151  if (event.isCancelled()) {
152  sender.sendMessage(String.format("Unable to increment %s for %s", statisticString, player.getName()));
153  return true;
154  }
155 
156  try {
157  player.incrementStatistic(statistic, material);
158  } catch (IllegalArgumentException e) {
159  sender.sendMessage(String.format("Unknown achievement or statistic '%s'", statisticString));
160  return true;
161  }
162  }
163 
164  Command.broadcastCommandMessage(sender, String.format("Successfully given %s the stat %s", player.getName(), statisticString));
165  return true;
166  }
167 
168  @Override
169  public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
170  Validate.notNull(sender, "Sender cannot be null");
171  Validate.notNull(args, "Arguments cannot be null");
172  Validate.notNull(alias, "Alias cannot be null");
173 
174  if (args.length == 1) {
175  return Arrays.asList("give");
176  }
177 
178  if (args.length == 2) {
179  return Bukkit.getUnsafe().tabCompleteInternalStatisticOrAchievementName(args[1], new ArrayList<String>());
180  }
181 
182  if (args.length == 3) {
183  return super.tabComplete(sender, alias, args);
184  }
185  return ImmutableList.of();
186  }
187 }
boolean testPermission(CommandSender target)
int getStatistic(Statistic statistic)
PluginManager getPluginManager()
static Material getMaterial(final int id)
Definition: Material.java:575
void sendMessage(String message)
void setPermission(String permission)
void incrementStatistic(Statistic statistic)
static Server getServer()
Definition: Bukkit.java:50
static UnsafeValues getUnsafe()
Definition: Bukkit.java:732
boolean hasAchievement(Achievement achievement)
void awardAchievement(Achievement achievement)
static EntityType fromName(String name)
static Player getPlayer(String name)
Definition: Bukkit.java:186