CraftingTransaction: make CraftingManager injectable

this becomes a bit easier to unit-test.
This commit is contained in:
Dylan K. Taylor 2020-07-01 13:29:58 +01:00
parent 96541763f1
commit 29612cded3
2 changed files with 11 additions and 2 deletions

View File

@ -23,9 +23,11 @@ declare(strict_types=1);
namespace pocketmine\inventory\transaction;
use pocketmine\crafting\CraftingManager;
use pocketmine\crafting\CraftingRecipe;
use pocketmine\event\inventory\CraftItemEvent;
use pocketmine\item\Item;
use pocketmine\player\Player;
use function array_pop;
use function count;
use function intdiv;
@ -55,6 +57,13 @@ class CraftingTransaction extends InventoryTransaction{
protected $inputs = [];
/** @var Item[] */
protected $outputs = [];
/** @var CraftingManager */
private $craftingManager;
public function __construct(Player $source, CraftingManager $craftingManager, array $actions = []){
parent::__construct($source, $actions);
$this->craftingManager = $craftingManager;
}
/**
* @param Item[] $txItems
@ -126,7 +135,7 @@ class CraftingTransaction extends InventoryTransaction{
$this->matchItems($this->outputs, $this->inputs);
$failed = 0;
foreach($this->source->getServer()->getCraftingManager()->matchRecipeByOutputs($this->outputs) as $recipe){
foreach($this->craftingManager->matchRecipeByOutputs($this->outputs) as $recipe){
try{
//compute number of times recipe was crafted
$this->repetitions = $this->matchRecipeItems($this->outputs, $recipe->getResultsFor($this->source->getCraftingGrid()), false);

View File

@ -236,7 +236,7 @@ class InGamePacketHandler extends PacketHandler{
//trying to execute it
if($this->craftingTransaction === null){
$this->craftingTransaction = new CraftingTransaction($this->player, $actions);
$this->craftingTransaction = new CraftingTransaction($this->player, $this->player->getServer()->getCraftingManager(), $actions);
}else{
foreach($actions as $action){
$this->craftingTransaction->addAction($action);