mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 17:59:48 +00:00
Cleaned up network inventory action reading and core action creation
This commit is contained in:
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user