Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
SpawnpointCommand.java
1 package org.bukkit.command.defaults;
2 
3 import com.google.common.collect.ImmutableList;
4 import org.apache.commons.lang.Validate;
5 import org.bukkit.Bukkit;
6 import org.bukkit.ChatColor;
7 import org.bukkit.Location;
8 import org.bukkit.World;
11 import org.bukkit.entity.Player;
12 
13 import java.util.List;
14 
15 public class SpawnpointCommand extends VanillaCommand {
16 
17  public SpawnpointCommand() {
18  super("spawnpoint");
19  this.description = "Sets a player's spawn point";
20  this.usageMessage = "/spawnpoint OR /spawnpoint <player> OR /spawnpoint <player> <x> <y> <z>";
21  this.setPermission("bukkit.command.spawnpoint");
22  }
23 
24  @Override
25  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
26  if (!testPermission(sender)) return true;
27 
28  Player player;
29 
30  if (args.length == 0) {
31  if (sender instanceof Player) {
32  player = (Player) sender;
33  } else {
34  sender.sendMessage("Please provide a player!");
35  return true;
36  }
37  } else {
38  player = Bukkit.getPlayerExact(args[0]);
39  if (player == null) {
40  sender.sendMessage("Can't find player " + args[0]);
41  return true;
42  }
43  }
44 
45  World world = player.getWorld();
46 
47  if (args.length == 4) {
48  if (world != null) {
49  int pos = 1;
50  final int x, y, z;
51  try {
52  x = getInteger(sender, args[pos++], MIN_COORD, MAX_COORD, true);
53  y = getInteger(sender, args[pos++], 0, world.getMaxHeight());
54  z = getInteger(sender, args[pos], MIN_COORD, MAX_COORD, true);
55  } catch(NumberFormatException ex) {
56  sender.sendMessage(ex.getMessage());
57  return true;
58  }
59 
60  player.setBedSpawnLocation(new Location(world, x, y, z), true);
61  Command.broadcastCommandMessage(sender, "Set " + player.getDisplayName() + "'s spawnpoint to " + x + ", " + y + ", " + z);
62  }
63  } else if (args.length <= 1) {
64  Location location = player.getLocation();
65  player.setBedSpawnLocation(location, true);
66  Command.broadcastCommandMessage(sender, "Set " + player.getDisplayName() + "'s spawnpoint to " + location.getX() + ", " + location.getY() + ", " + location.getZ());
67  } else {
68  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
69  return false;
70  }
71 
72  return true;
73  }
74 
75  @Override
76  public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
77  Validate.notNull(sender, "Sender cannot be null");
78  Validate.notNull(args, "Arguments cannot be null");
79  Validate.notNull(alias, "Alias cannot be null");
80 
81  if (args.length == 1) {
82  return super.tabComplete(sender, alias, args);
83  }
84 
85  return ImmutableList.of();
86  }
87 }
boolean testPermission(CommandSender target)
void sendMessage(String message)
void setPermission(String permission)
void setBedSpawnLocation(Location location)
static Player getPlayerExact(String name)
Definition: Bukkit.java:436