diff options
author | feildmaster <admin@feildmaster.com> | 2012-10-30 21:21:21 -0500 |
---|---|---|
committer | EvilSeph <evilseph@gmail.com> | 2012-10-31 03:45:26 -0400 |
commit | 6b333649492712056504439a07aa570db1ccf7f5 (patch) | |
tree | cdf3e06890b69e5a5591bff5e712c8aecd726ba9 /src/main | |
parent | 0eb215cd8f8a0dd6e94b213654d1d8c4698c34c8 (diff) | |
download | bukkit-6b333649492712056504439a07aa570db1ccf7f5.tar bukkit-6b333649492712056504439a07aa570db1ccf7f5.tar.gz bukkit-6b333649492712056504439a07aa570db1ccf7f5.tar.lz bukkit-6b333649492712056504439a07aa570db1ccf7f5.tar.xz bukkit-6b333649492712056504439a07aa570db1ccf7f5.zip |
Add default GameRule command. Fixes BUKKIT-2671
Diffstat (limited to 'src/main')
3 files changed, 97 insertions, 2 deletions
diff --git a/src/main/java/org/bukkit/command/SimpleCommandMap.java b/src/main/java/org/bukkit/command/SimpleCommandMap.java index 7e007c57..1c199dc2 100644 --- a/src/main/java/org/bukkit/command/SimpleCommandMap.java +++ b/src/main/java/org/bukkit/command/SimpleCommandMap.java @@ -57,6 +57,7 @@ public class SimpleCommandMap implements CommandMap { fallbackCommands.add(new WeatherCommand()); fallbackCommands.add(new SpawnpointCommand()); fallbackCommands.add(new ClearCommand()); + fallbackCommands.add(new GameRuleCommand()); } public SimpleCommandMap(final Server server) { diff --git a/src/main/java/org/bukkit/command/defaults/GameRuleCommand.java b/src/main/java/org/bukkit/command/defaults/GameRuleCommand.java new file mode 100644 index 00000000..b5729039 --- /dev/null +++ b/src/main/java/org/bukkit/command/defaults/GameRuleCommand.java @@ -0,0 +1,90 @@ +package org.bukkit.command.defaults; + +import com.google.common.collect.ImmutableList; +import org.apache.commons.lang.Validate; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.util.StringUtil; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.entity.HumanEntity; + +public class GameRuleCommand extends VanillaCommand { + private static final List<String> GAMERULE_STATES = ImmutableList.of("true", "false"); + + public GameRuleCommand() { + super("gamerule"); + this.description = "Sets a server's game rules"; + this.usageMessage = "/gamerule <rule name> <value> OR /gamerule <rule name>"; + this.setPermission("bukkit.command.gamerule"); + } + + @Override + public boolean execute(CommandSender sender, String currentAlias, String[] args) { + if (!testPermission(sender)) return true; + + if (args.length > 0) { + String rule = args[0]; + World world = getGameWorld(sender); + + if (world.isGameRule(rule)) { + if (args.length > 1) { + String value = args[1]; + + world.setGameRuleValue(rule, value); + Command.broadcastCommandMessage(sender, "Game rule has been updated"); + } else { + String value = world.getGameRuleValue(rule); + sender.sendMessage(rule + " = " + value); + } + } else { + sender.sendMessage(ChatColor.RED + "No game rule called " + rule + " is available"); + } + + return true; + } else { + sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); + sender.sendMessage("Rules: " + this.createString(getGameWorld(sender).getGameRules(), 0, ", ")); + + return true; + } + } + + private World getGameWorld(CommandSender sender) { + if (sender instanceof HumanEntity) { + World world = ((HumanEntity) sender).getWorld(); + if (world != null) { + return world; + } + } + + return Bukkit.getWorlds().get(0); + } + + @Override + public boolean matches(String input) { + return input.equalsIgnoreCase("gamerule"); + } + + @Override + public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { + 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], Arrays.asList(getGameWorld(sender).getGameRules()), new ArrayList<String>()); + } + + if (args.length == 2) { + return StringUtil.copyPartialMatches(args[1], GAMERULE_STATES, new ArrayList<String>(GAMERULE_STATES.size())); + } + + return ImmutableList.of(); + } +} diff --git a/src/main/java/org/bukkit/command/defaults/VanillaCommand.java b/src/main/java/org/bukkit/command/defaults/VanillaCommand.java index fc0edf90..d762e64f 100644 --- a/src/main/java/org/bukkit/command/defaults/VanillaCommand.java +++ b/src/main/java/org/bukkit/command/defaults/VanillaCommand.java @@ -58,13 +58,17 @@ public abstract class VanillaCommand extends Command { return result; } - protected String createString(String[] args, int start) { + String createString(String[] args, int start) { + return createString(args, start, " "); + } + + String createString(String[] args, int start, String glue) { StringBuilder string = new StringBuilder(); for (int x = start; x < args.length; x++) { string.append(args[x]); if (x != args.length - 1) { - string.append(" "); + string.append(glue); } } |