Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
DifficultyCommand.java
1 package org.bukkit.command.defaults;
2 
3 import com.google.common.collect.ImmutableList;
4 import org.apache.commons.lang.Validate;
5 import org.bukkit.Bukkit;
6 import org.bukkit.ChatColor;
10 import org.bukkit.Difficulty;
11 
12 import java.util.ArrayList;
13 import java.util.List;
14 
15 public class DifficultyCommand extends VanillaCommand {
16  private static final List<String> DIFFICULTY_NAMES = ImmutableList.of("peaceful", "easy", "normal", "hard");
17 
18  public DifficultyCommand() {
19  super("difficulty");
20  this.description = "Sets the game difficulty";
21  this.usageMessage = "/difficulty <new difficulty> ";
22  this.setPermission("bukkit.command.difficulty");
23  }
24 
25  @Override
26  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
27  if (!testPermission(sender)) return true;
28  if (args.length != 1 || args[0].length() == 0) {
29  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
30  return false;
31  }
32 
33  Difficulty difficulty = Difficulty.getByValue(getDifficultyForString(sender, args[0]));
34 
35  if (Bukkit.isHardcore()) {
36  difficulty = Difficulty.HARD;
37  }
38 
39  Bukkit.getWorlds().get(0).setDifficulty(difficulty);
40 
41  int levelCount = 1;
42  if (Bukkit.getAllowNether()) {
43  Bukkit.getWorlds().get(levelCount).setDifficulty(difficulty);
44  levelCount++;
45  }
46 
47  if (Bukkit.getAllowEnd()) {
48  Bukkit.getWorlds().get(levelCount).setDifficulty(difficulty);
49  }
50 
51  Command.broadcastCommandMessage(sender, "Set difficulty to " + difficulty.toString());
52  return true;
53  }
54 
55  protected int getDifficultyForString(CommandSender sender, String name) {
56  if (name.equalsIgnoreCase("peaceful") || name.equalsIgnoreCase("p")) {
57  return 0;
58  } else if (name.equalsIgnoreCase("easy") || name.equalsIgnoreCase("e")) {
59  return 1;
60  } else if (name.equalsIgnoreCase("normal") || name.equalsIgnoreCase("n")) {
61  return 2;
62  } else if (name.equalsIgnoreCase("hard") || name.equalsIgnoreCase("h")) {
63  return 3;
64  } else {
65  return getInteger(sender, name, 0, 3);
66  }
67  }
68 
69  @Override
70  public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
71  Validate.notNull(sender, "Sender cannot be null");
72  Validate.notNull(args, "Arguments cannot be null");
73  Validate.notNull(alias, "Alias cannot be null");
74 
75  if (args.length == 1) {
76  return StringUtil.copyPartialMatches(args[0], DIFFICULTY_NAMES, new ArrayList<String>(DIFFICULTY_NAMES.size()));
77  }
78 
79  return ImmutableList.of();
80  }
81 }
static Difficulty getByValue(final int value)
Definition: Difficulty.java:63
boolean testPermission(CommandSender target)
static boolean isHardcore()
Definition: Bukkit.java:399
static< TextendsCollection<?superString > T copyPartialMatches(final String token, final Iterable< String > originals, final T collection)
Definition: StringUtil.java:24
void sendMessage(String message)
void setPermission(String permission)
static List< World > getWorlds()
Definition: Bukkit.java:229
static boolean getAllowNether()
Definition: Bukkit.java:157
static boolean getAllowEnd()
Definition: Bukkit.java:527