Skip to content

Commit aacd634

Browse files
committed
Remove old embedded components and use standalone modules
- Removed components/database, components/hud, components/serialization (now in LibDB, LibHud, LibSerializer) - Added HelpGenerator for command system - Updated LibraryComponents, LibraryModules and PluginToolkit - Updated ArrayUtils and Number utilities
1 parent b4d1831 commit aacd634

25 files changed

Lines changed: 337 additions & 1918 deletions

plugin.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ api: 5.0.0
88
main: Library
99
load: STARTUP
1010

11+
softdepend:
12+
- LibPacket
13+
- LibCommand
14+
- LibForm
15+
- LibHud
16+
- LibDB
17+
- LibSerializer
18+
1119
extensions:
1220
Core: ">=8.2"
1321

src/LibraryComponents.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use imperazim\components\world\WorldManager;
99
use imperazim\components\plugin\PluginToolkit;
1010
use imperazim\components\trigger\TriggerManager;
11-
use imperazim\components\hud\bossbar\BossBarManager;
1211

1312
use imperazim\vendor\invmenu\InvMenuManager;
1413
use imperazim\vendor\customies\CustomiesManager;
@@ -26,7 +25,6 @@ final class LibraryComponents {
2625
/** @var array */
2726
private array $componentClasses = [
2827
'World' => WorldManager::class,
29-
'BossBar' => BossBarManager::class,
3028
'InvMenu' => InvMenuManager::class,
3129
'Triggers' => TriggerManager::class,
3230
'BugFixes' => BugFixesManager::class,

src/LibraryModules.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
declare(strict_types = 1);
44

5+
use pocketmine\event\Listener;
56
use pocketmine\plugin\PluginBase;
7+
use pocketmine\event\server\DataPacketReceiveEvent;
8+
use pocketmine\network\mcpe\protocol\BossEventPacket;
69
use imperazim\command\LibCommandHooker;
710

811
/**
@@ -16,9 +19,35 @@ final class LibraryModules {
1619
*/
1720
public static function loadModules(PluginBase $plugin): void {
1821
$pluginManager = $plugin->getServer()->getPluginManager();
19-
if ($pluginManager->getPlugin('LibCommand') === null) {
22+
if ($pluginManager->getPlugin('LibCommand') === null
23+
&& $pluginManager->getPlugin('LibPacket') === null) {
2024
LibCommandHooker::registerInterceptor($plugin);
2125
}
26+
if ($pluginManager->getPlugin('LibHud') === null) {
27+
$pluginManager->registerEvents(
28+
new LibHudListener(),
29+
$plugin
30+
);
31+
}
2232
}
2333

24-
}
34+
}
35+
36+
/**
37+
* @internal Embedded LibHud listener for when standalone LibHud is not installed.
38+
*/
39+
final class LibHudListener implements Listener {
40+
41+
public function onDataPacketReceiveEvent(DataPacketReceiveEvent $e): void {
42+
$pk = $e->getPacket();
43+
if ($pk instanceof BossEventPacket) {
44+
switch ($pk->eventType) {
45+
case BossEventPacket::TYPE_REGISTER_PLAYER:
46+
case BossEventPacket::TYPE_UNREGISTER_PLAYER:
47+
break;
48+
default:
49+
$e->getOrigin()->getPlayer()->kick(reason: "Invalid packet received");
50+
}
51+
}
52+
}
53+
}
Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace imperazim\command;
6+
7+
use imperazim\command\Command;
8+
use imperazim\command\SubCommand;
9+
use imperazim\command\argument\Argument;
10+
use imperazim\command\constraint\Constraint;
11+
12+
/**
13+
* Generates automatic help text for commands.
14+
*/
15+
final class HelpGenerator {
16+
17+
/**
18+
* Generates help text for a command.
19+
*
20+
* @param Command $command The command
21+
* @param string $format The format style (detailed, compact, usage)
22+
* @return string The help text
23+
*/
24+
public static function generate(Command $command, string $format = "detailed"): string {
25+
return match ($format) {
26+
"compact" => self::generateCompact($command),
27+
"usage" => self::generateUsage($command),
28+
default => self::generateDetailed($command)
29+
};
30+
}
31+
32+
/**
33+
* Generates detailed help text.
34+
*
35+
* @param Command $command The command
36+
* @return string The help text
37+
*/
38+
private static function generateDetailed(Command $command): string {
39+
$lines = [];
40+
41+
// Header
42+
$lines[] = "§e§l" . strtoupper($command->getName()) . " COMMAND";
43+
$lines[] = "§r§7" . $command->getDescription();
44+
$lines[] = "";
45+
46+
// Usage
47+
$lines[] = "§eUsage:";
48+
$lines[] = " §7/" . self::buildUsageLine($command);
49+
$lines[] = "";
50+
51+
// Arguments
52+
if (!empty($command->getArguments())) {
53+
$lines[] = "§eArguments:";
54+
foreach ($command->getArguments() as $arg) {
55+
$name = $arg->getName();
56+
$type = $arg->getTypeName();
57+
$desc = $arg->getDescription();
58+
$optional = $arg->isOptional();
59+
$default = $arg->getDefault();
60+
61+
$prefix = $optional ? " §7[" : " §7<";
62+
$suffix = $optional ? "]" : ">";
63+
$line = $prefix . $name . "§8:" . $type . $suffix;
64+
65+
if ($desc !== "") {
66+
$line .= " §f- " . $desc;
67+
}
68+
69+
if ($optional && $default !== null) {
70+
$line .= " §8(default: §7" . self::formatValue($default) . "§8)";
71+
}
72+
73+
$lines[] = $line;
74+
}
75+
$lines[] = "";
76+
}
77+
78+
// Subcommands
79+
if (!empty($command->getSubCommands())) {
80+
$lines[] = "§eSubcommands:";
81+
foreach ($command->getSubCommands() as $sub) {
82+
$name = $sub->getName();
83+
$desc = $sub->getDescription();
84+
$aliases = $sub->getAliases();
85+
86+
$line = " §7" . $name;
87+
88+
if (!empty($aliases)) {
89+
$line .= " §8(" . implode(", ", $aliases) . ")";
90+
}
91+
92+
if ($desc !== "") {
93+
$line .= " §f- " . $desc;
94+
}
95+
96+
$lines[] = $line;
97+
}
98+
$lines[] = "";
99+
}
100+
101+
// Constraints
102+
if (!empty($command->getConstraints())) {
103+
$lines[] = "§eRequirements:";
104+
foreach ($command->getConstraints() as $constraint) {
105+
$desc = $constraint->getDescription();
106+
if ($desc !== "") {
107+
$lines[] = " §7• " . $desc;
108+
}
109+
}
110+
$lines[] = "";
111+
}
112+
113+
return implode("\n", $lines);
114+
}
115+
116+
/**
117+
* Generates compact help text.
118+
*
119+
* @param Command $command The command
120+
* @return string The help text
121+
*/
122+
private static function generateCompact(Command $command): string {
123+
$lines = [];
124+
125+
$lines[] = "§e/" . self::buildUsageLine($command);
126+
$lines[] = "§7" . $command->getDescription();
127+
128+
if (!empty($command->getSubCommands())) {
129+
$subNames = array_map(fn($s) => $s->getName(), $command->getSubCommands());
130+
$lines[] = "§8Subcommands: §7" . implode("§8, §7", $subNames);
131+
}
132+
133+
return implode("\n", $lines);
134+
}
135+
136+
/**
137+
* Generates usage-only help text.
138+
*
139+
* @param Command $command The command
140+
* @return string The usage line
141+
*/
142+
private static function generateUsage(Command $command): string {
143+
return "§7/" . self::buildUsageLine($command);
144+
}
145+
146+
/**
147+
* Builds the usage line for a command.
148+
*
149+
* @param Command $command The command
150+
* @return string The usage line
151+
*/
152+
private static function buildUsageLine(Command $command): string {
153+
$parts = [$command->getName()];
154+
155+
if (!empty($command->getSubCommands())) {
156+
$subNames = array_map(fn($s) => $s->getName(), $command->getSubCommands());
157+
$parts[] = "<" . implode("|", $subNames) . ">";
158+
}
159+
160+
foreach ($command->getArguments() as $arg) {
161+
$name = $arg->getName();
162+
$aliases = $arg->getAliases();
163+
164+
if (!empty($aliases)) {
165+
$name .= "|" . implode("|", $aliases);
166+
}
167+
168+
if ($arg->isOptional()) {
169+
$parts[] = "[" . $name . "]";
170+
} else {
171+
$parts[] = "<" . $name . ">";
172+
}
173+
}
174+
175+
return implode(" ", $parts);
176+
}
177+
178+
/**
179+
* Generates help text for a subcommand.
180+
*
181+
* @param SubCommand $subcommand The subcommand
182+
* @param string $parentName The parent command name
183+
* @param string $format The format style
184+
* @return string The help text
185+
*/
186+
public static function generateSubCommand(
187+
SubCommand $subcommand,
188+
string $parentName,
189+
string $format = "detailed"
190+
): string {
191+
$lines = [];
192+
193+
if ($format === "detailed") {
194+
// Header
195+
$lines[] = "§e§l" . strtoupper($parentName . " " . $subcommand->getName());
196+
$lines[] = "§r§7" . $subcommand->getDescription();
197+
$lines[] = "";
198+
199+
// Usage
200+
$lines[] = "§eUsage:";
201+
$lines[] = " §7/" . self::buildSubCommandUsageLine($subcommand, $parentName);
202+
$lines[] = "";
203+
204+
// Arguments
205+
if (!empty($subcommand->getArguments())) {
206+
$lines[] = "§eArguments:";
207+
foreach ($subcommand->getArguments() as $arg) {
208+
$name = $arg->getName();
209+
$type = $arg->getTypeName();
210+
$desc = $arg->getDescription();
211+
$optional = $arg->isOptional();
212+
213+
$prefix = $optional ? " §7[" : " §7<";
214+
$suffix = $optional ? "]" : ">";
215+
$line = $prefix . $name . "§8:" . $type . $suffix;
216+
217+
if ($desc !== "") {
218+
$line .= " §f- " . $desc;
219+
}
220+
221+
$lines[] = $line;
222+
}
223+
$lines[] = "";
224+
}
225+
} else {
226+
$lines[] = "§7/" . self::buildSubCommandUsageLine($subcommand, $parentName);
227+
$lines[] = "§7" . $subcommand->getDescription();
228+
}
229+
230+
return implode("\n", $lines);
231+
}
232+
233+
/**
234+
* Builds the usage line for a subcommand.
235+
*
236+
* @param SubCommand $subcommand The subcommand
237+
* @param string $parentName The parent command name
238+
* @return string The usage line
239+
*/
240+
private static function buildSubCommandUsageLine(
241+
SubCommand $subcommand,
242+
string $parentName
243+
): string {
244+
$parts = [$parentName, $subcommand->getName()];
245+
246+
foreach ($subcommand->getArguments() as $arg) {
247+
$name = $arg->getName();
248+
249+
if ($arg->isOptional()) {
250+
$parts[] = "[" . $name . "]";
251+
} else {
252+
$parts[] = "<" . $name . ">";
253+
}
254+
}
255+
256+
return implode(" ", $parts);
257+
}
258+
259+
/**
260+
* Formats a value for display.
261+
*
262+
* @param mixed $value The value
263+
* @return string The formatted value
264+
*/
265+
private static function formatValue($value): string {
266+
if (is_bool($value)) {
267+
return $value ? "true" : "false";
268+
}
269+
270+
if (is_array($value)) {
271+
return "[" . implode(", ", array_map([self::class, "formatValue"], $value)) . "]";
272+
}
273+
274+
if (is_string($value)) {
275+
return '"' . $value . '"';
276+
}
277+
278+
return (string) $value;
279+
}
280+
}

src/imperazim/components/database/DataBaseManager.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)