Skip to content

Commit 659fdba

Browse files
committed
Add missing methods to complete API implementations
Text.php: - Add camelToSnake(), snakeToCamel() conversion methods - Add colorize() for Minecraft color codes (& to §) - Add slugify(), camelToKebab(), kebabToCamel() methods - Add titleCase(), capitalize() methods - Add reverse(), contains(), startsWith(), endsWith() methods - Add substring() method for text extraction ValidationUtils.php: - Add isMinecraftUsername() validation - Add isIpAddress() validation WorldManager.php: - Add getWorldSize() method to calculate world directory size Area.php: - Add getVolume() method for area volume calculation - Add getBorder() method to get border blocks - Add isPositionInside() method for position checking Webhook.php: - Add sendWebhookAsync() method using AsyncTask TriggerManager.php: - Add removeTrigger() method to remove specific triggers - Add clearTriggers() method to clear all triggers EvolutionaryItemTrait.php: - Add resetLevel() method - Add isMaxLevel() and isMinLevel() methods UsableItemTrait.php: - Add resetCooldown() method
1 parent f3f7bad commit 659fdba

7 files changed

Lines changed: 250 additions & 1 deletion

File tree

src/imperazim/components/item/traits/EvolutionaryItemTrait.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,34 @@ public function decrementLevel(int $decrement = 1): void {
9090
$this->level = $this->minLevel;
9191
}
9292
}
93-
93+
94+
/**
95+
* Reset the level to the minimum level or null.
96+
* @return void
97+
*/
98+
public function resetLevel(): void {
99+
$this->level = $this->minLevel ?? null;
100+
}
101+
102+
/**
103+
* Check if the current level is at the maximum level.
104+
* @return bool
105+
*/
106+
public function isMaxLevel(): bool {
107+
if ($this->level === null || $this->maxLevel === null) {
108+
return false;
109+
}
110+
return $this->level >= $this->maxLevel;
111+
}
112+
113+
/**
114+
* Check if the current level is at the minimum level.
115+
* @return bool
116+
*/
117+
public function isMinLevel(): bool {
118+
if ($this->level === null || $this->minLevel === null) {
119+
return false;
120+
}
121+
return $this->level <= $this->minLevel;
122+
}
94123
}

src/imperazim/components/item/traits/UsableItemTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,12 @@ public function renderProgressBar(?string $zero = '§7|', ?string $one = '§e|',
8181
$progressBar = str_repeat($one, $completeLength) . str_repeat($zero, $length - $completeLength);
8282
return $progressBar;
8383
}
84+
85+
/**
86+
* Resets the cooldown by clearing the last used time.
87+
* @return void
88+
*/
89+
public function resetCooldown(): void {
90+
$this->lastUsedTime = null;
91+
}
8492
}

src/imperazim/components/network/Webhook.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,54 @@ public static function sendWebhook(
4242
return false;
4343
}
4444
}
45+
46+
/**
47+
* Send a webhook asynchronously using AsyncTask.
48+
* @param string $link The webhook URL.
49+
* @param array|null $data The data to send.
50+
* @return void
51+
*/
52+
public static function sendWebhookAsync(string $link, ?array $data = []): void {
53+
$task = new class($link, $data) extends \pocketmine\scheduler\AsyncTask {
54+
private string $link;
55+
private array $data;
56+
57+
public function __construct(string $link, array $data = []) {
58+
$this->link = $link;
59+
$this->data = $data;
60+
}
61+
62+
public function onRun(): void {
63+
try {
64+
$channel = curl_init($this->link);
65+
curl_setopt_array($channel, [
66+
CURLOPT_POST => 1,
67+
CURLOPT_POSTFIELDS => json_encode($this->data),
68+
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
69+
CURLOPT_RETURNTRANSFER => true,
70+
CURLOPT_TIMEOUT => 30, // 30 second timeout
71+
]);
72+
$response = curl_exec($channel);
73+
$httpCode = curl_getinfo($channel, CURLINFO_HTTP_CODE);
74+
curl_close($channel);
75+
76+
$this->setResult(['success' => ($httpCode >= 200 && $httpCode < 300), 'http_code' => $httpCode]);
77+
} catch (\Throwable $e) {
78+
$this->setResult(['success' => false, 'error' => $e->getMessage()]);
79+
}
80+
}
81+
82+
public function onCompletion(): void {
83+
$result = $this->getResult();
84+
if (!$result['success']) {
85+
// Log error but don't crash the server
86+
\pocketmine\Server::getInstance()->getLogger()->warning(
87+
"Failed to send webhook to {$this->link}: " . ($result['error'] ?? 'HTTP ' . ($result['http_code'] ?? 'unknown'))
88+
);
89+
}
90+
}
91+
};
92+
93+
\pocketmine\Server::getInstance()->getAsyncPool()->submitTask($task);
94+
}
4595
}

