PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
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
22declare(strict_types=1);
23
24namespace pocketmine\command\defaults;
25
36use function array_shift;
37use function count;
38use function round;
39
41
42 public function __construct(){
43 parent::__construct(
44 "tp",
45 KnownTranslationFactory::pocketmine_command_tp_description(),
46 KnownTranslationFactory::commands_tp_usage(),
47 ["teleport"]
48 );
49 $this->setPermissions([
50 DefaultPermissionNames::COMMAND_TELEPORT_SELF,
51 DefaultPermissionNames::COMMAND_TELEPORT_OTHER
52 ]);
53 }
54
55 private function findPlayer(CommandSender $sender, string $playerName) : ?Player{
56 $subject = $sender->getServer()->getPlayerByPrefix($playerName);
57 if($subject === null){
58 $sender->sendMessage(TextFormat::RED . "Can't find player " . $playerName);
59 return null;
60 }
61 return $subject;
62 }
63
64 public function execute(CommandSender $sender, string $commandLabel, array $args){
65 switch(count($args)){
66 case 1: // /tp targetPlayer
67 case 3: // /tp x y z
68 case 5: // /tp x y z yaw pitch - TODO: 5 args could be target x y z yaw :(
69 $subjectName = null; //self
70 break;
71 case 2: // /tp player1 player2
72 case 4: // /tp player1 x y z - TODO: 4 args could be x y z yaw :(
73 case 6: // /tp player1 x y z yaw pitch
74 $subjectName = array_shift($args);
75 break;
76 default:
78 }
79
80 $subject = $this->fetchPermittedPlayerTarget($sender, $subjectName, DefaultPermissionNames::COMMAND_TELEPORT_SELF, DefaultPermissionNames::COMMAND_TELEPORT_OTHER);
81 if($subject === null){
82 return true;
83 }
84
85 switch(count($args)){
86 case 1:
87 $targetPlayer = $this->findPlayer($sender, $args[0]);
88 if($targetPlayer === null){
89 return true;
90 }
91
92 $subject->teleport($targetPlayer->getLocation());
93 Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_tp_success($subject->getName(), $targetPlayer->getName()));
94
95 return true;
96 case 3:
97 case 5:
98 $base = $subject->getLocation();
99 if(count($args) === 5){
100 $yaw = (float) $args[3];
101 $pitch = (float) $args[4];
102 }else{
103 $yaw = $base->yaw;
104 $pitch = $base->pitch;
105 }
106
107 $x = $this->getRelativeDouble($base->x, $sender, $args[0]);
108 $y = $this->getRelativeDouble($base->y, $sender, $args[1], World::Y_MIN, World::Y_MAX);
109 $z = $this->getRelativeDouble($base->z, $sender, $args[2]);
110 $targetLocation = new Location($x, $y, $z, $base->getWorld(), $yaw, $pitch);
111
112 $subject->teleport($targetLocation);
113 Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_tp_success_coordinates(
114 $subject->getName(),
115 (string) round($targetLocation->x, 2),
116 (string) round($targetLocation->y, 2),
117 (string) round($targetLocation->z, 2)
118 ));
119 return true;
120 default:
121 throw new AssumptionFailedError("This branch should be unreachable (for now)");
122 }
123 }
124}
setPermissions(array $permissions)
Definition Command.php:96
execute(CommandSender $sender, string $commandLabel, array $args)