Rewritten crafting, fixed #45

This commit is contained in:
Dylan K. Taylor
2017-09-24 14:14:24 +01:00
parent 043ae487de
commit 240cc3043a
14 changed files with 611 additions and 279 deletions

View File

@ -26,51 +26,55 @@ namespace pocketmine\event\inventory;
use pocketmine\event\Cancellable;
use pocketmine\event\Event;
use pocketmine\inventory\Recipe;
use pocketmine\inventory\transaction\CraftingTransaction;
use pocketmine\item\Item;
use pocketmine\Player;
class CraftItemEvent extends Event implements Cancellable{
public static $handlerList = null;
/** @var Item[] */
private $input;
/** @var Recipe */
private $recipe;
/** @var Player */
private $player;
/** @var CraftingTransaction */
private $transaction;
/**
* @param Player $player
* @param Item[] $input
* @param Recipe $recipe
* @param CraftingTransaction $transaction
*/
public function __construct(Player $player, array $input, Recipe $recipe){
$this->player = $player;
$this->input = $input;
$this->recipe = $recipe;
public function __construct(CraftingTransaction $transaction){
$this->transaction = $transaction;
}
public function getTransaction() : CraftingTransaction{
return $this->transaction;
}
/**
* @deprecated This returns a one-dimensional array of ingredients and does not account for the positioning of
* items in the crafting grid. Prefer getting the input map from the transaction instead.
*
* @return Item[]
*/
public function getInput() : array{
return array_map(function(Item $item) : Item{
return clone $item;
}, $this->input);
}, array_merge(...$this->transaction->getInputMap()));
}
/**
* @return Recipe
*/
public function getRecipe() : Recipe{
return $this->recipe;
$recipe = $this->transaction->getRecipe();
if($recipe === null){
throw new \RuntimeException("This shouldn't be called if the transaction can't be executed");
}
return $recipe;
}
/**
* @return Player
*/
public function getPlayer() : Player{
return $this->player;
return $this->transaction->getSource();
}
}