Inventory: Use exceptions to report back why a transaction failed

Returning false all the time could mean any one of a range of things. Throwing exceptions is better in that it allows us to catch them and see what actually broke.
This commit is contained in:
Dylan K. Taylor
2018-04-04 12:11:24 +01:00
parent 5b7b2dd0e2
commit ef2dd1de92
4 changed files with 80 additions and 44 deletions

View File

@ -74,6 +74,7 @@ use pocketmine\inventory\PlayerCursorInventory;
use pocketmine\inventory\transaction\action\InventoryAction;
use pocketmine\inventory\transaction\CraftingTransaction;
use pocketmine\inventory\transaction\InventoryTransaction;
use pocketmine\inventory\transaction\TransactionValidationException;
use pocketmine\item\Consumable;
use pocketmine\item\Item;
use pocketmine\item\WritableBook;
@ -2302,13 +2303,16 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
//we get the actions for this in several packets, so we need to wait until we have all the pieces before
//trying to execute it
$result = $this->craftingTransaction->execute();
if(!$result){
$this->server->getLogger()->debug("Failed to execute crafting transaction from " . $this->getName());
$ret = true;
try{
$this->craftingTransaction->execute();
}catch(TransactionValidationException $e){
$this->server->getLogger()->debug("Failed to execute crafting transaction for " . $this->getName() . ": " . $e->getMessage());
$ret = false;
}
$this->craftingTransaction = null;
return $result;
return $ret;
}
return true;
@ -2322,10 +2326,13 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$this->setUsingItem(false);
$transaction = new InventoryTransaction($this, $actions);
if(!$transaction->execute()){
$this->server->getLogger()->debug("Failed to execute inventory transaction from " . $this->getName() . " with actions: " . json_encode($packet->actions));
try{
$transaction->execute();
}catch(TransactionValidationException $e){
$this->server->getLogger()->debug("Failed to execute inventory transaction from " . $this->getName() . ": " . $e->getMessage());
$this->server->getLogger()->debug("Actions: " . json_encode($packet->actions));
return false; //oops!
return false;
}
//TODO: fix achievement for getting iron from furnace