PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
TeleportCommand.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  "Teleports the given player (or yourself) to another player or coordinates",
36  "/tp [player] <target> and/or <x> <y> <z>"
37  );
38  $this->setPermission("pocketmine.command.teleport");
39  }
40 
41  public function execute(CommandSender $sender, $currentAlias, array $args){
42  if(!$this->testPermission($sender)){
43  return true;
44  }
45 
46  if(count($args) < 1 or count($args) > 4){
47  $sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage);
48 
49  return true;
50  }
51 
52  $target = null;
53  $origin = $sender;
54 
55  if(count($args) === 1 or count($args) === 3){
56  if($sender instanceof Player){
57  $target = $sender;
58  }else{
59  $sender->sendMessage(TextFormat::RED . "Please provide a player!");
60 
61  return true;
62  }
63  if(count($args) === 1){
64  $target = $sender->getServer()->getPlayer($args[0]);
65  if($target === null){
66  $sender->sendMessage(TextFormat::RED . "Can't find player " . $args[0]);
67 
68  return true;
69  }
70  }
71  }else{
72  $target = $sender->getServer()->getPlayer($args[0]);
73  if($target === null){
74  $sender->sendMessage(TextFormat::RED . "Can't find player " . $args[0]);
75 
76  return true;
77  }
78  if(count($args) === 2){
79  $origin = $target;
80  $target = $sender->getServer()->getPlayer($args[1]);
81  if($target === null){
82  $sender->sendMessage(TextFormat::RED . "Can't find player " . $args[1]);
83 
84  return true;
85  }
86  }
87  }
88 
89  if(count($args) < 3){
90  $origin->teleport($target);
91  Command::broadcastCommandMessage($sender, "Teleported " . $origin->getDisplayName() . " to " . $target->getDisplayName());
92 
93  return true;
94  }elseif($target->getLevel() !== null){
95  $pos = count($args) === 4 ? 1 : 0;
96  $x = $this->getRelativeDouble($target->x, $sender, $args[$pos++]);
97  $y = $this->getRelativeDouble($target->y, $sender, $args[$pos++], 0, 128);
98  $z = $this->getRelativeDouble($target->z, $sender, $args[$pos]);
99  $target->teleport(new Vector3($x, $y, $z));
100  Command::broadcastCommandMessage($sender, "Teleported " . $target->getDisplayName() . " to " . round($x, 2) . ", " . round($y, 2) . ", " . round($z, 2));
101 
102  return true;
103  }
104 
105  $sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage);
106 
107  return true;
108  }
109 }
static broadcastCommandMessage(CommandSender $source, $message, $sendToSource=true)
Definition: Command.php:287
testPermission(CommandSender $target)
Definition: Command.php:121
setPermission($permission)
Definition: Command.php:112