From 4c6c68d5fcc621ee3b26abbd82cdd964dbcce31b Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Mon, 29 Oct 2012 03:40:09 -0400 Subject: [PATCH] Add default SpawnpointCommand. Partially fixes BUKKIT-2671 By: EvilSeph --- .../org/bukkit/command/SimpleCommandMap.java | 1 + .../command/defaults/SpawnpointCommand.java | 83 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 paper-api/src/main/java/org/bukkit/command/defaults/SpawnpointCommand.java diff --git a/paper-api/src/main/java/org/bukkit/command/SimpleCommandMap.java b/paper-api/src/main/java/org/bukkit/command/SimpleCommandMap.java index 8b28d90603..292f768630 100644 --- a/paper-api/src/main/java/org/bukkit/command/SimpleCommandMap.java +++ b/paper-api/src/main/java/org/bukkit/command/SimpleCommandMap.java @@ -55,6 +55,7 @@ public class SimpleCommandMap implements CommandMap { fallbackCommands.add(new SeedCommand()); fallbackCommands.add(new DifficultyCommand()); fallbackCommands.add(new WeatherCommand()); + fallbackCommands.add(new SpawnpointCommand()); } public SimpleCommandMap(final Server server) { diff --git a/paper-api/src/main/java/org/bukkit/command/defaults/SpawnpointCommand.java b/paper-api/src/main/java/org/bukkit/command/defaults/SpawnpointCommand.java new file mode 100644 index 0000000000..0f2abfa687 --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/command/defaults/SpawnpointCommand.java @@ -0,0 +1,83 @@ +package org.bukkit.command.defaults; + +import com.google.common.collect.ImmutableList; +import org.apache.commons.lang.Validate; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.List; + +public class SpawnpointCommand extends VanillaCommand { + + public SpawnpointCommand() { + super("spawnpoint"); + this.description = "Sets a player's spawn point"; + this.usageMessage = "/spawnpoint OR /spawnpoint OR /spawnpoint "; + this.setPermission("bukkit.command.spawnpoint"); + } + + @Override + public boolean execute(CommandSender sender, String currentAlias, String[] args) { + if (!testPermission(sender)) return true; + + Player player; + + if (args.length == 0) { + if (sender instanceof Player) { + player = (Player) sender; + } else { + sender.sendMessage("Please provide a player!"); + return true; + } + } else { + player = Bukkit.getPlayerExact(args[0]); + } + + World world = player.getWorld(); + + if (args.length == 4) { + if (world != null) { + int pos = 1; + int maxPos = 30000000; + int x = getInteger(sender, args[pos++], -maxPos, maxPos); + int y = getInteger(sender, args[pos++], 0, world.getMaxHeight()); + int z = getInteger(sender, args[pos], -maxPos, maxPos); + + player.setBedSpawnLocation(new Location(world, x, y, z), true); + sender.sendMessage("Set " + player.getDisplayName() + "'s spawnpoint to " + x + ", " + y + ", " + z); + } + } else if (args.length <= 1) { + Location location = player.getLocation(); + player.setBedSpawnLocation(location, true); + sender.sendMessage("Set " + player.getDisplayName() + "'s spawnpoint to " + location.getX() + ", " + location.getY() + ", " + location.getZ()); + } else { + sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); + return false; + } + + return true; + } + + @Override + public boolean matches(String input) { + return input.equalsIgnoreCase("spawnpoint"); + } + + @Override + public List tabComplete(CommandSender sender, String alias, String[] args) { + 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 super.tabComplete(sender, alias, args); + } + + return ImmutableList.of(); + } +}