mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-27 05:40:01 +00:00
InventoryAction: replace isValid() with validate() which throws TransactionValidationException
This commit is contained in:
parent
cf5e31c619
commit
1d18662d9b
@ -142,8 +142,10 @@ class InventoryTransaction{
|
|||||||
$needItems[] = $action->getTargetItem();
|
$needItems[] = $action->getTargetItem();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$action->isValid($this->source)){
|
try{
|
||||||
throw new TransactionValidationException("Action " . get_class($action) . " is not valid in the current transaction");
|
$action->validate($this->source);
|
||||||
|
}catch(TransactionValidationException $e){
|
||||||
|
throw new TransactionValidationException(get_class($action) . ": " . $e->getMessage(), 0, $e);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$action->getSourceItem()->isNull()){
|
if(!$action->getSourceItem()->isNull()){
|
||||||
|
@ -24,6 +24,7 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\inventory\transaction\action;
|
namespace pocketmine\inventory\transaction\action;
|
||||||
|
|
||||||
use pocketmine\inventory\CreativeInventory;
|
use pocketmine\inventory\CreativeInventory;
|
||||||
|
use pocketmine\inventory\transaction\TransactionValidationException;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\item\ItemFactory;
|
use pocketmine\item\ItemFactory;
|
||||||
use pocketmine\player\Player;
|
use pocketmine\player\Player;
|
||||||
@ -38,8 +39,13 @@ class CreateItemAction extends InventoryAction{
|
|||||||
parent::__construct($sourceItem, ItemFactory::air());
|
parent::__construct($sourceItem, ItemFactory::air());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isValid(Player $source) : bool{
|
public function validate(Player $source) : void{
|
||||||
return !$source->hasFiniteResources() and CreativeInventory::getInstance()->contains($this->sourceItem);
|
if($source->hasFiniteResources()){
|
||||||
|
throw new TransactionValidationException("Player has finite resources, cannot create items");
|
||||||
|
}
|
||||||
|
if(!CreativeInventory::getInstance()->contains($this->sourceItem)){
|
||||||
|
throw new TransactionValidationException("Creative inventory does not contain requested item");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute(Player $source) : void{
|
public function execute(Player $source) : void{
|
||||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\inventory\transaction\action;
|
namespace pocketmine\inventory\transaction\action;
|
||||||
|
|
||||||
|
use pocketmine\inventory\transaction\TransactionValidationException;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\item\ItemFactory;
|
use pocketmine\item\ItemFactory;
|
||||||
use pocketmine\player\Player;
|
use pocketmine\player\Player;
|
||||||
@ -37,8 +38,10 @@ class DestroyItemAction extends InventoryAction{
|
|||||||
parent::__construct(ItemFactory::air(), $targetItem);
|
parent::__construct(ItemFactory::air(), $targetItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isValid(Player $source) : bool{
|
public function validate(Player $source) : void{
|
||||||
return !$source->hasFiniteResources();
|
if($source->hasFiniteResources()){
|
||||||
|
throw new TransactionValidationException("Player has finite resources, cannot destroy items");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute(Player $source) : void{
|
public function execute(Player $source) : void{
|
||||||
|
@ -24,6 +24,7 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\inventory\transaction\action;
|
namespace pocketmine\inventory\transaction\action;
|
||||||
|
|
||||||
use pocketmine\event\player\PlayerDropItemEvent;
|
use pocketmine\event\player\PlayerDropItemEvent;
|
||||||
|
use pocketmine\inventory\transaction\TransactionValidationException;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\item\ItemFactory;
|
use pocketmine\item\ItemFactory;
|
||||||
use pocketmine\player\Player;
|
use pocketmine\player\Player;
|
||||||
@ -37,8 +38,10 @@ class DropItemAction extends InventoryAction{
|
|||||||
parent::__construct(ItemFactory::air(), $targetItem);
|
parent::__construct(ItemFactory::air(), $targetItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isValid(Player $source) : bool{
|
public function validate(Player $source) : void{
|
||||||
return !$this->targetItem->isNull();
|
if($this->targetItem->isNull()){
|
||||||
|
throw new TransactionValidationException("Cannot drop an empty itemstack");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onPreExecute(Player $source) : bool{
|
public function onPreExecute(Player $source) : bool{
|
||||||
|
@ -24,6 +24,7 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\inventory\transaction\action;
|
namespace pocketmine\inventory\transaction\action;
|
||||||
|
|
||||||
use pocketmine\inventory\transaction\InventoryTransaction;
|
use pocketmine\inventory\transaction\InventoryTransaction;
|
||||||
|
use pocketmine\inventory\transaction\TransactionValidationException;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\player\Player;
|
use pocketmine\player\Player;
|
||||||
|
|
||||||
@ -57,8 +58,10 @@ abstract class InventoryAction{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether this action is currently valid. This should perform any necessary sanity checks.
|
* Returns whether this action is currently valid. This should perform any necessary sanity checks.
|
||||||
|
*
|
||||||
|
* @throws TransactionValidationException
|
||||||
*/
|
*/
|
||||||
abstract public function isValid(Player $source) : bool;
|
abstract public function validate(Player $source) : void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the action is added to the specified InventoryTransaction.
|
* Called when the action is added to the specified InventoryTransaction.
|
||||||
|
@ -25,6 +25,7 @@ namespace pocketmine\inventory\transaction\action;
|
|||||||
|
|
||||||
use pocketmine\inventory\Inventory;
|
use pocketmine\inventory\Inventory;
|
||||||
use pocketmine\inventory\transaction\InventoryTransaction;
|
use pocketmine\inventory\transaction\InventoryTransaction;
|
||||||
|
use pocketmine\inventory\transaction\TransactionValidationException;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\player\Player;
|
use pocketmine\player\Player;
|
||||||
|
|
||||||
@ -60,12 +61,16 @@ class SlotChangeAction extends InventoryAction{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the item in the inventory at the specified slot is the same as this action's source item.
|
* Checks if the item in the inventory at the specified slot is the same as this action's source item.
|
||||||
|
*
|
||||||
|
* @throws TransactionValidationException
|
||||||
*/
|
*/
|
||||||
public function isValid(Player $source) : bool{
|
public function validate(Player $source) : void{
|
||||||
return (
|
if(!$this->inventory->slotExists($this->inventorySlot)){
|
||||||
$this->inventory->slotExists($this->inventorySlot) and
|
throw new TransactionValidationException("Slot does not exist");
|
||||||
$this->inventory->getItem($this->inventorySlot)->equalsExact($this->sourceItem)
|
}
|
||||||
);
|
if(!$this->inventory->getItem($this->inventorySlot)->equalsExact($this->sourceItem)){
|
||||||
|
throw new TransactionValidationException("Slot does not contain expected original item");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user