Cleaned up network inventory action reading and core action creation

This commit is contained in:
Dylan K. Taylor
2017-08-28 20:04:35 +01:00
parent ece0692229
commit a3d21de559
5 changed files with 174 additions and 100 deletions

View File

@ -25,7 +25,6 @@ namespace pocketmine\inventory\transaction;
use pocketmine\event\inventory\InventoryTransactionEvent;
use pocketmine\inventory\Inventory;
use pocketmine\inventory\transaction\InventoryTransaction;
use pocketmine\inventory\transaction\action\InventoryAction;
use pocketmine\inventory\transaction\action\SlotChangeAction;
use pocketmine\item\Item;
@ -49,11 +48,15 @@ class SimpleInventoryTransaction implements InventoryTransaction{
protected $actions = [];
/**
* @param Player $source
* @param Player $source
* @param InventoryAction[] $actions
*/
public function __construct(Player $source = null){
public function __construct(Player $source = null, array $actions = []){
$this->creationTime = microtime(true);
$this->source = $source;
foreach($actions as $action){
$this->addAction($action);
}
}
/**
@ -87,7 +90,6 @@ class SimpleInventoryTransaction implements InventoryTransaction{
}
if($action instanceof SlotChangeAction){
$action->setInventoryFrom($this->source);
$this->inventories[spl_object_hash($action->getInventory())] = $action->getInventory();
}

View File

@ -32,47 +32,32 @@ use pocketmine\Player;
*/
class SlotChangeAction extends InventoryAction{
/** @var Inventory|null */
/** @var Inventory */
protected $inventory;
/** @var int */
private $inventorySlot;
/** @var int */
private $containerId;
/**
* @param Item $sourceItem
* @param Item $targetItem
* @param int $containerId
* @param int $inventorySlot
* @param Inventory $inventory
* @param int $inventorySlot
* @param Item $sourceItem
* @param Item $targetItem
*/
public function __construct(Item $sourceItem, Item $targetItem, int $containerId, int $inventorySlot){
public function __construct(Inventory $inventory, int $inventorySlot, Item $sourceItem, Item $targetItem){
parent::__construct($sourceItem, $targetItem);
$this->inventory = $inventory;
$this->inventorySlot = $inventorySlot;
$this->containerId = $containerId;
}
public function getContainerId() : int{
return $this->containerId;
}
/**
* Returns the inventory involved in this action. Will return null if the action has not yet been fully initialized.
* Returns the inventory involved in this action.
*
* @return Inventory|null
* @return Inventory
*/
public function getInventory(){
public function getInventory() : Inventory{
return $this->inventory;
}
public function setInventoryFrom(Player $player){
$inventory = $player->getWindow($this->containerId);
if($inventory === null){
throw new \InvalidStateException("Player " . $player->getName() . " has no open container with ID " . $this->containerId);
}
$this->inventory = $inventory;
}
/**
* Returns the slot in the inventory which this action modified.
* @return int