mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-07 02:08:21 +00:00
InventoryTransaction::execute() now throws exceptions instead of returning true/false
This commit is contained in:
@ -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{
|
||||
|
31
src/inventory/transaction/TransactionCancelledException.php
Normal file
31
src/inventory/transaction/TransactionCancelledException.php
Normal 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{
|
||||
|
||||
}
|
31
src/inventory/transaction/TransactionException.php
Normal file
31
src/inventory/transaction/TransactionException.php
Normal 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{
|
||||
|
||||
}
|
@ -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{
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user