diff options
author | EvilSeph <evilseph@gmail.com> | 2012-10-29 01:53:31 -0400 |
---|---|---|
committer | EvilSeph <evilseph@gmail.com> | 2012-10-29 01:53:31 -0400 |
commit | 1f37a492b42ce0e70183b20bd6c57984903c98b5 (patch) | |
tree | 825e77807a36720297dfc0bf1260ec5f06c97606 /src/main | |
parent | f28f674a3d06a25eae404226e9d55def07354adf (diff) | |
download | bukkit-1f37a492b42ce0e70183b20bd6c57984903c98b5.tar bukkit-1f37a492b42ce0e70183b20bd6c57984903c98b5.tar.gz bukkit-1f37a492b42ce0e70183b20bd6c57984903c98b5.tar.lz bukkit-1f37a492b42ce0e70183b20bd6c57984903c98b5.tar.xz bukkit-1f37a492b42ce0e70183b20bd6c57984903c98b5.zip |
Add default DifficultyCommand. Partially fixes BUKKIT-2671
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/org/bukkit/command/SimpleCommandMap.java | 1 | ||||
-rw-r--r-- | src/main/java/org/bukkit/command/defaults/DifficultyCommand.java | 86 |
2 files changed, 87 insertions, 0 deletions
diff --git a/src/main/java/org/bukkit/command/SimpleCommandMap.java b/src/main/java/org/bukkit/command/SimpleCommandMap.java index a44504ef..6254c0a2 100644 --- a/src/main/java/org/bukkit/command/SimpleCommandMap.java +++ b/src/main/java/org/bukkit/command/SimpleCommandMap.java @@ -53,6 +53,7 @@ public class SimpleCommandMap implements CommandMap { fallbackCommands.add(new BanListCommand()); fallbackCommands.add(new DefaultGameModeCommand()); fallbackCommands.add(new SeedCommand()); + fallbackCommands.add(new DifficultyCommand()); } public SimpleCommandMap(final Server server) { diff --git a/src/main/java/org/bukkit/command/defaults/DifficultyCommand.java b/src/main/java/org/bukkit/command/defaults/DifficultyCommand.java new file mode 100644 index 00000000..05b10001 --- /dev/null +++ b/src/main/java/org/bukkit/command/defaults/DifficultyCommand.java @@ -0,0 +1,86 @@ +package org.bukkit.command.defaults; + +import com.google.common.collect.ImmutableList; +import org.apache.commons.lang.Validate; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.util.StringUtil; +import org.bukkit.Difficulty; + +import java.util.ArrayList; +import java.util.List; + +public class DifficultyCommand extends VanillaCommand { + private static final List<String> DIFFICULTY_NAMES = ImmutableList.of("peaceful", "easy", "normal", "hard"); + + public DifficultyCommand() { + super("difficulty"); + this.description = "Sets the game difficulty"; + this.usageMessage = "/difficulty <new difficulty> "; + this.setPermission("bukkit.command.difficulty"); + } + + @Override + public boolean execute(CommandSender sender, String currentAlias, String[] args) { + if (!testPermission(sender)) return true; + if (args.length != 1 || args[0].length() == 0) { + sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); + return false; + } + + Difficulty difficulty = Difficulty.getByValue(getDifficultyForString(sender, args[0])); + + if (Bukkit.isHardcore()) { + difficulty = Difficulty.HARD; + } + + Bukkit.getWorlds().get(0).setDifficulty(difficulty); + + int levelCount = 1; + if (Bukkit.getAllowNether()) { + Bukkit.getWorlds().get(levelCount).setDifficulty(difficulty); + levelCount++; + } + + if (Bukkit.getAllowEnd()) { + Bukkit.getWorlds().get(levelCount).setDifficulty(difficulty); + } + + Command.broadcastCommandMessage(sender, "Set difficulty to " + difficulty.toString()); + return true; + } + + protected int getDifficultyForString(CommandSender sender, String name) { + if (name.equalsIgnoreCase("peaceful") || name.equalsIgnoreCase("p")) { + return 0; + } else if (name.equalsIgnoreCase("easy") || name.equalsIgnoreCase("e")) { + return 1; + } else if (name.equalsIgnoreCase("normal") || name.equalsIgnoreCase("n")) { + return 2; + } else if (name.equalsIgnoreCase("hard") || name.equalsIgnoreCase("h")) { + return 3; + } else { + return getInteger(sender, name, 0, 3); + } + } + + @Override + public boolean matches(String input) { + return input.equalsIgnoreCase("difficulty"); + } + + @Override + public List<String> tabComplete(CommandSender sender, String alias, String[] args) { + Validate.notNull(sender, "Sender cannot be null"); + Validate.notNull(args, "Arguments cannot be null"); + Validate.notNull(alias, "Alias cannot be null"); + + if (args.length == 1) { + return StringUtil.copyPartialMatches(args[0], DIFFICULTY_NAMES, new ArrayList<String>(DIFFICULTY_NAMES.size())); + } + + return ImmutableList.of(); + } +} |