PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
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
32 private array $ingredients = [];
34 private array $results;
35 private ShapelessRecipeType $type;
36
41 public function __construct(array $ingredients, array $results, ShapelessRecipeType $type){
42 $this->type = $type;
43
44 if(count($ingredients) > 9){
45 throw new \InvalidArgumentException("Shapeless recipes cannot have more than 9 ingredients");
46 }
47 $this->ingredients = $ingredients;
48 $this->results = Utils::cloneObjectArray($results);
49 }
50
54 public function getResults() : array{
55 return Utils::cloneObjectArray($this->results);
56 }
57
58 public function getResultsFor(CraftingGrid $grid) : array{
59 return $this->getResults();
60 }
61
62 public function getType() : ShapelessRecipeType{
63 return $this->type;
64 }
65
69 public function getIngredientList() : array{
70 return $this->ingredients;
71 }
72
73 public function getIngredientCount() : int{
74 return count($this->ingredients);
75 }
76
77 public function matchesCraftingGrid(CraftingGrid $grid) : bool{
78 //don't pack the ingredients - shapeless recipes require that each ingredient be in a separate slot
79 $input = $grid->getContents();
80
81 foreach($this->ingredients as $ingredient){
82 foreach($input as $j => $haveItem){
83 if($ingredient->accepts($haveItem)){
84 unset($input[$j]);
85 continue 2;
86 }
87 }
88
89 return false; //failed to match the needed item to a given item
90 }
91
92 return count($input) === 0; //crafting grid should be empty apart from the given ingredient stacks
93 }
94}
__construct(array $ingredients, array $results, ShapelessRecipeType $type)
static cloneObjectArray(array $array)
Definition: Utils.php:185