|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Core\Services; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Core\DataProviders\Listener\ChatCommandInterface as ChatCommandPluginInterface; |
|
6
|
|
|
use eXpansion\Core\Exceptions\ChatCommand\CommandExistException; |
|
7
|
|
|
use eXpansion\Core\Model\ChatCommand\ChatCommandInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ChatCommands |
|
11
|
|
|
* |
|
12
|
|
|
* @package eXpansion\Core\Services; |
|
13
|
|
|
* @author oliver de Cramer <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class ChatCommands |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var ChatCommandInterface[] */ |
|
18
|
|
|
protected $commands; |
|
19
|
|
|
|
|
20
|
|
|
/** @var ChatCommandInterface[] */ |
|
21
|
|
|
protected $commandPlugin; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Register chat commands of a plugin. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $pluginId |
|
27
|
|
|
* @param ChatCommandPluginInterface $pluginService |
|
28
|
|
|
* |
|
29
|
|
|
* @throws CommandExistException |
|
30
|
|
|
*/ |
|
31
|
2 |
|
public function registerPlugin($pluginId, ChatCommandPluginInterface $pluginService) |
|
32
|
|
|
{ |
|
33
|
2 |
|
$commands = $pluginService->getChatCommands(); |
|
34
|
|
|
|
|
35
|
2 |
|
foreach($commands as $command) |
|
36
|
|
|
{ |
|
37
|
2 |
|
$this->addCommand($pluginId, $command->getCommand(), $command); |
|
38
|
|
|
|
|
39
|
2 |
|
foreach ($command->getAliases() as $alias) { |
|
40
|
2 |
|
$this->addCommand($pluginId, $alias, $command); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Remove all chat commands registered for a plugin. |
|
47
|
|
|
* |
|
48
|
|
|
* @param $pluginId |
|
49
|
|
|
*/ |
|
50
|
2 |
|
public function deletePlugin($pluginId) |
|
51
|
|
|
{ |
|
52
|
2 |
|
if(!isset($this->commandPlugin[$pluginId])) { |
|
53
|
1 |
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
foreach ($this->commandPlugin[$pluginId] as $cmdTxt => $command) { |
|
|
|
|
|
|
57
|
1 |
|
unset($this->commands[$cmdTxt]); |
|
58
|
|
|
} |
|
59
|
1 |
|
unset($this->commandPlugin[$pluginId]); |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get a chat command. |
|
64
|
|
|
* |
|
65
|
|
|
* @param $command |
|
66
|
|
|
* @return ChatCommandInterface|null |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function getChatCommand($command) |
|
69
|
|
|
{ |
|
70
|
1 |
|
return isset($this->commands[$command]) ? $this->commands[$command] : null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* |
|
75
|
|
|
* @param $pluginId |
|
76
|
|
|
* @param $cmdTxt |
|
77
|
|
|
* @param ChatCommandInterface $command |
|
78
|
|
|
* |
|
79
|
|
|
* @throws CommandExistException |
|
80
|
|
|
*/ |
|
81
|
2 |
|
protected function addCommand($pluginId, $cmdTxt, ChatCommandInterface $command) |
|
82
|
|
|
{ |
|
83
|
2 |
|
if (isset($this->commands[$cmdTxt])) { |
|
84
|
1 |
|
throw new CommandExistException( |
|
85
|
1 |
|
"$pluginId tries to register command '$cmdTxt' already registered" |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
$this->commands[$cmdTxt] = $command; |
|
90
|
2 |
|
$this->commandPlugin[$pluginId][$cmdTxt] = $command; |
|
91
|
|
|
} |
|
92
|
|
|
} |