Skip to content

Commit

Permalink
aggressiveoptz:network_item: cache encoded CraftingDataPacket and Cre…
Browse files Browse the repository at this point in the history
…ativeContentPacket data
  • Loading branch information
Muqsit committed Feb 8, 2024
1 parent 7062f33 commit d2fc411
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: AggressiveOptz
main: muqsit\aggressiveoptz\Loader
api: 5.0.0
version: 0.0.6
version: 0.0.7
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
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;
use pocketmine\item\Banner;
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{

Expand All @@ -27,6 +31,9 @@ public static function fromConfig(array $config) : NetworkItemOptimizationCompon

private ?Closure $unregister = null;

/** @var array<int, array{int: LazyEncodedPacket}> */
private array $encoded_packets = [];

public function __construct(){
}

Expand All @@ -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()){
Expand Down Expand Up @@ -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);
Expand Down
51 changes: 51 additions & 0 deletions src/muqsit/aggressiveoptz/helper/LazyEncodedPacket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace muqsit\aggressiveoptz\helper;

use pocketmine\network\mcpe\protocol\ClientboundPacket;
use pocketmine\network\mcpe\protocol\PacketHandlerInterface;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializerContext;

final class LazyEncodedPacket implements ClientboundPacket{

public const NETWORK_ID = -1;

private ?string $encoded = null;

public function __construct(
readonly private PacketSerializerContext $context,
readonly private ClientboundPacket $inner
){}

public function pid() : int{
return $this->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);
}
}

0 comments on commit d2fc411

Please sign in to comment.