From cf5e31c61911a2a8d1d1a239df56aab00d5de17e Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 1 Jul 2020 14:08:28 +0100 Subject: [PATCH] InventoryTransaction::execute() now throws exceptions instead of returning true/false --- .../transaction/InventoryTransaction.php | 28 ++++------------- .../TransactionCancelledException.php | 31 +++++++++++++++++++ .../transaction/TransactionException.php | 31 +++++++++++++++++++ .../TransactionValidationException.php | 5 ++- .../mcpe/handler/InGamePacketHandler.php | 14 +++++++-- 5 files changed, 83 insertions(+), 26 deletions(-) create mode 100644 src/inventory/transaction/TransactionCancelledException.php create mode 100644 src/inventory/transaction/TransactionException.php diff --git a/src/inventory/transaction/InventoryTransaction.php b/src/inventory/transaction/InventoryTransaction.php index d6680c284e..e7b4a6d3fc 100644 --- a/src/inventory/transaction/InventoryTransaction.php +++ b/src/inventory/transaction/InventoryTransaction.php @@ -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; - } + $this->validate(); 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{ diff --git a/src/inventory/transaction/TransactionCancelledException.php b/src/inventory/transaction/TransactionCancelledException.php new file mode 100644 index 0000000000..7db33ed3a4 --- /dev/null +++ b/src/inventory/transaction/TransactionCancelledException.php @@ -0,0 +1,31 @@ +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; } }