PocketMine-MP 5.15.1 git-be6754494fdbbb9dd57c058ba0e33a4a78c4581f
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
22declare(strict_types=1);
23
24namespace pocketmine\command\defaults;
25
34use function count;
35use function round;
36
38
39 public function __construct(){
40 parent::__construct(
41 "spawnpoint",
42 KnownTranslationFactory::pocketmine_command_spawnpoint_description(),
43 KnownTranslationFactory::commands_spawnpoint_usage()
44 );
45 $this->setPermissions([
46 DefaultPermissionNames::COMMAND_SPAWNPOINT_SELF,
47 DefaultPermissionNames::COMMAND_SPAWNPOINT_OTHER
48 ]);
49 }
50
51 public function execute(CommandSender $sender, string $commandLabel, array $args){
52 $target = $this->fetchPermittedPlayerTarget($sender, $args[0] ?? null, DefaultPermissionNames::COMMAND_SPAWNPOINT_SELF, DefaultPermissionNames::COMMAND_SPAWNPOINT_OTHER);
53 if($target === null){
54 return true;
55 }
56
57 if(count($args) === 4){
58 $world = $target->getWorld();
59 $pos = $sender instanceof Player ? $sender->getPosition() : $world->getSpawnLocation();
60 $x = $this->getRelativeDouble($pos->x, $sender, $args[1]);
61 $y = $this->getRelativeDouble($pos->y, $sender, $args[2], World::Y_MIN, World::Y_MAX);
62 $z = $this->getRelativeDouble($pos->z, $sender, $args[3]);
63 $target->setSpawn(new Position($x, $y, $z, $world));
64
65 Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_spawnpoint_success($target->getName(), (string) round($x, 2), (string) round($y, 2), (string) round($z, 2)));
66
67 return true;
68 }elseif(count($args) <= 1 && $sender instanceof Player){
69 $cpos = $sender->getPosition();
70 $pos = Position::fromObject($cpos->floor(), $cpos->getWorld());
71 $target->setSpawn($pos);
72
73 Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_spawnpoint_success($target->getName(), (string) round($pos->x, 2), (string) round($pos->y, 2), (string) round($pos->z, 2)));
74 return true;
75 }
76
78 }
79}
setPermissions(array $permissions)
Definition: Command.php:96
execute(CommandSender $sender, string $commandLabel, array $args)
static fromObject(Vector3 $pos, ?World $world)
Definition: Position.php:45