Clean up SlotChangeAction inventory handling

This commit is contained in:
Dylan K. Taylor 2017-09-21 12:44:03 +01:00
parent 6aa9b081e9
commit d89b8cf12e
3 changed files with 36 additions and 7 deletions

View File

@ -88,16 +88,25 @@ class InventoryTransaction{
/**
* @param InventoryAction $action
*/
public function addAction(InventoryAction $action){
if(isset($this->actions[spl_object_hash($action)])){
return;
public function addAction(InventoryAction $action) : void{
if(!isset($this->actions[$hash = spl_object_hash($action)])){
$this->actions[spl_object_hash($action)] = $action;
$action->onAddToTransaction($this);
}else{
throw new \InvalidArgumentException("Tried to add the same action to a transaction twice");
}
}
if($action instanceof SlotChangeAction){
$this->inventories[spl_object_hash($action->getInventory())] = $action->getInventory();
/**
* @internal This method should not be used by plugins, it's used to add tracked inventories for InventoryActions
* involving inventories.
*
* @param Inventory $inventory
*/
public function addInventory(Inventory $inventory) : void{
if(!isset($this->inventories[$hash = spl_object_hash($inventory)])){
$this->inventories[$hash] = $inventory;
}
$this->actions[spl_object_hash($action)] = $action;
}
/**

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\inventory\transaction\action;
use pocketmine\inventory\transaction\InventoryTransaction;
use pocketmine\item\Item;
use pocketmine\Player;
@ -74,6 +75,15 @@ abstract class InventoryAction{
*/
abstract public function isValid(Player $source) : bool;
/**
* Called when the action is added to the specified InventoryTransaction.
*
* @param InventoryTransaction $transaction
*/
public function onAddToTransaction(InventoryTransaction $transaction) : void{
}
/**
* Called by inventory transactions before any actions are processed. If this returns false, the transaction will
* be cancelled.

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\inventory\transaction\action;
use pocketmine\inventory\Inventory;
use pocketmine\inventory\transaction\InventoryTransaction;
use pocketmine\item\Item;
use pocketmine\Player;
@ -78,6 +79,15 @@ class SlotChangeAction extends InventoryAction{
return $check->equalsExact($this->sourceItem);
}
/**
* Adds this action's target inventory to the transaction's inventory list.
*
* @param InventoryTransaction $transaction
*/
public function onAddToTransaction(InventoryTransaction $transaction) : void{
$transaction->addInventory($this->inventory);
}
/**
* Sets the item into the target inventory.
*