PocketMine-MP 5.35.1 git-f412a390b8f63d0311cc1d1c81046404153b8440
Loading...
Searching...
No Matches
CommandAliasMap.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;
25
26use function array_filter;
27use function array_key_first;
28use function array_keys;
29use function array_values;
30use function count;
31use function is_array;
32
33final class CommandAliasMap{
34
39 private array $aliasToCommandMap = [];
40
45 private array $commandToAliasesMap = [];
46
47 private function mapSingleAlias(string $commandId, string $alias) : bool{
48 $existing = $this->aliasToCommandMap[$alias] ?? null;
49 if($existing !== null){
50 if(!is_array($existing)){
51 //old command can't use this alias anymore, since it's conflicted
52 unset($this->commandToAliasesMap[$existing][$alias]);
53 $existing = [$existing];
54 }
55 $existing[] = $commandId;
56 $this->aliasToCommandMap[$alias] = $existing;
57 return false;
58 }
59
60 $this->commandToAliasesMap[$commandId][$alias] = true;
61 $this->aliasToCommandMap[$alias] = $commandId;
62 return true;
63 }
64
65 public function bindAlias(string $commandId, string $newAlias, bool $override) : void{
66 if($override){
67 //explicit alias registration overrides everything else, including conflicts
68 $this->unbindAlias($newAlias);
69 }
70
71 $this->mapSingleAlias($commandId, $newAlias);
72 }
73
74 public function unbindAlias(string $alias) : bool{
75 $commandIds = $this->aliasToCommandMap[$alias] ?? null;
76 if($commandIds === null){
77 return false;
78 }
79 unset($this->aliasToCommandMap[$alias]);
80 if(!is_array($commandIds)){
81 //this should only be set if the alias wasn't conflicted
82 unset($this->commandToAliasesMap[$commandIds][$alias]);
83 }
84
85 return true;
86 }
87
88 public function unbindAliasesForCommand(string $commandId) : void{
89 foreach($this->getAliases($commandId) as $alias){
90 $aliasMap = $this->aliasToCommandMap[$alias] ?? null;
91
92 if($aliasMap === $commandId){
93 unset($this->aliasToCommandMap[$alias]);
94 }elseif(is_array($aliasMap)){
95 //this may leave 1 command remaining - we purposely don't deconflict it here because successive command
96 //invocations should be predictable. Leave the conflict and let the user override it if they want.
97 $replacement = array_filter($aliasMap, fn(string $cid) => $cid !== $commandId);
98 if(count($replacement) === 0){
99 unset($this->aliasToCommandMap[$alias]);
100 }else{
101 $this->aliasToCommandMap[$alias] = array_values($replacement);
102 }
103 }else{
104 throw new \LogicException("Alias map state corrupted");
105 }
106 }
107 }
108
114 public function getAliases(string $commandId) : array{
115 return array_keys($this->commandToAliasesMap[$commandId] ?? []);
116 }
117
126 public function resolveAlias(string $alias) : string|array|null{
127 return $this->aliasToCommandMap[$alias] ?? null;
128 }
129
138 public function getMergedAliases(string $commandId, CommandAliasMap $fallbackMap) : array{
139 $localAliases = $this->getAliases($commandId);
140
141 foreach($fallbackMap->getAliases($commandId) as $globalAlias){
142 $userMappedCommandId = $this->resolveAlias($globalAlias);
143 if($userMappedCommandId === null){
144 //only include if this map doesn't have this alias at all
145 $localAliases[] = $globalAlias;
146 }
147 }
148
149 $localAliases[] = $commandId;
150
151 return $localAliases;
152 }
153
159 public function getPreferredAlias(string $commandId, CommandAliasMap $fallbackMap) : string{
160 $aliasList = $this->getMergedAliases($commandId, $fallbackMap);
161 return $aliasList[array_key_first($aliasList)];
162 }
163
168 public function getAllAliases() : array{
169 return $this->aliasToCommandMap;
170 }
171}
getPreferredAlias(string $commandId, CommandAliasMap $fallbackMap)
getMergedAliases(string $commandId, CommandAliasMap $fallbackMap)