Added InventoryAction->onPreExecute(), fixed PlayerDropItemEvent deleting items

This commit is contained in:
Dylan K. Taylor 2017-09-20 12:19:42 +01:00
parent 0262465a26
commit 55720d9f0a
4 changed files with 37 additions and 7 deletions

View File

@ -49,7 +49,6 @@ use pocketmine\event\player\PlayerBedLeaveEvent;
use pocketmine\event\player\PlayerChatEvent;
use pocketmine\event\player\PlayerCommandPreprocessEvent;
use pocketmine\event\player\PlayerDeathEvent;
use pocketmine\event\player\PlayerDropItemEvent;
use pocketmine\event\player\PlayerExhaustEvent;
use pocketmine\event\player\PlayerGameModeChangeEvent;
use pocketmine\event\player\PlayerInteractEvent;
@ -2723,11 +2722,6 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
return true;
}
$this->server->getPluginManager()->callEvent($ev = new PlayerDropItemEvent($this, $item));
if($ev->isCancelled()){
return false;
}
$motion = $this->getDirectionVector()->multiply(0.4);
$this->level->dropItem($this->add(0, 1.3, 0), $item, $motion, 40);

View File

@ -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){

View File

@ -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.
*

View File

@ -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