13
0
geforkt von Mirrors/Paper

Add default GameRule command. Fixes BUKKIT-2671

By: feildmaster <admin@feildmaster.com>
Dieser Commit ist enthalten in:
Bukkit/Spigot 2012-10-30 21:21:21 -05:00
Ursprung 4ee1a130c5
Commit 4b23d98b72
3 geänderte Dateien mit 97 neuen und 2 gelöschten Zeilen

Datei anzeigen

@ -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) {

Datei anzeigen

@ -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();
}
}

Datei anzeigen

@ -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);
}
}