PocketMine-MP 5.25.3 git-afc4a3c7f18d42b41cbfde84ab6a2e4dd7c03045
All Classes Namespaces Functions Variables Enumerations Enumerator Pages
src/crafting/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
22declare(strict_types=1);
23
24namespace pocketmine\crafting;
25
28use function array_values;
29use function count;
30use function implode;
31use function str_contains;
32use function strlen;
33
39 private array $shape = [];
44 private array $ingredientList = [];
49 private array $results = [];
50
51 private int $height;
52 private int $width;
53
73 public function __construct(array $shape, array $ingredients, array $results){
74 $this->height = count($shape);
75 if($this->height > 3 || $this->height <= 0){
76 throw new \InvalidArgumentException("Shaped recipes may only have 1, 2 or 3 rows, not $this->height");
77 }
78
79 $shape = array_values($shape);
80
81 $this->width = strlen($shape[0]);
82 if($this->width > 3 || $this->width <= 0){
83 throw new \InvalidArgumentException("Shaped recipes may only have 1, 2 or 3 columns, not $this->width");
84 }
85
86 foreach($shape as $y => $row){
87 if(strlen($row) !== $this->width){
88 throw new \InvalidArgumentException("Shaped recipe rows must all have the same length (expected $this->width, got " . strlen($row) . ")");
89 }
90
91 for($x = 0; $x < $this->width; ++$x){
92 if($row[$x] !== ' ' && !isset($ingredients[$row[$x]])){
93 throw new \InvalidArgumentException("No item specified for symbol '" . $row[$x] . "'");
94 }
95 }
96 }
97
98 $this->shape = $shape;
99
100 Utils::validateArrayValueType($ingredients, function(RecipeIngredient $_) : void{});
101 foreach(Utils::stringifyKeys($ingredients) as $char => $i){
102 if(!str_contains(implode($this->shape), $char)){
103 throw new \InvalidArgumentException("Symbol '$char' does not appear in the recipe shape");
104 }
105
106 $this->ingredientList[$char] = clone $i;
107 }
108
109 Utils::validateArrayValueType($results, function(Item $_) : void{});
110 $this->results = Utils::cloneObjectArray($results);
111 }
112
113 public function getWidth() : int{
114 return $this->width;
115 }
116
117 public function getHeight() : int{
118 return $this->height;
119 }
120
125 public function getResults() : array{
126 return Utils::cloneObjectArray($this->results);
127 }
128
133 public function getResultsFor(CraftingGrid $grid) : array{
134 return $this->getResults();
135 }
136
141 public function getIngredientMap() : array{
142 $ingredients = [];
143
144 for($y = 0; $y < $this->height; ++$y){
145 for($x = 0; $x < $this->width; ++$x){
146 $ingredients[$y][$x] = $this->getIngredient($x, $y);
147 }
148 }
149
150 return $ingredients;
151 }
152
153 public function getIngredientList() : array{
154 $ingredients = [];
155
156 for($y = 0; $y < $this->height; ++$y){
157 for($x = 0; $x < $this->width; ++$x){
158 $ingredient = $this->getIngredient($x, $y);
159 if($ingredient !== null){
160 $ingredients[] = $ingredient;
161 }
162 }
163 }
164
165 return $ingredients;
166 }
167
168 public function getIngredient(int $x, int $y) : ?RecipeIngredient{
169 return $this->ingredientList[$this->shape[$y][$x]] ?? null;
170 }
171
177 public function getShape() : array{
178 return $this->shape;
179 }
180
181 private function matchInputMap(CraftingGrid $grid, bool $reverse) : bool{
182 for($y = 0; $y < $this->height; ++$y){
183 for($x = 0; $x < $this->width; ++$x){
184
185 $given = $grid->getIngredient($reverse ? $this->width - $x - 1 : $x, $y);
186 $required = $this->getIngredient($x, $y);
187
188 if($required === null){
189 if(!$given->isNull()){
190 return false; //hole, such as that in the center of a chest recipe, should not be filled
191 }
192 }elseif(!$required->accepts($given)){
193 return false;
194 }
195 }
196 }
197
198 return true;
199 }
200
201 public function matchesCraftingGrid(CraftingGrid $grid) : bool{
202 if($this->width !== $grid->getRecipeWidth() || $this->height !== $grid->getRecipeHeight()){
203 return false;
204 }
205
206 return $this->matchInputMap($grid, false) || $this->matchInputMap($grid, true);
207 }
208}
__construct(array $shape, array $ingredients, array $results)