PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
CraftingTransactionGroup.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\inventory;
23 
27 
30  protected $input = [];
32  protected $output = [];
33 
35  protected $recipe = null;
36 
37  public function __construct(SimpleTransactionGroup $group){
38  parent::__construct();
39  $this->transactions = $group->getTransactions();
40  $this->inventories = $group->getInventories();
41  $this->source = $group->getSource();
42 
43  $this->matchItems($this->output, $this->input);
44  }
45 
46  public function addTransaction(Transaction $transaction){
47  parent::addTransaction($transaction);
48  $this->input = [];
49  $this->output = [];
50  $this->matchItems($this->output, $this->input);
51  }
52 
58  public function getRecipe(){
59  return $this->input;
60  }
61 
65  public function getResult(){
66  reset($this->output);
67 
68  return current($this->output);
69  }
70 
71  public function canExecute(){
72  if(count($this->output) !== 1 or count($this->input) === 0){
73  return false;
74  }
75 
76  return $this->getMatchingRecipe() instanceof Recipe;
77  }
78 
82  public function getMatchingRecipe(){
83  if($this->recipe === null){
84  $this->recipe = Server::getInstance()->getCraftingManager()->matchTransaction($this);
85  }
86 
87  return $this->recipe;
88  }
89 
90  public function execute(){
91  if($this->hasExecuted() or !$this->canExecute()){
92  return false;
93  }
94 
95  Server::getInstance()->getPluginManager()->callEvent($ev = new CraftItemEvent($this, $this->getMatchingRecipe()));
96  if($ev->isCancelled()){
97  foreach($this->inventories as $inventory){
98  $inventory->sendContents($inventory->getViewers());
99  }
100 
101  return false;
102  }
103 
104  foreach($this->transactions as $transaction){
105  $transaction->getInventory()->setItem($transaction->getSlot(), $transaction->getTargetItem(), $this->getSource());
106  }
107  $this->hasExecuted = true;
108 
109  return true;
110  }
111 }
matchItems(array &$needItems, array &$haveItems)
static getInstance()
Definition: Server.php:1444