Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
SetWorldSpawnCommand.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 SetWorldSpawnCommand extends VanillaCommand {
16 
17  public SetWorldSpawnCommand() {
18  super("setworldspawn");
19  this.description = "Sets a worlds's spawn point. If no coordinates are specified, the player's coordinates will be used.";
20  this.usageMessage = "/setworldspawn OR /setworldspawn <x> <y> <z>";
21  this.setPermission("bukkit.command.setworldspawn");
22  }
23 
24  @Override
25  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
26  if (!testPermission(sender)) return true;
27 
28  Player player = null;
29  World world;
30  if (sender instanceof Player) {
31  player = (Player) sender;
32  world = player.getWorld();
33  } else {
34  world = Bukkit.getWorlds().get(0);
35  }
36 
37  final int x, y, z;
38 
39  if (args.length == 0) {
40  if (player == null) {
41  sender.sendMessage("You can only perform this command as a player");
42  return true;
43  }
44 
45  Location location = player.getLocation();
46 
47  x = location.getBlockX();
48  y = location.getBlockY();
49  z = location.getBlockZ();
50  } else if (args.length == 3) {
51  try {
52  x = getInteger(sender, args[0], MIN_COORD, MAX_COORD, true);
53  y = getInteger(sender, args[1], 0, world.getMaxHeight(), true);
54  z = getInteger(sender, args[2], MIN_COORD, MAX_COORD, true);
55  } catch (NumberFormatException ex) {
56  sender.sendMessage(ex.getMessage());
57  return true;
58  }
59  } else {
60  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
61  return false;
62  }
63 
64  world.setSpawnLocation(x, y, z);
65 
66  Command.broadcastCommandMessage(sender, "Set world " + world.getName() + "'s spawnpoint to (" + x + ", " + y + ", " + z + ")");
67  return true;
68 
69  }
70 
71  @Override
72  public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
73  Validate.notNull(sender, "Sender cannot be null");
74  Validate.notNull(args, "Arguments cannot be null");
75  Validate.notNull(alias, "Alias cannot be null");
76 
77  return ImmutableList.of();
78  }
79 }
boolean testPermission(CommandSender target)
void sendMessage(String message)
void setPermission(String permission)
boolean setSpawnLocation(int x, int y, int z)
static List< World > getWorlds()
Definition: Bukkit.java:229
String getName()