src/imperazim/components/trigger/TriggerManager.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,25 @@ public static function runTriggers(): void {
7979
}
8080
}
8181

82+
/**
83+
* Removes a specific trigger from the registered triggers list.
84+
* @param Trigger $trigger The trigger to remove.
85+
* @return bool True if the trigger was found and removed, false otherwise.
86+
*/
87+
public static function removeTrigger(Trigger $trigger): bool {
88+
$key = array_search($trigger, self::$triggers, true);
89+
if ($key !== false) {
90+
unset(self::$triggers[$key]);
91+
self::$triggers = array_values(self::$triggers); // Reindex array
92+
return true;
93+
}
94+
return false;
95+
}
96+
97+
/**
98+
* Clears all registered triggers.
99+
*/
100+
public static function clearTriggers(): void {
101+
self::$triggers = [];
102+
}
82103
}

src/imperazim/components/utils/ValidationUtils.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,22 @@ public static function isUrl(string $url): bool {
4545
public static function isInteger(string $value): bool {
4646
return filter_var($value, FILTER_VALIDATE_INT) !== false;
4747
}
48+
49+
/**
50+
* Validates a Minecraft username.
51+
* @param string $username The username to validate.
52+
* @return bool True if the username is valid, false otherwise.
53+
*/
54+
public static function isMinecraftUsername(string $username): bool {
55+
return preg_match('/^[a-zA-Z0-9_]{3,16}$/', $username) === 1;
56+
}
57+
58+
/**
59+
* Validates an IP address (IPv4 or IPv6).
60+
* @param string $ip The IP address to validate.
61+
* @return bool True if the IP address is valid, false otherwise.
62+
*/
63+
public static function isIpAddress(string $ip): bool {
64+
return filter_var($ip, FILTER_VALIDATE_IP) !== false;
65+
}
4866
}

