PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
ShapelessRecipe.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 
26 
27 class ShapelessRecipe implements Recipe{
29  private $output;
30 
32  private $ingredients = [];
33 
34  public function __construct(Item $result){
35  $this->output = clone $result;
36  }
37 
38  public function getResult(){
39  return clone $this->output;
40  }
41 
49  public function addIngredient(Item $item){
50  if(count($this->ingredients) >= 9){
51  throw new \InvalidArgumentException("Shapeless recipes cannot have more than 9 ingredients");
52  }
53 
54  $it = clone $item;
55  $it->setCount(1);
56 
57  while($item->getCount() > 0){
58  $this->ingredients[] = clone $it;
59  $item->setCount($item->getCount() - 1);
60  }
61 
62  return $this;
63  }
64 
70  public function removeIngredient(Item $item){
71  foreach($this->ingredients as $index => $ingredient){
72  if($item->getCount() <= 0){
73  break;
74  }
75  if($ingredient->equals($item, $item->getDamage() === null ? false : true)){
76  unset($this->ingredients[$index]);
77  $item->setCount($item->getCount() - 1);
78  }
79  }
80 
81  return $this;
82  }
83 
87  public function getIngredientList(){
88  $ingredients = [];
89  foreach($this->ingredients as $ingredient){
90  $ingredients[] = clone $ingredient;
91  }
92 
93  return $ingredients;
94  }
95 
99  public function getIngredientCount(){
100  $count = 0;
101  foreach($this->ingredients as $ingredient){
102  $count += $ingredient->getCount();
103  }
104 
105  return $count;
106  }
107 
108  public function registerToCraftingManager(){
109  Server::getInstance()->getCraftingManager()->registerShapelessRecipe($this);
110  }
111 }
static getInstance()
Definition: Server.php:1444