PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
SpawnpointCommand.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\command\defaults;
23 
29 
31 
32  public function __construct($name){
33  parent::__construct(
34  $name,
35  "Sets a player's spawn point",
36  "/spawnpoint OR /spawnpoint <player> OR /spawnpoint <player> <x> <y> <z>"
37  );
38  $this->setPermission("pocketmine.command.spawnpoint");
39  }
40 
41  public function execute(CommandSender $sender, $currentAlias, array $args){
42  if(!$this->testPermission($sender)){
43  return true;
44  }
45 
46  $target = null;
47 
48  if(count($args) === 0){
49  if($sender instanceof Player){
50  $target = $sender;
51  }else{
52  $sender->sendMessage(TextFormat::RED . "Please provide a player!");
53 
54  return true;
55  }
56  }else{
57  $target = $sender->getServer()->getPlayer($args[0]);
58  if($target === null){
59  $sender->sendMessage(TextFormat::RED . "Can't find player " . $args[0]);
60 
61  return true;
62  }
63  }
64 
65  $level = $target->getLevel();
66 
67  if(count($args) === 4){
68  if($level !== null){
69  $pos = $sender instanceof Player ? $sender->getPosition() : $level->getSpawnLocation();
70  $x = (int) $this->getRelativeDouble($pos->x, $sender, $args[1]);
71  $y = $this->getRelativeDouble($pos->y, $sender, $args[2], 0, 128);
72  $z = $this->getRelativeDouble($pos->z, $sender, $args[3]);
73  $target->setSpawn(new Position($x, $y, $z, $level));
74  Command::broadcastCommandMessage($sender, "Set " . $target->getDisplayName() . "'s spawnpoint to " . $x . ", " . $y . ", " . $z);
75 
76  return true;
77  }
78  }elseif(count($args) <= 1){
79  if($sender instanceof Player){
80  $pos = new Position((int) $sender->x, (int) $sender->y, (int) $sender->z, $sender->getLevel());
81  $target->setSpawn($pos);
82  Command::broadcastCommandMessage($sender, "Set " . $target->getDisplayName() . "'s spawnpoint to " . $pos->x . ", " . $pos->y . ", " . $pos->z);
83 
84  return true;
85  }else{
86  $sender->sendMessage(TextFormat::RED . "Please provide a player!");
87 
88  return true;
89  }
90  }
91 
92  $sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage);
93 
94  return true;
95  }
96 }
static broadcastCommandMessage(CommandSender $source, $message, $sendToSource=true)
Definition: Command.php:287
testPermission(CommandSender $target)
Definition: Command.php:121
setPermission($permission)
Definition: Command.php:112