Added TransactionBuilderInventory for server-side inventory transaction generation

This commit is contained in:
Dylan K. Taylor 2021-09-09 17:10:04 +01:00
parent 4a787769bf
commit 5d4f14b388
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -0,0 +1,106 @@
<?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\inventory\transaction;
use pocketmine\inventory\BaseInventory;
use pocketmine\inventory\Inventory;
use pocketmine\inventory\transaction\action\SlotChangeAction;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
/**
* This class facilitates generating SlotChangeActions to build an inventory transaction.
* It wraps around the inventory you want to modify under transaction, and generates a diff of changes.
* This allows you to use the normal Inventory API methods like addItem() and so on to build a transaction, without
* modifying the original inventory.
*/
final class TransactionBuilderInventory extends BaseInventory{
/**
* @var \SplFixedArray|(Item|null)[]
* @phpstan-var \SplFixedArray<Item|null>
*/
private \SplFixedArray $changedSlots;
public function __construct(
private Inventory $actualInventory
){
parent::__construct();
$this->changedSlots = new \SplFixedArray($this->actualInventory->getSize());
}
protected function internalSetContents(array $items) : void{
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
if(!isset($items[$i])){
$this->clear($i);
}else{
$this->setItem($i, $items[$i]);
}
}
}
protected function internalSetItem(int $index, Item $item) : void{
if(!$item->equalsExact($this->actualInventory->getItem($index))){
$this->changedSlots[$index] = $item->isNull() ? ItemFactory::air() : clone $item;
}
}
public function getSize() : int{
return $this->actualInventory->getSize();
}
public function getItem(int $index) : Item{
return $this->changedSlots[$index] !== null ? clone $this->changedSlots[$index] : $this->actualInventory->getItem($index);
}
public function getContents(bool $includeEmpty = false) : array{
$contents = $this->actualInventory->getContents($includeEmpty);
foreach($this->changedSlots as $index => $item){
if($item !== null){
if($includeEmpty || !$item->isNull()){
$contents[$index] = clone $item;
}else{
unset($contents[$index]);
}
}
}
return $contents;
}
/**
* @return SlotChangeAction[]
*/
public function generateActions() : array{
$result = [];
foreach($this->changedSlots as $index => $newItem){
if($newItem !== null){
$oldItem = $this->actualInventory->getItem($index);
if(!$newItem->equalsExact($oldItem)){
$result[] = new SlotChangeAction($this->actualInventory, $index, $oldItem, $newItem);
}
}
}
return $result;
}
}