PocketMine-MP 5.35.1 git-f412a390b8f63d0311cc1d1c81046404153b8440
Loading...
Searching...
No Matches
CommandAliasCommand.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 array_map;
35use function array_shift;
36use function count;
37use function implode;
38use function is_array;
39use function ksort;
40
41final class CommandAliasCommand extends Command{
42 private const SELF_PERM = DefaultPermissionNames::COMMAND_CMDALIAS_EDIT_SELF;
43 private const GLOBAL_PERM = DefaultPermissionNames::COMMAND_CMDALIAS_EDIT_GLOBAL;
44 private const LIST_PERM = DefaultPermissionNames::COMMAND_CMDALIAS_LIST;
45
46 public function __construct(string $namespace, string $name){
47 parent::__construct(
48 $namespace,
49 $name,
50 KnownTranslationFactory::pocketmine_command_cmdalias_description(),
51 "/cmdalias [global] create <alias> <target> OR /cmdalias [global] delete <alias>"
52 );
53 $this->setPermissions([self::GLOBAL_PERM, self::SELF_PERM, self::LIST_PERM]);
54 }
55
56 public function execute(CommandSender $sender, string $commandLabel, array $args){
57 if(count($args) === 0){
59 }
60 $parsedArgs = $args;
61 $commandMap = $sender->getServer()->getCommandMap();
62 if($parsedArgs[0] === "global"){
63 $editPermission = self::GLOBAL_PERM;
64 $permissionCtx = $commandLabel . " global";
65 array_shift($parsedArgs);
66 $aliasMap = $commandMap->getAliasMap();
67 $messageScope = fn(Translatable $t) => KnownTranslationFactory::pocketmine_command_cmdalias_template($t, KnownTranslationFactory::pocketmine_command_cmdalias_scope_global());
68 $auditLog = true;
69 }else{
70 $editPermission = self::SELF_PERM;
71 $permissionCtx = $commandLabel;
72 $aliasMap = $sender->getCommandAliasMap();
73 $messageScope = fn(Translatable $t) => KnownTranslationFactory::pocketmine_command_cmdalias_template($t, KnownTranslationFactory::pocketmine_command_cmdalias_scope_userSpecific());
74 $auditLog = false;
75 }
76 $operation = array_shift($parsedArgs);
77
78 if($operation === "create"){
79 if(count($parsedArgs) !== 2){
81 }
82 if(!$this->testPermission($permissionCtx, $sender, $editPermission)){
83 return true;
84 }
85
86 [$alias, $target] = $parsedArgs;
87 $command = $commandMap->getCommand($target, $sender->getCommandAliasMap());
88 if($command === null){
89 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_notFound(
90 "/$target",
91 "/" . $sender->getCommandAliasMap()->getPreferredAlias("pocketmine:help", $sender->getServer()->getCommandMap()->getAliasMap())
92 )->prefix(TextFormat::RED));
93 return true;
94 }
95 if(is_array($command)){
96 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_error_aliasConflict("/$target", implode(", ", array_map(fn(Command $c) => "/" . $c->getId(), $command)))->prefix(TextFormat::RED));
97 return true;
98 }
99 $aliasMap->bindAlias($command->getId(), $alias, override: true);
100 $message = $messageScope(KnownTranslationFactory::pocketmine_command_cmdalias_create_success("/$alias", "/" . $command->getId()));
101 if($auditLog){
102 Command::broadcastCommandMessage($sender, $message);
103 }else{
104 $sender->sendMessage($message);
105 }
106 return true;
107 }
108 if($operation === "delete"){
109 if(count($parsedArgs) !== 1){
111 }
112 if(!$this->testPermission($permissionCtx, $sender, $editPermission)){
113 return true;
114 }
115
116 $alias = $parsedArgs[0];
117
118 if($aliasMap->unbindAlias($alias)){
119 $message = $messageScope(KnownTranslationFactory::pocketmine_command_cmdalias_delete_success("/$alias"));
120 if($auditLog){
121 Command::broadcastCommandMessage($sender, $message);
122 }else{
123 $sender->sendMessage($message);
124 }
125 }else{
126 $sender->sendMessage($messageScope(KnownTranslationFactory::pocketmine_command_cmdalias_delete_notFound("/$alias"))->prefix(TextFormat::RED));
127 }
128 return true;
129 }
130 if($operation === "list"){
131 if(count($parsedArgs) !== 0){
133 }
134 if(!$this->testPermission($permissionCtx, $sender, self::LIST_PERM)){
135 return true;
136 }
137 $allAliases = $aliasMap->getAllAliases();
138 if(count($allAliases) === 0){
139 $sender->sendMessage($messageScope(KnownTranslationFactory::pocketmine_command_cmdalias_list_noneSet())->prefix(TextFormat::RED));
140 return true;
141 }
142 ksort($allAliases);
143 foreach(Utils::promoteKeys($allAliases) as $alias => $commandIds){
144 if(is_array($commandIds)){
145 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_cmdalias_list_conflicted(
146 TextFormat::RED . "/$alias" . TextFormat::RESET,
147 implode(", ", array_map(fn(string $c) => TextFormat::RED . "/$c" . TextFormat::RESET, $commandIds))
148 ));
149 }else{
150 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_cmdalias_list_normal(
151 TextFormat::DARK_GREEN . "/$alias" . TextFormat::RESET,
152 TextFormat::DARK_GREEN . "/$commandIds" . TextFormat::RESET
153 ));
154 }
155 }
156 return true;
157 }
158
160 }
161}
testPermission(string $context, CommandSender $target, ?string $permission=null)
Definition Command.php:125
setPermissions(array $permissions)
Definition Command.php:106
execute(CommandSender $sender, string $commandLabel, array $args)