InventoryTransaction::execute() now throws exceptions instead of returning true/false

This commit is contained in:
Dylan K. Taylor 2020-07-01 14:08:28 +01:00
parent 30591d047c
commit cf5e31c619
5 changed files with 83 additions and 26 deletions

View File

@ -269,12 +269,6 @@ class InventoryTransaction{
}
}
protected function sendInventories() : void{
foreach($this->inventories as $inventory){
$this->source->getNetworkSession()->getInvManager()->syncContents($inventory);
}
}
protected function callExecuteEvent() : bool{
$ev = new InventoryTransactionEvent($this);
$ev->call();
@ -284,32 +278,24 @@ class InventoryTransaction{
/**
* Executes the group of actions, returning whether the transaction executed successfully or not.
*
* @throws TransactionValidationException
* @throws TransactionException
*/
public function execute() : bool{
public function execute() : void{
if($this->hasExecuted()){
$this->sendInventories();
return false;
throw new TransactionValidationException("Transaction has already been executed");
}
$this->shuffleActions();
try{
$this->validate();
}catch(TransactionValidationException $e){
$this->sendInventories();
throw $e;
}
if(!$this->callExecuteEvent()){
$this->sendInventories();
return false;
throw new TransactionCancelledException("Transaction event cancelled");
}
foreach($this->actions as $action){
if(!$action->onPreExecute($this->source)){
$this->sendInventories();
return false;
throw new TransactionCancelledException("One of the actions in this transaction was cancelled");
}
}
@ -318,8 +304,6 @@ class InventoryTransaction{
}
$this->hasExecuted = true;
return true;
}
public function hasExecuted() : bool{

View File

@ -0,0 +1,31 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\inventory\transaction;
/**
* Thrown when an inventory transaction is valid, but can't proceed due to other factors (e.g. a plugin intervened).
*/
final class TransactionCancelledException extends TransactionException{
}

View File

@ -0,0 +1,31 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\inventory\transaction;
/**
* Thrown when a transaction fails to execute for any reason.
*/
abstract class TransactionException extends \RuntimeException{
}

View File

@ -23,6 +23,9 @@ declare(strict_types=1);
namespace pocketmine\inventory\transaction;
class TransactionValidationException extends \RuntimeException{
/**
* Thrown when a transaction cannot proceed due to preconditions not being met (e.g. transaction doesn't balance).
*/
class TransactionValidationException extends TransactionException{
}

View File

@ -33,7 +33,7 @@ use pocketmine\event\player\PlayerEditBookEvent;
use pocketmine\inventory\transaction\action\InventoryAction;
use pocketmine\inventory\transaction\CraftingTransaction;
use pocketmine\inventory\transaction\InventoryTransaction;
use pocketmine\inventory\transaction\TransactionValidationException;
use pocketmine\inventory\transaction\TransactionException;
use pocketmine\item\VanillaItems;
use pocketmine\item\WritableBook;
use pocketmine\item\WrittenBook;
@ -247,9 +247,13 @@ class InGamePacketHandler extends PacketHandler{
try{
$this->session->getInvManager()->onTransactionStart($this->craftingTransaction);
$this->craftingTransaction->execute();
}catch(TransactionValidationException $e){
}catch(TransactionException $e){
$this->session->getLogger()->debug("Failed to execute crafting transaction: " . $e->getMessage());
//TODO: only sync slots that the client tried to change
foreach($this->craftingTransaction->getInventories() as $inventory){
$this->session->getInvManager()->syncContents($inventory);
}
/*
* TODO: HACK!
* we can't resend the contents of the crafting window, so we force the client to close it instead.
@ -280,11 +284,15 @@ class InGamePacketHandler extends PacketHandler{
$this->session->getInvManager()->onTransactionStart($transaction);
try{
$transaction->execute();
}catch(TransactionValidationException $e){
}catch(TransactionException $e){
$logger = $this->session->getLogger();
$logger->debug("Failed to execute inventory transaction: " . $e->getMessage());
$logger->debug("Actions: " . json_encode($data->getActions()));
foreach($transaction->getInventories() as $inventory){
$this->session->getInvManager()->syncContents($inventory);
}
return false;
}
}