001 package org.bukkit.command.defaults;
002
003 import com.google.common.collect.ImmutableList;
004 import org.apache.commons.lang.Validate;
005 import org.bukkit.Bukkit;
006 import org.bukkit.ChatColor;
007 import org.bukkit.Location;
008 import org.bukkit.World;
009 import org.bukkit.command.Command;
010 import org.bukkit.command.CommandSender;
011 import org.bukkit.entity.Player;
012
013 import java.util.List;
014
015 public class SetWorldSpawnCommand extends VanillaCommand {
016
017 public SetWorldSpawnCommand() {
018 super("setworldspawn");
019 this.description = "Sets a worlds's spawn point. If no coordinates are specified, the player's coordinates will be used.";
020 this.usageMessage = "/setworldspawn OR /setworldspawn <x> <y> <z>";
021 this.setPermission("bukkit.command.setworldspawn");
022 }
023
024 @Override
025 public boolean execute(CommandSender sender, String currentAlias, String[] args) {
026 if (!testPermission(sender)) return true;
027
028 Player player = null;
029 World world;
030 if (sender instanceof Player) {
031 player = (Player) sender;
032 world = player.getWorld();
033 } else {
034 world = Bukkit.getWorlds().get(0);
035 }
036
037 final int x, y, z;
038
039 if (args.length == 0) {
040 if (player == null) {
041 sender.sendMessage("You can only perform this command as a player");
042 return true;
043 }
044
045 Location location = player.getLocation();
046
047 x = location.getBlockX();
048 y = location.getBlockY();
049 z = location.getBlockZ();
050 } else if (args.length == 3) {
051 try {
052 x = getInteger(sender, args[0], MIN_COORD, MAX_COORD, true);
053 y = getInteger(sender, args[1], 0, world.getMaxHeight(), true);
054 z = getInteger(sender, args[2], MIN_COORD, MAX_COORD, true);
055 } catch (NumberFormatException ex) {
056 sender.sendMessage(ex.getMessage());
057 return true;
058 }
059 } else {
060 sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
061 return false;
062 }
063
064 world.setSpawnLocation(x, y, z);
065
066 Command.broadcastCommandMessage(sender, "Set world " + world.getName() + "'s spawnpoint to (" + x + ", " + y + ", " + z + ")");
067 return true;
068
069 }
070
071 @Override
072 public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
073 Validate.notNull(sender, "Sender cannot be null");
074 Validate.notNull(args, "Arguments cannot be null");
075 Validate.notNull(alias, "Alias cannot be null");
076
077 return ImmutableList.of();
078 }
079 }