Skip to content

Commit ad46c55

Browse files
committed
New Customies ItemComponent: ItemTaskComponent
1 parent 41ac6e5 commit ad46c55

4 files changed

Lines changed: 107 additions & 5 deletions

File tree

changelogs/1.3.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,7 @@ It is not compatible with plugins written for version 1.3.0 or lower, and plugin
962962
- `ShouldDespawnComponent`: Toggles item despawn mechanics for floating entities
963963
- `ShooterComponent`: Enables projectile launching (requires `UseModifiersComponent`)
964964
- `RecordComponent`: Adds music playback functionality to items
965+
- `ItemTaskComponent`: Creates a new ItemTaskComponent to define a task that checks for specific items in a player's inventory.
965966

966967
### Visual & Interaction
967968
- `HoverTextColorComponent`: Sets custom color for item name hover text

src/imperazim/vendor/customies/CustomiesManager.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,18 @@ public static function init(PluginToolkit $plugin): array {
7272
new self()
7373
],
7474
self::SCHEDULER_COMPONENT => [
75-
'type' => 'delayed',
76-
'class' => new ClosureTask(static function () use ($cachePath): void {
77-
CustomiesBlockFactory::getInstance()->addWorkerInitHook($cachePath);
78-
}),
79-
'sleep' => 0
75+
[
76+
'type' => 'delayed',
77+
'class' => new ClosureTask(static function () use ($cachePath): void {
78+
CustomiesBlockFactory::getInstance()->addWorkerInitHook($cachePath);
79+
}),
80+
'sleep' => 0
81+
],
82+
[
83+
'type' => 'repeating',
84+
'class' => new ItemTask(),
85+
'sleep' => 20
86+
]
8087
]
8188
];
8289
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace imperazim\vendor\customies\item\component;
6+
7+
use Closure;
8+
use pocketmine\player\Player;
9+
use pocketmine\item\Item;
10+
use imperazim\vendor\customies\item\ItemComponents;
11+
12+
final class ItemTaskComponent implements ItemComponents {
13+
14+
public const FILTER_HAND = 'hand';
15+
public const FILTER_INVENTORY = 'inventory';
16+
17+
private string $itemClass;
18+
private Closure $callback;
19+
private string $filter;
20+
21+
/**
22+
* Creates a new ItemTaskComponent to define a task that checks for specific items in a player's inventory.
23+
*
24+
* @param string $itemClass The class of the item to check for (e.g., DiamondSword::class).
25+
* @param Closure $callback The action to execute when the item is found. Receives the Player and Item as parameters.
26+
* @param string $filter The location to check for the item. Use FILTER_HAND to check the main hand or FILTER_INVENTORY to check the entire inventory.
27+
*/
28+
public function __construct(string $itemClass, Closure $callback, string $filter = self::FILTER_INVENTORY) {
29+
$this->itemClass = $itemClass;
30+
$this->callback = $callback;
31+
$this->filter = $filter;
32+
}
33+
34+
public function getName(): string {
35+
return "minecraft:item_task";
36+
}
37+
38+
public function getValue(): array {
39+
return [
40+
'item_class' => $this->itemClass,
41+
'filter' => $this->filter,
42+
];
43+
}
44+
45+
public function isProperty(): bool {
46+
return false;
47+
}
48+
49+
public function checkCondition(Player $player): ?Item {
50+
$inventory = $player->getInventory();
51+
switch ($this->filter) {
52+
case self::FILTER_HAND:
53+
$item = $inventory->getItemInHand();
54+
return $item instanceof $this->itemClass ? $item : null;
55+
default:
56+
foreach ($inventory->getContents() as $item) {
57+
if ($item instanceof $this->itemClass) {
58+
return $item;
59+
}
60+
}
61+
return null;
62+
}
63+
}
64+
65+
public function execute(Player $player, Item $item): void {
66+
($this->callback)($player, $item);
67+
}
68+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace imperazim\vendor\customies\task;
5+
6+
use imperazim\vendor\customies\item\CustomiesItemFactory;
7+
use pocketmine\scheduler\Task;
8+
use pocketmine\Server;
9+
10+
final class ItemTask extends Task {
11+
12+
public function onRun(): void {
13+
foreach (Server::getInstance()->getOnlinePlayers() as $player) {
14+
foreach (CustomiesItemFactory::getInstance()->getItemRegisteredList() as $itemData) {
15+
$item = $itemData['item'];
16+
if (!$item->hasComponent("minecraft:item_task")) continue;
17+
18+
/** @var \imperazim\vendor\customies\item\component\ItemTaskComponent $component */
19+
$component = $item->getComponent("minecraft:item_task");
20+
if ($itemInstance = $component->checkCondition($player)) {
21+
$component->execute($player, $itemInstance);
22+
}
23+
}
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)