PocketMine-MP 5.36.1 git-eaa7c4834c8fe2f379d24e7f0ee6cc63cfb18ccc
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(
40 string $testPermissionContext,
41 CommandSender $sender,
42 ?string $target,
43 string $selfPermission,
44 string $otherPermission
45 ) : ?Player{
46 //TODO: we need proper command selector support, but this one is useful and easy to hack in for now
47 if($target !== null && $target !== "@s"){
48 $player = $sender->getServer()->getPlayerByPrefix($target);
49 }elseif($sender instanceof Player){
50 $player = $sender;
51 }else{
53 }
54
55 if($player === null){
56 $sender->sendMessage(KnownTranslationFactory::commands_generic_player_notFound()->prefix(TextFormat::RED));
57 return null;
58 }
59 //TODO: using loud testPermission here will generate misleading messages
60 //e.g. if the sender has self permission and tries to use the command on another player, it will give them a
61 //generic message saying that they don't have permission to use the command, which is not correct
62 if(
63 ($player === $sender && $this->testPermission($testPermissionContext, $sender, $selfPermission)) ||
64 ($player !== $sender && $this->testPermission($testPermissionContext, $sender, $otherPermission))
65 ){
66 return $player;
67 }
68 return null;
69 }
70
71 protected function getInteger(CommandSender $sender, string $value, int $min = self::MIN_COORD, int $max = self::MAX_COORD) : int{
72 $i = (int) $value;
73
74 if($i < $min){
75 $i = $min;
76 }elseif($i > $max){
77 $i = $max;
78 }
79
80 return $i;
81 }
82
83 protected function getRelativeDouble(float $original, CommandSender $sender, string $input, float $min = self::MIN_COORD, float $max = self::MAX_COORD) : float{
84 if($input[0] === "~"){
85 $value = $this->getDouble($sender, substr($input, 1));
86
87 return $original + $value;
88 }
89
90 return $this->getDouble($sender, $input, $min, $max);
91 }
92
93 protected function getDouble(CommandSender $sender, string $value, float $min = self::MIN_COORD, float $max = self::MAX_COORD) : float{
94 $i = (double) $value;
95
96 if($i < $min){
97 $i = $min;
98 }elseif($i > $max){
99 $i = $max;
100 }
101
102 return $i;
103 }
104
105 protected function getBoundedInt(CommandSender $sender, string $input, int $min, int $max) : ?int{
106 if(!is_numeric($input)){
108 }
109
110 $v = (int) $input;
111 if($v > $max){
112 $sender->sendMessage(KnownTranslationFactory::commands_generic_num_tooBig($input, (string) $max)->prefix(TextFormat::RED));
113 return null;
114 }
115 if($v < $min){
116 $sender->sendMessage(KnownTranslationFactory::commands_generic_num_tooSmall($input, (string) $min)->prefix(TextFormat::RED));
117 return null;
118 }
119
120 return $v;
121 }
122}
testPermission(string $context, CommandSender $target, ?string $permission=null)
Definition Command.php:125