Loading [MathJax]/extensions/tex2jax.js
PocketMine-MP 5.23.3 git-4a4572131f27ab967701ceaaf2020cfbe26e375c
All Classes Namespaces Functions Variables Enumerations Enumerator Pages
src/crafting/ShapelessRecipe.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 count;
29
35 private array $ingredients = [];
40 private array $results;
41 private ShapelessRecipeType $type;
42
50 public function __construct(array $ingredients, array $results, ShapelessRecipeType $type){
51 $this->type = $type;
52
53 if(count($ingredients) > 9){
54 throw new \InvalidArgumentException("Shapeless recipes cannot have more than 9 ingredients");
55 }
56 $this->ingredients = $ingredients;
57 $this->results = Utils::cloneObjectArray($results);
58 }
59
64 public function getResults() : array{
65 return Utils::cloneObjectArray($this->results);
66 }
67
68 public function getResultsFor(CraftingGrid $grid) : array{
69 return $this->getResults();
70 }
71
72 public function getType() : ShapelessRecipeType{
73 return $this->type;
74 }
75
76 public function getIngredientList() : array{
77 return $this->ingredients;
78 }
79
80 public function getIngredientCount() : int{
81 return count($this->ingredients);
82 }
83
84 public function matchesCraftingGrid(CraftingGrid $grid) : bool{
85 //don't pack the ingredients - shapeless recipes require that each ingredient be in a separate slot
86 $input = $grid->getContents();
87
88 foreach($this->ingredients as $ingredient){
89 foreach($input as $j => $haveItem){
90 if($ingredient->accepts($haveItem)){
91 unset($input[$j]);
92 continue 2;
93 }
94 }
95
96 return false; //failed to match the needed item to a given item
97 }
98
99 return count($input) === 0; //crafting grid should be empty apart from the given ingredient stacks
100 }
101}
__construct(array $ingredients, array $results, ShapelessRecipeType $type)