PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
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 foreach(Utils::stringifyKeys($ingredients) as $char => $i){
101 if(!str_contains(implode($this->shape), $char)){
102 throw new \InvalidArgumentException("Symbol '$char' does not appear in the recipe shape");
103 }
104
105 $this->ingredientList[$char] = clone $i;
106 }
107
108 $this->results = Utils::cloneObjectArray($results);
109 }
110
111 public function getWidth() : int{
112 return $this->width;
113 }
114
115 public function getHeight() : int{
116 return $this->height;
117 }
118
123 public function getResults() : array{
124 return Utils::cloneObjectArray($this->results);
125 }
126
131 public function getResultsFor(CraftingGrid $grid) : array{
132 return $this->getResults();
133 }
134
139 public function getIngredientMap() : array{
140 $ingredients = [];
141
142 for($y = 0; $y < $this->height; ++$y){
143 for($x = 0; $x < $this->width; ++$x){
144 $ingredients[$y][$x] = $this->getIngredient($x, $y);
145 }
146 }
147
148 return $ingredients;
149 }
150
151 public function getIngredientList() : array{
152 $ingredients = [];
153
154 for($y = 0; $y < $this->height; ++$y){
155 for($x = 0; $x < $this->width; ++$x){
156 $ingredient = $this->getIngredient($x, $y);
157 if($ingredient !== null){
158 $ingredients[] = $ingredient;
159 }
160 }
161 }
162
163 return $ingredients;
164 }
165
166 public function getIngredient(int $x, int $y) : ?RecipeIngredient{
167 return $this->ingredientList[$this->shape[$y][$x]] ?? null;
168 }
169
175 public function getShape() : array{
176 return $this->shape;
177 }
178
179 private function matchInputMap(CraftingGrid $grid, bool $reverse) : bool{
180 for($y = 0; $y < $this->height; ++$y){
181 for($x = 0; $x < $this->width; ++$x){
182
183 $given = $grid->getIngredient($reverse ? $this->width - $x - 1 : $x, $y);
184 $required = $this->getIngredient($x, $y);
185
186 if($required === null){
187 if(!$given->isNull()){
188 return false; //hole, such as that in the center of a chest recipe, should not be filled
189 }
190 }elseif(!$required->accepts($given)){
191 return false;
192 }
193 }
194 }
195
196 return true;
197 }
198
199 public function matchesCraftingGrid(CraftingGrid $grid) : bool{
200 if($this->width !== $grid->getRecipeWidth() || $this->height !== $grid->getRecipeHeight()){
201 return false;
202 }
203
204 return $this->matchInputMap($grid, false) || $this->matchInputMap($grid, true);
205 }
206}
__construct(array $shape, array $ingredients, array $results)