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 SpawnpointCommand extends VanillaCommand { 016 017 public SpawnpointCommand() { 018 super("spawnpoint"); 019 this.description = "Sets a player's spawn point"; 020 this.usageMessage = "/spawnpoint OR /spawnpoint <player> OR /spawnpoint <player> <x> <y> <z>"; 021 this.setPermission("bukkit.command.spawnpoint"); 022 } 023 024 @Override 025 public boolean execute(CommandSender sender, String currentAlias, String[] args) { 026 if (!testPermission(sender)) return true; 027 028 Player player; 029 030 if (args.length == 0) { 031 if (sender instanceof Player) { 032 player = (Player) sender; 033 } else { 034 sender.sendMessage("Please provide a player!"); 035 return true; 036 } 037 } else { 038 player = Bukkit.getPlayerExact(args[0]); 039 if (player == null) { 040 sender.sendMessage("Can't find player " + args[0]); 041 return true; 042 } 043 } 044 045 World world = player.getWorld(); 046 047 if (args.length == 4) { 048 if (world != null) { 049 int pos = 1; 050 final int x, y, z; 051 try { 052 x = getInteger(sender, args[pos++], MIN_COORD, MAX_COORD, true); 053 y = getInteger(sender, args[pos++], 0, world.getMaxHeight()); 054 z = getInteger(sender, args[pos], MIN_COORD, MAX_COORD, true); 055 } catch(NumberFormatException ex) { 056 sender.sendMessage(ex.getMessage()); 057 return true; 058 } 059 060 player.setBedSpawnLocation(new Location(world, x, y, z), true); 061 Command.broadcastCommandMessage(sender, "Set " + player.getDisplayName() + "'s spawnpoint to " + x + ", " + y + ", " + z); 062 } 063 } else if (args.length <= 1) { 064 Location location = player.getLocation(); 065 player.setBedSpawnLocation(location, true); 066 Command.broadcastCommandMessage(sender, "Set " + player.getDisplayName() + "'s spawnpoint to " + location.getX() + ", " + location.getY() + ", " + location.getZ()); 067 } else { 068 sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); 069 return false; 070 } 071 072 return true; 073 } 074 075 @Override 076 public List<String> tabComplete(CommandSender sender, String alias, String[] args) { 077 Validate.notNull(sender, "Sender cannot be null"); 078 Validate.notNull(args, "Arguments cannot be null"); 079 Validate.notNull(alias, "Alias cannot be null"); 080 081 if (args.length == 1) { 082 return super.tabComplete(sender, alias, args); 083 } 084 085 return ImmutableList.of(); 086 } 087 }