mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-10-17 19:50:18 +00:00
PacketBatch: added a getPackets() method which encapsulates some logic
This commit is contained in:
@@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||
namespace pocketmine\network\mcpe\protocol\serializer;
|
||||
|
||||
use pocketmine\network\mcpe\protocol\Packet;
|
||||
use pocketmine\network\mcpe\protocol\PacketDecodeException;
|
||||
use pocketmine\network\mcpe\protocol\PacketPool;
|
||||
use pocketmine\utils\BinaryDataException;
|
||||
|
||||
@@ -48,6 +49,19 @@ class PacketBatch{
|
||||
return $packetPool->getPacket($this->serializer->getString());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Generator|Packet[]
|
||||
* @phpstan-return \Generator<int, Packet, void, void>
|
||||
*/
|
||||
public function getPackets(PacketPool $packetPool, int $max) : \Generator{
|
||||
for($c = 0; $c < $max and !$this->serializer->feof(); ++$c){
|
||||
yield $c => $packetPool->getPacket($this->serializer->getString());
|
||||
}
|
||||
if(!$this->serializer->feof()){
|
||||
throw new PacketDecodeException("Reached limit of $max packets in a single batch");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a packet batch from the given list of packets.
|
||||
*
|
||||
|
Reference in New Issue
Block a user