PocketMine-MP 5.19.1 git-5cc1068cd43264d3363295eb8d6901e02f467897
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 if($target !== null){
41 $player = $sender->getServer()->getPlayerByPrefix($target);
42 }elseif($sender instanceof Player){
43 $player = $sender;
44 }else{
46 }
47
48 if($player === null){
49 $sender->sendMessage(KnownTranslationFactory::commands_generic_player_notFound()->prefix(TextFormat::RED));
50 return null;
51 }
52 if(
53 ($player === $sender && $this->testPermission($sender, $selfPermission)) ||
54 ($player !== $sender && $this->testPermission($sender, $otherPermission))
55 ){
56 return $player;
57 }
58 return null;
59 }
60
61 protected function getInteger(CommandSender $sender, string $value, int $min = self::MIN_COORD, int $max = self::MAX_COORD) : int{
62 $i = (int) $value;
63
64 if($i < $min){
65 $i = $min;
66 }elseif($i > $max){
67 $i = $max;
68 }
69
70 return $i;
71 }
72
73 protected function getRelativeDouble(float $original, CommandSender $sender, string $input, float $min = self::MIN_COORD, float $max = self::MAX_COORD) : float{
74 if($input[0] === "~"){
75 $value = $this->getDouble($sender, substr($input, 1));
76
77 return $original + $value;
78 }
79
80 return $this->getDouble($sender, $input, $min, $max);
81 }
82
83 protected function getDouble(CommandSender $sender, string $value, float $min = self::MIN_COORD, float $max = self::MAX_COORD) : float{
84 $i = (double) $value;
85
86 if($i < $min){
87 $i = $min;
88 }elseif($i > $max){
89 $i = $max;
90 }
91
92 return $i;
93 }
94
95 protected function getBoundedInt(CommandSender $sender, string $input, int $min, int $max) : ?int{
96 if(!is_numeric($input)){
98 }
99
100 $v = (int) $input;
101 if($v > $max){
102 $sender->sendMessage(KnownTranslationFactory::commands_generic_num_tooBig($input, (string) $max)->prefix(TextFormat::RED));
103 return null;
104 }
105 if($v < $min){
106 $sender->sendMessage(KnownTranslationFactory::commands_generic_num_tooSmall($input, (string) $min)->prefix(TextFormat::RED));
107 return null;
108 }
109
110 return $v;
111 }
112}