mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 01:46:04 +00:00
Added InventoryAction->onPreExecute(), fixed PlayerDropItemEvent deleting items
This commit is contained in:
@ -222,6 +222,12 @@ class SimpleInventoryTransaction implements InventoryTransaction{
|
||||
return $this->matchItems($needItems, $haveItems) and count($this->actions) > 0 and count($haveItems) === 0 and count($needItems) === 0;
|
||||
}
|
||||
|
||||
protected function handleFailed(){
|
||||
foreach($this->actions as $action){
|
||||
$action->onExecuteFail($this->source);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
@ -232,7 +238,15 @@ class SimpleInventoryTransaction implements InventoryTransaction{
|
||||
|
||||
Server::getInstance()->getPluginManager()->callEvent($ev = new InventoryTransactionEvent($this));
|
||||
if($ev->isCancelled()){
|
||||
return false;
|
||||
$this->handleFailed();
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach($this->actions as $action){
|
||||
if(!$action->onPreExecute($this->source)){
|
||||
$this->handleFailed();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
foreach($this->actions as $action){
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\inventory\transaction\action;
|
||||
|
||||
use pocketmine\event\player\PlayerDropItemEvent;
|
||||
use pocketmine\Player;
|
||||
|
||||
/**
|
||||
@ -41,6 +42,15 @@ class DropItemAction extends InventoryAction{
|
||||
return $this->sourceItem->isNull();
|
||||
}
|
||||
|
||||
public function onPreExecute(Player $source) : bool{
|
||||
$source->getServer()->getPluginManager()->callEvent($ev = new PlayerDropItemEvent($source, $this->targetItem));
|
||||
if($ev->isCancelled()){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops the target item in front of the player.
|
||||
*
|
||||
|
@ -74,6 +74,18 @@ abstract class InventoryAction{
|
||||
*/
|
||||
abstract public function isValid(Player $source) : bool;
|
||||
|
||||
/**
|
||||
* Called by inventory transactions before any actions are processed. If this returns false, the transaction will
|
||||
* be cancelled.
|
||||
*
|
||||
* @param Player $source
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function onPreExecute(Player $source) : bool{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs actions needed to complete the inventory-action server-side. Returns if it was successful. Will return
|
||||
* false if plugins cancelled events. This will only be called if the transaction which it is part of is considered
|
||||
|
Reference in New Issue
Block a user