inventory = $inventory; $this->inventorySlot = $inventorySlot; } /** * Returns the inventory involved in this action. */ public function getInventory() : Inventory{ return $this->inventory; } /** * Returns the slot in the inventory which this action modified. */ public function getSlot() : int{ return $this->inventorySlot; } /** * Checks if the item in the inventory at the specified slot is the same as this action's source item. */ public function isValid(Player $source) : bool{ return ( $this->inventory->slotExists($this->inventorySlot) and $this->inventory->getItem($this->inventorySlot)->equalsExact($this->sourceItem) ); } /** * Adds this action's target inventory to the transaction's inventory list. */ public function onAddToTransaction(InventoryTransaction $transaction) : void{ $transaction->addInventory($this->inventory); } /** * Sets the item into the target inventory. */ public function execute(Player $source) : bool{ return $this->inventory->setItem($this->inventorySlot, $this->targetItem, false); } /** * Sends slot changes to other viewers of the inventory. This will not send any change back to the source Player. */ public function onExecuteSuccess(Player $source) : void{ $viewers = $this->inventory->getViewers(); unset($viewers[spl_object_hash($source)]); $this->inventory->sendSlot($this->inventorySlot, $viewers); } /** * Sends the original slot contents to the source player to revert the action. */ public function onExecuteFail(Player $source) : void{ $this->inventory->sendSlot($this->inventorySlot, $source); } }