From d2fc411390573fcbe434961fb2c655e1887c20db Mon Sep 17 00:00:00 2001 From: Muqsit Date: Thu, 8 Feb 2024 21:07:43 +0000 Subject: [PATCH] aggressiveoptz:network_item: cache encoded CraftingDataPacket and CreativeContentPacket data --- plugin.yml | 2 +- .../NetworkItemOptimizationComponent.php | 19 ++++++- .../helper/LazyEncodedPacket.php | 51 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 src/muqsit/aggressiveoptz/helper/LazyEncodedPacket.php diff --git a/plugin.yml b/plugin.yml index e701ba4..863948b 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,4 +1,4 @@ name: AggressiveOptz main: muqsit\aggressiveoptz\Loader api: 5.0.0 -version: 0.0.6 \ No newline at end of file +version: 0.0.7 \ No newline at end of file diff --git a/src/muqsit/aggressiveoptz/component/defaults/NetworkItemOptimizationComponent.php b/src/muqsit/aggressiveoptz/component/defaults/NetworkItemOptimizationComponent.php index 4444069..3f3ee83 100644 --- a/src/muqsit/aggressiveoptz/component/defaults/NetworkItemOptimizationComponent.php +++ b/src/muqsit/aggressiveoptz/component/defaults/NetworkItemOptimizationComponent.php @@ -8,6 +8,7 @@ use LogicException; use muqsit\aggressiveoptz\AggressiveOptzApi; use muqsit\aggressiveoptz\component\OptimizationComponent; +use muqsit\aggressiveoptz\helper\LazyEncodedPacket; use pocketmine\event\EventPriority; use pocketmine\event\server\DataPacketSendEvent; use pocketmine\item\Armor; @@ -15,9 +16,12 @@ use pocketmine\item\Item; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\ListTag; +use pocketmine\network\mcpe\protocol\CraftingDataPacket; +use pocketmine\network\mcpe\protocol\CreativeContentPacket; use pocketmine\network\mcpe\protocol\MobArmorEquipmentPacket; use pocketmine\network\mcpe\protocol\MobEquipmentPacket; use function assert; +use function spl_object_id; class NetworkItemOptimizationComponent implements OptimizationComponent{ @@ -27,6 +31,9 @@ public static function fromConfig(array $config) : NetworkItemOptimizationCompon private ?Closure $unregister = null; + /** @var array */ + private array $encoded_packets = []; + public function __construct(){ } @@ -37,7 +44,7 @@ public function enable(AggressiveOptzApi $api) : void{ $this->unregister = $api->registerEvent(function(DataPacketSendEvent $event) : void{ $targets = $event->getTargets(); - foreach($event->getPackets() as $packet){ + foreach($event->getPackets() as $index => $packet){ if($packet instanceof MobEquipmentPacket){ foreach($targets as $target){ if($packet->actorRuntimeId === $target->getPlayer()?->getId()){ @@ -71,6 +78,16 @@ public function enable(AggressiveOptzApi $api) : void{ if($nbt !== null){ $this->cleanItemStackNbt($nbt); } + }elseif($packet instanceof CraftingDataPacket || $packet instanceof CreativeContentPacket){ + if(!isset($this->encoded_packets[$pid = $packet::NETWORK_ID][$id = spl_object_id($packet)])){ + // these packets are already cached - just not encoded. + // in case the cache was purged, their spl_object_id would be changed. + // this array structure takes care of such changes + $this->encoded_packets[$pid] = [$id => $packet]; + } + $packets = $event->getPackets(); + $packets[$index] = $this->encoded_packets[$pid][$id]; + $event->setPackets($packets); } } }, EventPriority::HIGHEST); diff --git a/src/muqsit/aggressiveoptz/helper/LazyEncodedPacket.php b/src/muqsit/aggressiveoptz/helper/LazyEncodedPacket.php new file mode 100644 index 0000000..69c7605 --- /dev/null +++ b/src/muqsit/aggressiveoptz/helper/LazyEncodedPacket.php @@ -0,0 +1,51 @@ +inner->pid(); + } + + public function getName() : string{ + return $this->inner->getName(); + } + + public function canBeSentBeforeLogin() : bool{ + return $this->inner->canBeSentBeforeLogin(); + } + + public function decode(PacketSerializer $in) : void{ + $this->inner->decode($in); + } + + public function encode(PacketSerializer $out) : void{ + if($this->encoded === null){ + $serializer = PacketSerializer::encoder($this->context); + $this->inner->encode($serializer); + $this->encoded = $serializer->getBuffer(); + } + $out->put($this->encoded); + } + + public function handle(PacketHandlerInterface $handler) : bool{ + return $this->inner->handle($handler); + } +} \ No newline at end of file