mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-08 02:42:58 +00:00
Caching creative inventory entries (#5703)
Due to the high cost of Item::serializeCompoundTag(), it's very costly to rebuild this every time we need it. This is sent during the pre-spawn step, where we need to minimize costs as much as possible.
This commit is contained in:
69
src/network/mcpe/cache/CreativeInventoryCache.php
vendored
Normal file
69
src/network/mcpe/cache/CreativeInventoryCache.php
vendored
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\network\mcpe\cache;
|
||||
|
||||
use pocketmine\inventory\CreativeInventory;
|
||||
use pocketmine\network\mcpe\convert\TypeConverter;
|
||||
use pocketmine\network\mcpe\protocol\CreativeContentPacket;
|
||||
use pocketmine\network\mcpe\protocol\types\inventory\CreativeContentEntry;
|
||||
use pocketmine\utils\SingletonTrait;
|
||||
use function spl_object_id;
|
||||
|
||||
final class CreativeInventoryCache{
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var CreativeContentPacket[]
|
||||
* @phpstan-var array<int, CreativeContentPacket>
|
||||
*/
|
||||
private array $caches = [];
|
||||
|
||||
public function getCache(CreativeInventory $inventory) : CreativeContentPacket{
|
||||
$id = spl_object_id($inventory);
|
||||
if(!isset($this->caches[$id])){
|
||||
$inventory->getDestructorCallbacks()->add(function() use ($id) : void{
|
||||
unset($this->caches[$id]);
|
||||
});
|
||||
$inventory->getContentChangedCallbacks()->add(function() use ($id) : void{
|
||||
unset($this->caches[$id]);
|
||||
});
|
||||
$this->caches[$id] = $this->buildCreativeInventoryCache($inventory);
|
||||
}
|
||||
return $this->caches[$id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebuild the cache for the given inventory.
|
||||
*/
|
||||
private function buildCreativeInventoryCache(CreativeInventory $inventory) : CreativeContentPacket{
|
||||
$entries = [];
|
||||
$typeConverter = TypeConverter::getInstance();
|
||||
//creative inventory may have holes if items were unregistered - ensure network IDs used are always consistent
|
||||
foreach($inventory->getAll() as $k => $item){
|
||||
$entries[] = new CreativeContentEntry($k, $typeConverter->coreItemStackToNet($item));
|
||||
}
|
||||
|
||||
return CreativeContentPacket::create($entries);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user