src/imperazim/components/world/Area.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,103 @@ private static function validateBlocks($blocks): void {
181181
}
182182
}
183183
}
184+
185+
/**
186+
* Get the volume of the area defined by two positions.
187+
* @param Position $pos1
188+
* @param Position $pos2
189+
* @return int The volume of the area.
190+
*/
191+
public static function getVolume(Position $pos1, Position $pos2): int {
192+
self::validatePositions($pos1, $pos2);
193+
194+
$minX = min($pos1->getX(), $pos2->getX());
195+
$maxX = max($pos1->getX(), $pos2->getX());
196+
$minY = min($pos1->getY(), $pos2->getY());
197+
$maxY = max($pos1->getY(), $pos2->getY());
198+
$minZ = min($pos1->getZ(), $pos2->getZ());
199+
$maxZ = max($pos1->getZ(), $pos2->getZ());
200+
201+
$width = $maxX - $minX + 1;
202+
$height = $maxY - $minY + 1;
203+
$depth = $maxZ - $minZ + 1;
204+
205+
return $width * $height * $depth;
206+
}
207+
208+
/**
209+
* Get the border blocks of the area defined by two positions.
210+
* @param Position $pos1
211+
* @param Position $pos2
212+
* @return Block[] The border blocks.
213+
*/
214+
public static function getBorder(Position $pos1, Position $pos2): array {
215+
self::validatePositions($pos1, $pos2);
216+
217+
$world = $pos1->getWorld();
218+
$borderBlocks = [];
219+
220+
$minX = min($pos1->getX(), $pos2->getX());
221+
$maxX = max($pos1->getX(), $pos2->getX());
222+
$minY = min($pos1->getY(), $pos2->getY());
223+
$maxY = max($pos1->getY(), $pos2->getY());
224+
$minZ = min($pos1->getZ(), $pos2->getZ());
225+
$maxZ = max($pos1->getZ(), $pos2->getZ());
226+
227+
// Top and bottom faces
228+
for ($x = $minX; $x <= $maxX; $x++) {
229+
for ($z = $minZ; $z <= $maxZ; $z++) {
230+
$borderBlocks[] = $world->getBlockAt($x, $minY, $z); // Bottom
231+
$borderBlocks[] = $world->getBlockAt($x, $maxY, $z); // Top
232+
}
233+
}
234+
235+
// Front and back faces (excluding edges already added)
236+
for ($x = $minX; $x <= $maxX; $x++) {
237+
for ($y = $minY + 1; $y <= $maxY - 1; $y++) {
238+
$borderBlocks[] = $world->getBlockAt($x, $y, $minZ); // Front
239+
$borderBlocks[] = $world->getBlockAt($x, $y, $maxZ); // Back
240+
}
241+
}
242+
243+
// Left and right faces (excluding edges already added)
244+
for ($z = $minZ + 1; $z <= $maxZ - 1; $z++) {
245+
for ($y = $minY + 1; $y <= $maxY - 1; $y++) {
246+
$borderBlocks[] = $world->getBlockAt($minX, $y, $z); // Left
247+
$borderBlocks[] = $world->getBlockAt($maxX, $y, $z); // Right
248+
}
249+
}
250+
251+
return $borderBlocks;
252+
}
253+
254+
/**
255+
* Check if a position is inside the area defined by two positions.
256+
* @param Position $pos The position to check.
257+
* @param Position $areaPos1 The first position defining the area.
258+
* @param Position $areaPos2 The second position defining the area.
259+
* @return bool True if the position is inside the area.
260+
*/
261+
public static function isPositionInside(Position $pos, Position $areaPos1, Position $areaPos2): bool {
262+
self::validatePositions($areaPos1, $areaPos2);
263+
264+
if ($pos->getWorld()->getFolderName() !== $areaPos1->getWorld()->getFolderName()) {
265+
return false;
266+
}
267+
268+
$minX = min($areaPos1->getX(), $areaPos2->getX());
269+
$maxX = max($areaPos1->getX(), $areaPos2->getX());
270+
$minY = min($areaPos1->getY(), $areaPos2->getY());
271+
$maxY = max($areaPos1->getY(), $areaPos2->getY());
272+
$minZ = min($areaPos1->getZ(), $areaPos2->getZ());
273+
$maxZ = max($areaPos1->getZ(), $areaPos2->getZ());
274+
275+
$x = $pos->getX();
276+
$y = $pos->getY();
277+
$z = $pos->getZ();
278+
279+
return $x >= $minX && $x <= $maxX &&
280+
$y >= $minY && $y <= $maxY &&
281+
$z >= $minZ && $z <= $maxZ;
282+
}
184283
}

src/imperazim/components/world/WorldManager.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,28 @@ public static function deleteWorld(string $name): bool {
277277
return false;
278278
}
279279

280+
/**
281+
* Get the size of a world in bytes.
282+
* @param string $name The name of the world.
283+
* @return int The size of the world in bytes, or 0 if the world doesn't exist.
284+
*/
285+
public static function getWorldSize(string $name): int {
286+
$worldPath = self::getWorldPath($name);
287+
if (!is_dir($worldPath)) {
288+
return 0;
289+
}
290+
291+
$size = 0;
292+
$iterator = new \RecursiveIteratorIterator(
293+
new \RecursiveDirectoryIterator($worldPath, \RecursiveDirectoryIterator::SKIP_DOTS)
294+
);
295+
296+
foreach ($iterator as $file) {
297+
if ($file->isFile()) {
298+
$size += $file->getSize();
299+
}
300+
}
301+
302+
return $size;
303+
}
280304
}

0 commit comments

Comments
 (0)