PocketMine-MP 5.39.3 git-400eb2dddf91a9c112aa09f3b498ffc8c85e98ed
Loading...
Searching...
No Matches
VanillaCommand.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
32use function is_numeric;
33use function substr;
34
35abstract class VanillaCommand extends Command{
36 public const MAX_COORD = 30000000;
37 public const MIN_COORD = -30000000;
38
39 protected function fetchPermittedPlayerTarget(CommandSender $sender, ?string $target, string $selfPermission, string $otherPermission) : ?Player{
40 //TODO: we need proper command selector support, but this one is useful and easy to hack in for now
41 if($target !== null && $target !== "@s"){
42 $player = $sender->getServer()->getPlayerByPrefix($target);
43 if($player === null){
44 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_error_playerNotFound($target)->prefix(TextFormat::RED));
45 return null;
46 }
47 }elseif($sender instanceof Player){
48 $player = $sender;
49 }else{
51 }
52
53 if(
54 ($player === $sender && $this->testPermission($sender, $selfPermission)) ||
55 ($player !== $sender && $this->testPermission($sender, $otherPermission))
56 ){
57 return $player;
58 }
59 return null;
60 }
61
62 protected function getInteger(CommandSender $sender, string $value, int $min = self::MIN_COORD, int $max = self::MAX_COORD) : int{
63 $i = (int) $value;
64
65 if($i < $min){
66 $i = $min;
67 }elseif($i > $max){
68 $i = $max;
69 }
70
71 return $i;
72 }
73
74 protected function getRelativeDouble(float $original, CommandSender $sender, string $input, float $min = self::MIN_COORD, float $max = self::MAX_COORD) : float{
75 if($input[0] === "~"){
76 $value = $this->getDouble($sender, substr($input, 1));
77
78 return $original + $value;
79 }
80
81 return $this->getDouble($sender, $input, $min, $max);
82 }
83
84 protected function getDouble(CommandSender $sender, string $value, float $min = self::MIN_COORD, float $max = self::MAX_COORD) : float{
85 $i = (float) $value;
86
87 if($i < $min){
88 $i = $min;
89 }elseif($i > $max){
90 $i = $max;
91 }
92
93 return $i;
94 }
95
96 protected function getBoundedInt(CommandSender $sender, string $input, int $min, int $max) : ?int{
97 if(!is_numeric($input)){
99 }
100
101 $v = (int) $input;
102 if($v > $max){
103 $sender->sendMessage(KnownTranslationFactory::commands_generic_num_tooBig($input, (string) $max)->prefix(TextFormat::RED));
104 return null;
105 }
106 if($v < $min){
107 $sender->sendMessage(KnownTranslationFactory::commands_generic_num_tooSmall($input, (string) $min)->prefix(TextFormat::RED));
108 return null;
109 }
110
111 return $v;
112 }
113}