mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-08-31 23:33:06 +00:00
BaseInventory: improved performance of getContents()
this old code is extremely inefficient. This showed up distinctly in my crafting bruteforce benchmarks, where I discovered that getContents() accounted for the vast majority of the time taken to match shaped recipes.
This commit is contained in:
parent
c7e803372c
commit
7e20385bdb
@ -99,10 +99,13 @@ abstract class BaseInventory implements Inventory{
|
|||||||
*/
|
*/
|
||||||
public function getContents(bool $includeEmpty = false) : array{
|
public function getContents(bool $includeEmpty = false) : array{
|
||||||
$contents = [];
|
$contents = [];
|
||||||
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
$air = null;
|
||||||
$item = $this->getItem($i);
|
|
||||||
if($includeEmpty or !$item->isNull()){
|
foreach($this->slots as $i => $slot){
|
||||||
$contents[$i] = $item;
|
if($slot !== null){
|
||||||
|
$contents[$i] = clone $slot;
|
||||||
|
}elseif($includeEmpty){
|
||||||
|
$contents[$i] = $air ?? ($air = ItemFactory::get(Item::AIR, 0, 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user