mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-27 13:49:55 +00:00
InventoryTransaction::execute() now throws exceptions instead of returning true/false
This commit is contained in:
parent
30591d047c
commit
cf5e31c619
@ -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{
|
protected function callExecuteEvent() : bool{
|
||||||
$ev = new InventoryTransactionEvent($this);
|
$ev = new InventoryTransactionEvent($this);
|
||||||
$ev->call();
|
$ev->call();
|
||||||
@ -284,32 +278,24 @@ class InventoryTransaction{
|
|||||||
/**
|
/**
|
||||||
* Executes the group of actions, returning whether the transaction executed successfully or not.
|
* 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()){
|
if($this->hasExecuted()){
|
||||||
$this->sendInventories();
|
throw new TransactionValidationException("Transaction has already been executed");
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->shuffleActions();
|
$this->shuffleActions();
|
||||||
|
|
||||||
try{
|
|
||||||
$this->validate();
|
$this->validate();
|
||||||
}catch(TransactionValidationException $e){
|
|
||||||
$this->sendInventories();
|
|
||||||
throw $e;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!$this->callExecuteEvent()){
|
if(!$this->callExecuteEvent()){
|
||||||
$this->sendInventories();
|
throw new TransactionCancelledException("Transaction event cancelled");
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($this->actions as $action){
|
foreach($this->actions as $action){
|
||||||
if(!$action->onPreExecute($this->source)){
|
if(!$action->onPreExecute($this->source)){
|
||||||
$this->sendInventories();
|
throw new TransactionCancelledException("One of the actions in this transaction was cancelled");
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -318,8 +304,6 @@ class InventoryTransaction{
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->hasExecuted = true;
|
$this->hasExecuted = true;
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasExecuted() : bool{
|
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;
|
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{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ use pocketmine\event\player\PlayerEditBookEvent;
|
|||||||
use pocketmine\inventory\transaction\action\InventoryAction;
|
use pocketmine\inventory\transaction\action\InventoryAction;
|
||||||
use pocketmine\inventory\transaction\CraftingTransaction;
|
use pocketmine\inventory\transaction\CraftingTransaction;
|
||||||
use pocketmine\inventory\transaction\InventoryTransaction;
|
use pocketmine\inventory\transaction\InventoryTransaction;
|
||||||
use pocketmine\inventory\transaction\TransactionValidationException;
|
use pocketmine\inventory\transaction\TransactionException;
|
||||||
use pocketmine\item\VanillaItems;
|
use pocketmine\item\VanillaItems;
|
||||||
use pocketmine\item\WritableBook;
|
use pocketmine\item\WritableBook;
|
||||||
use pocketmine\item\WrittenBook;
|
use pocketmine\item\WrittenBook;
|
||||||
@ -247,9 +247,13 @@ class InGamePacketHandler extends PacketHandler{
|
|||||||
try{
|
try{
|
||||||
$this->session->getInvManager()->onTransactionStart($this->craftingTransaction);
|
$this->session->getInvManager()->onTransactionStart($this->craftingTransaction);
|
||||||
$this->craftingTransaction->execute();
|
$this->craftingTransaction->execute();
|
||||||
}catch(TransactionValidationException $e){
|
}catch(TransactionException $e){
|
||||||
$this->session->getLogger()->debug("Failed to execute crafting transaction: " . $e->getMessage());
|
$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!
|
* TODO: HACK!
|
||||||
* we can't resend the contents of the crafting window, so we force the client to close it instead.
|
* 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);
|
$this->session->getInvManager()->onTransactionStart($transaction);
|
||||||
try{
|
try{
|
||||||
$transaction->execute();
|
$transaction->execute();
|
||||||
}catch(TransactionValidationException $e){
|
}catch(TransactionException $e){
|
||||||
$logger = $this->session->getLogger();
|
$logger = $this->session->getLogger();
|
||||||
$logger->debug("Failed to execute inventory transaction: " . $e->getMessage());
|
$logger->debug("Failed to execute inventory transaction: " . $e->getMessage());
|
||||||
$logger->debug("Actions: " . json_encode($data->getActions()));
|
$logger->debug("Actions: " . json_encode($data->getActions()));
|
||||||
|
|
||||||
|
foreach($transaction->getInventories() as $inventory){
|
||||||
|
$this->session->getInvManager()->syncContents($inventory);
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user