PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
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
36 private array $shape = [];
38 private array $ingredientList = [];
40 private array $results = [];
41
42 private int $height;
43 private int $width;
44
60 public function __construct(array $shape, array $ingredients, array $results){
61 $this->height = count($shape);
62 if($this->height > 3 || $this->height <= 0){
63 throw new \InvalidArgumentException("Shaped recipes may only have 1, 2 or 3 rows, not $this->height");
64 }
65
66 $shape = array_values($shape);
67
68 $this->width = strlen($shape[0]);
69 if($this->width > 3 || $this->width <= 0){
70 throw new \InvalidArgumentException("Shaped recipes may only have 1, 2 or 3 columns, not $this->width");
71 }
72
73 foreach($shape as $y => $row){
74 if(strlen($row) !== $this->width){
75 throw new \InvalidArgumentException("Shaped recipe rows must all have the same length (expected $this->width, got " . strlen($row) . ")");
76 }
77
78 for($x = 0; $x < $this->width; ++$x){
79 if($row[$x] !== ' ' && !isset($ingredients[$row[$x]])){
80 throw new \InvalidArgumentException("No item specified for symbol '" . $row[$x] . "'");
81 }
82 }
83 }
84
85 $this->shape = $shape;
86
87 foreach($ingredients as $char => $i){
88 if(!str_contains(implode($this->shape), $char)){
89 throw new \InvalidArgumentException("Symbol '$char' does not appear in the recipe shape");
90 }
91
92 $this->ingredientList[$char] = clone $i;
93 }
94
95 $this->results = Utils::cloneObjectArray($results);
96 }
97
98 public function getWidth() : int{
99 return $this->width;
100 }
101
102 public function getHeight() : int{
103 return $this->height;
104 }
105
109 public function getResults() : array{
110 return Utils::cloneObjectArray($this->results);
111 }
112
116 public function getResultsFor(CraftingGrid $grid) : array{
117 return $this->getResults();
118 }
119
123 public function getIngredientMap() : array{
124 $ingredients = [];
125
126 for($y = 0; $y < $this->height; ++$y){
127 for($x = 0; $x < $this->width; ++$x){
128 $ingredients[$y][$x] = $this->getIngredient($x, $y);
129 }
130 }
131
132 return $ingredients;
133 }
134
138 public function getIngredientList() : array{
139 $ingredients = [];
140
141 for($y = 0; $y < $this->height; ++$y){
142 for($x = 0; $x < $this->width; ++$x){
143 $ingredient = $this->getIngredient($x, $y);
144 if($ingredient !== null){
145 $ingredients[] = $ingredient;
146 }
147 }
148 }
149
150 return $ingredients;
151 }
152
153 public function getIngredient(int $x, int $y) : ?RecipeIngredient{
154 return $this->ingredientList[$this->shape[$y][$x]] ?? null;
155 }
156
161 public function getShape() : array{
162 return $this->shape;
163 }
164
165 private function matchInputMap(CraftingGrid $grid, bool $reverse) : bool{
166 for($y = 0; $y < $this->height; ++$y){
167 for($x = 0; $x < $this->width; ++$x){
168
169 $given = $grid->getIngredient($reverse ? $this->width - $x - 1 : $x, $y);
170 $required = $this->getIngredient($x, $y);
171
172 if($required === null){
173 if(!$given->isNull()){
174 return false; //hole, such as that in the center of a chest recipe, should not be filled
175 }
176 }elseif(!$required->accepts($given)){
177 return false;
178 }
179 }
180 }
181
182 return true;
183 }
184
185 public function matchesCraftingGrid(CraftingGrid $grid) : bool{
186 if($this->width !== $grid->getRecipeWidth() || $this->height !== $grid->getRecipeHeight()){
187 return false;
188 }
189
190 return $this->matchInputMap($grid, false) || $this->matchInputMap($grid, true);
191 }
192}
__construct(array $shape, array $ingredients, array $results)
static cloneObjectArray(array $array)
Definition: Utils.php:185