22declare(strict_types=1);
24namespace pocketmine\crafting;
28use
function array_values;
31use
function str_contains;
36 private array $shape = [];
38 private array $ingredientList = [];
40 private array $results = [];
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");
66 $shape = array_values($shape);
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");
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) .
")");
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] .
"'");
85 $this->shape = $shape;
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");
92 $this->ingredientList[$char] = clone $i;
98 public function getWidth() : int{
102 public function getHeight() : int{
103 return $this->height;
110 return
Utils::cloneObjectArray($this->results);
117 return $this->getResults();
126 for($y = 0; $y < $this->height; ++$y){
127 for($x = 0; $x < $this->width; ++$x){
128 $ingredients[$y][$x] = $this->getIngredient($x, $y);
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;
153 public function getIngredient(
int $x,
int $y) : ?RecipeIngredient{
154 return $this->ingredientList[$this->shape[$y][$x]] ?? null;
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){
169 $given = $grid->
getIngredient($reverse ? $this->width - $x - 1 : $x, $y);
170 $required = $this->getIngredient($x, $y);
172 if($required ===
null){
173 if(!$given->isNull()){
176 }elseif(!$required->accepts($given)){
186 if($this->width !== $grid->getRecipeWidth() || $this->height !== $grid->getRecipeHeight()){
190 return $this->matchInputMap($grid,
false) || $this->matchInputMap($grid,
true);
getIngredient(int $x, int $y)
matchesCraftingGrid(CraftingGrid $grid)
__construct(array $shape, array $ingredients, array $results)
getResultsFor(CraftingGrid $grid)
static cloneObjectArray(array $array)