PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
ShapedRecipe.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 ShapedRecipe implements Recipe{
29  private $output;
30 
32  private $rows = [];
33 
35  private $ingredients = [];
36 
43  public function __construct(Item $result, array $shape = []){
44  if(count($shape) === 0){
45  throw new \InvalidArgumentException("Must provide a shape");
46  }
47  if(count($shape) > 3){
48  throw new \InvalidStateException("Crafting recipes should be 1, 2, 3 rows, not " . count($shape));
49  }
50  foreach($shape as $row){
51  if(strlen($row) === 0 or strlen($row) > 3){
52  throw new \InvalidStateException("Crafting rows should be 1, 2, 3 characters, not " . count($row));
53  }
54  $this->rows[] = $row;
55  $len = strlen($row);
56  for($i = 0; $i < $len; ++$i){
57  $this->ingredients[$row{$i}] = null;
58  }
59  }
60 
61  $this->output = clone $result;
62  }
63 
71  public function setIngredient($key, Item $item){
72  if(!isset($this->ingredients[$key])){
73  throw new \Exception("Symbol does not appear in the shape: " . $key);
74  }
75 
76  $this->ingredients[$key] = $item;
77 
78  return $this;
79  }
80 
84  public function getIngredientMap(){
85  $ingredients = [];
86  foreach($this->ingredients as $key => $ingredient){
87  if($ingredient instanceof Item){
88  $ingredients[$key] = clone $ingredient;
89  }else{
90  $ingredients[$key] = $ingredient;
91  }
92  }
93 
94  return $ingredients;
95  }
96 
100  public function getShape(){
101  return $this->rows;
102  }
103 
104  public function registerToCraftingManager(){
105  Server::getInstance()->getCraftingManager()->registerShapedRecipe($this);
106  }
107 }
static getInstance()
Definition: Server.php:1444
__construct(Item $result, array $shape=[])