From eb06095a39acba3a3928ddc4d94270b7e8b77652 Mon Sep 17 00:00:00 2001 From: ipad54 <63200545+ipad54@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:48:58 +0300 Subject: [PATCH 1/6] Implemented item cooldown synchronization --- src/network/mcpe/NetworkSession.php | 10 ++++++++++ src/player/Player.php | 1 + 2 files changed, 11 insertions(+) diff --git a/src/network/mcpe/NetworkSession.php b/src/network/mcpe/NetworkSession.php index 218e1025334..8de4eaa0467 100644 --- a/src/network/mcpe/NetworkSession.php +++ b/src/network/mcpe/NetworkSession.php @@ -30,6 +30,7 @@ use pocketmine\event\server\DataPacketReceiveEvent; use pocketmine\event\server\DataPacketSendEvent; use pocketmine\form\Form; +use pocketmine\item\Item; use pocketmine\lang\KnownTranslationFactory; use pocketmine\lang\Translatable; use pocketmine\math\Vector3; @@ -65,6 +66,7 @@ use pocketmine\network\mcpe\protocol\PacketDecodeException; use pocketmine\network\mcpe\protocol\PacketPool; use pocketmine\network\mcpe\protocol\PlayerListPacket; +use pocketmine\network\mcpe\protocol\PlayerStartItemCooldownPacket; use pocketmine\network\mcpe\protocol\PlayStatusPacket; use pocketmine\network\mcpe\protocol\ProtocolInfo; use pocketmine\network\mcpe\protocol\serializer\PacketBatch; @@ -111,6 +113,7 @@ use pocketmine\utils\BinaryStream; use pocketmine\utils\ObjectSet; use pocketmine\utils\TextFormat; +use pocketmine\world\format\io\GlobalItemDataHandlers; use pocketmine\world\Position; use pocketmine\YmlServerProperties; use function array_map; @@ -1289,6 +1292,13 @@ public function onOpenSignEditor(Vector3 $signPosition, bool $frontSide) : void{ $this->sendDataPacket(OpenSignPacket::create(BlockPosition::fromVector3($signPosition), $frontSide)); } + public function onItemCooldownChanged(Item $item, int $ticks) : void{ + $this->sendDataPacket(PlayerStartItemCooldownPacket::create( + GlobalItemDataHandlers::getSerializer()->serializeType($item)->getName(), + $ticks + )); + } + public function tick() : void{ if(!$this->isConnected()){ $this->dispose(); diff --git a/src/player/Player.php b/src/player/Player.php index d442c6a3b20..42842348472 100644 --- a/src/player/Player.php +++ b/src/player/Player.php @@ -715,6 +715,7 @@ public function resetItemCooldown(Item $item, ?int $ticks = null) : void{ $ticks = $ticks ?? $item->getCooldownTicks(); if($ticks > 0){ $this->usedItemsCooldown[$item->getStateId()] = $this->server->getTick() + $ticks; + $this->networkSession->onItemCooldownChanged($item, $ticks); } } From e400c8ac82324fb3cb1878cfe502e6992bd4c5dd Mon Sep 17 00:00:00 2001 From: ipad54 <63200545+ipad54@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:50:43 +0300 Subject: [PATCH 2/6] Fix PHPStan --- src/player/Player.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/player/Player.php b/src/player/Player.php index 42842348472..3787ffa091f 100644 --- a/src/player/Player.php +++ b/src/player/Player.php @@ -715,7 +715,7 @@ public function resetItemCooldown(Item $item, ?int $ticks = null) : void{ $ticks = $ticks ?? $item->getCooldownTicks(); if($ticks > 0){ $this->usedItemsCooldown[$item->getStateId()] = $this->server->getTick() + $ticks; - $this->networkSession->onItemCooldownChanged($item, $ticks); + $this->getNetworkSession()->onItemCooldownChanged($item, $ticks); } } From 5d3a7a80ff1a0b9f8b4f750fdebd2733c89c3c9b Mon Sep 17 00:00:00 2001 From: ipad54 <63200545+ipad54@users.noreply.github.com> Date: Tue, 13 Aug 2024 14:18:43 +0300 Subject: [PATCH 3/6] Implemented cooldown tag system --- src/item/BaseCooldownTags.php | 40 +++++++++++++++++++++++++++++++++++ src/item/ChorusFruit.php | 4 ++++ src/item/EnderPearl.php | 4 ++++ src/item/Item.php | 14 ++++++++++++ src/player/Player.php | 12 +++++++---- 5 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 src/item/BaseCooldownTags.php diff --git a/src/item/BaseCooldownTags.php b/src/item/BaseCooldownTags.php new file mode 100644 index 00000000000..5e782946f8b --- /dev/null +++ b/src/item/BaseCooldownTags.php @@ -0,0 +1,40 @@ + ticks map */ + + /** + * @phpstan-var array + * @var int[] stateId|cooldownTag => ticks map + */ protected array $usedItemsCooldown = []; private int $lastEmoteTick = 0; @@ -697,7 +701,7 @@ public function getItemUseDuration() : int{ */ public function getItemCooldownExpiry(Item $item) : int{ $this->checkItemCooldowns(); - return $this->usedItemsCooldown[$item->getStateId()] ?? 0; + return $this->usedItemsCooldown[$item->getCooldownTag() ?? $item->getStateId()] ?? 0; } /** @@ -705,7 +709,7 @@ public function getItemCooldownExpiry(Item $item) : int{ */ public function hasItemCooldown(Item $item) : bool{ $this->checkItemCooldowns(); - return isset($this->usedItemsCooldown[$item->getStateId()]); + return isset($this->usedItemsCooldown[$item->getCooldownTag() ?? $item->getStateId()]); } /** @@ -714,7 +718,7 @@ public function hasItemCooldown(Item $item) : bool{ public function resetItemCooldown(Item $item, ?int $ticks = null) : void{ $ticks = $ticks ?? $item->getCooldownTicks(); if($ticks > 0){ - $this->usedItemsCooldown[$item->getStateId()] = $this->server->getTick() + $ticks; + $this->usedItemsCooldown[$item->getCooldownTag() ?? $item->getStateId()] = $this->server->getTick() + $ticks; $this->getNetworkSession()->onItemCooldownChanged($item, $ticks); } } From 5755207b1b35052a209c1bd922a906d7bed3de87 Mon Sep 17 00:00:00 2001 From: ipad54 Date: Tue, 20 Aug 2024 14:19:04 +0300 Subject: [PATCH 4/6] Rename BaseCooldownTags to VanillaCooldownTags --- src/item/ChorusFruit.php | 2 +- src/item/EnderPearl.php | 2 +- src/item/Item.php | 2 +- src/item/{BaseCooldownTags.php => VanillaCooldownTags.php} | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename src/item/{BaseCooldownTags.php => VanillaCooldownTags.php} (97%) diff --git a/src/item/ChorusFruit.php b/src/item/ChorusFruit.php index 2ae3db483b2..56ab49ad9bc 100644 --- a/src/item/ChorusFruit.php +++ b/src/item/ChorusFruit.php @@ -90,6 +90,6 @@ public function getCooldownTicks() : int{ } public function getCooldownTag() : ?string{ - return BaseCooldownTags::CHORUS_FRUIT; + return VanillaCooldownTags::CHORUS_FRUIT; } } diff --git a/src/item/EnderPearl.php b/src/item/EnderPearl.php index 5b22780cd36..39ec8fe632c 100644 --- a/src/item/EnderPearl.php +++ b/src/item/EnderPearl.php @@ -47,6 +47,6 @@ public function getCooldownTicks() : int{ } public function getCooldownTag() : ?string{ - return BaseCooldownTags::ENDER_PEARL; + return VanillaCooldownTags::ENDER_PEARL; } } diff --git a/src/item/Item.php b/src/item/Item.php index 387301f214a..ad2dfcb16cf 100644 --- a/src/item/Item.php +++ b/src/item/Item.php @@ -662,7 +662,7 @@ public function getCooldownTicks() : int{ * * If tag is null, item state id will be used to store cooldown. * - * @see BaseCooldownTags + * @see VanillaCooldownTags */ public function getCooldownTag() : ?string{ return null; diff --git a/src/item/BaseCooldownTags.php b/src/item/VanillaCooldownTags.php similarity index 97% rename from src/item/BaseCooldownTags.php rename to src/item/VanillaCooldownTags.php index 5e782946f8b..aa391503bc7 100644 --- a/src/item/BaseCooldownTags.php +++ b/src/item/VanillaCooldownTags.php @@ -27,7 +27,7 @@ * Tags used by items to determine their cooldown group. * @see Item::getCooldownTag() */ -final class BaseCooldownTags{ +final class VanillaCooldownTags{ private function __construct(){ //NOOP From f358cf8ae307068cb606551acf0fcd0a7538423a Mon Sep 17 00:00:00 2001 From: ipad54 <63200545+ipad54@users.noreply.github.com> Date: Sun, 1 Sep 2024 18:53:40 +0300 Subject: [PATCH 5/6] Requested changes --- src/item/ChorusFruit.php | 2 +- src/item/EnderPearl.php | 2 +- src/item/Item.php | 2 +- src/item/{VanillaCooldownTags.php => ItemCooldownTags.php} | 7 ++++++- 4 files changed, 9 insertions(+), 4 deletions(-) rename src/item/{VanillaCooldownTags.php => ItemCooldownTags.php} (82%) diff --git a/src/item/ChorusFruit.php b/src/item/ChorusFruit.php index 56ab49ad9bc..e10c519576b 100644 --- a/src/item/ChorusFruit.php +++ b/src/item/ChorusFruit.php @@ -90,6 +90,6 @@ public function getCooldownTicks() : int{ } public function getCooldownTag() : ?string{ - return VanillaCooldownTags::CHORUS_FRUIT; + return ItemCooldownTags::CHORUS_FRUIT; } } diff --git a/src/item/EnderPearl.php b/src/item/EnderPearl.php index 39ec8fe632c..7109d3ae066 100644 --- a/src/item/EnderPearl.php +++ b/src/item/EnderPearl.php @@ -47,6 +47,6 @@ public function getCooldownTicks() : int{ } public function getCooldownTag() : ?string{ - return VanillaCooldownTags::ENDER_PEARL; + return ItemCooldownTags::ENDER_PEARL; } } diff --git a/src/item/Item.php b/src/item/Item.php index ad2dfcb16cf..205f15e1306 100644 --- a/src/item/Item.php +++ b/src/item/Item.php @@ -662,7 +662,7 @@ public function getCooldownTicks() : int{ * * If tag is null, item state id will be used to store cooldown. * - * @see VanillaCooldownTags + * @see ItemCooldownTags */ public function getCooldownTag() : ?string{ return null; diff --git a/src/item/VanillaCooldownTags.php b/src/item/ItemCooldownTags.php similarity index 82% rename from src/item/VanillaCooldownTags.php rename to src/item/ItemCooldownTags.php index aa391503bc7..3a5e15ec287 100644 --- a/src/item/VanillaCooldownTags.php +++ b/src/item/ItemCooldownTags.php @@ -27,12 +27,17 @@ * Tags used by items to determine their cooldown group. * @see Item::getCooldownTag() */ -final class VanillaCooldownTags{ +final class ItemCooldownTags{ private function __construct(){ //NOOP } + /** + * These tag values are not related to Minecraft internal IDs. + * They only share a visual similarity because these are the most obvious values to use. + * Any arbitrary string can be used. + */ public const CHORUS_FRUIT = "chorus_fruit"; public const ENDER_PEARL = "ender_pearl"; public const SHIELD = "shield"; From 3f4cd4bb4fc68365a2fcf794666a6e6d9cccec1d Mon Sep 17 00:00:00 2001 From: ipad54 <63200545+ipad54@users.noreply.github.com> Date: Sun, 1 Sep 2024 19:00:56 +0300 Subject: [PATCH 6/6] Place comment in the class docblock --- src/item/ItemCooldownTags.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/item/ItemCooldownTags.php b/src/item/ItemCooldownTags.php index 3a5e15ec287..f0ef6d1699d 100644 --- a/src/item/ItemCooldownTags.php +++ b/src/item/ItemCooldownTags.php @@ -25,6 +25,11 @@ /** * Tags used by items to determine their cooldown group. + * + * These tag values are not related to Minecraft internal IDs. + * They only share a visual similarity because these are the most obvious values to use. + * Any arbitrary string can be used. + * * @see Item::getCooldownTag() */ final class ItemCooldownTags{ @@ -33,11 +38,6 @@ private function __construct(){ //NOOP } - /** - * These tag values are not related to Minecraft internal IDs. - * They only share a visual similarity because these are the most obvious values to use. - * Any arbitrary string can be used. - */ public const CHORUS_FRUIT = "chorus_fruit"; public const ENDER_PEARL = "ender_pearl"; public const SHIELD = "shield";