PocketMine-MP 5.27.1 git-9af3cde03fabbe4129c79e46dc87ffa0fff446e6
Loading...
Searching...
No Matches
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 Utils::validateArrayValueType($ingredients, function(RecipeIngredient $_) : void{});
57 $this->ingredients = $ingredients;
58 Utils::validateArrayValueType($results, function(Item $_) : void{});
59 $this->results = Utils::cloneObjectArray($results);
60 }
61
66 public function getResults() : array{
67 return Utils::cloneObjectArray($this->results);
68 }
69
70 public function getResultsFor(CraftingGrid $grid) : array{
71 return $this->getResults();
72 }
73
74 public function getType() : ShapelessRecipeType{
75 return $this->type;
76 }
77
78 public function getIngredientList() : array{
79 return $this->ingredients;
80 }
81
82 public function getIngredientCount() : int{
83 return count($this->ingredients);
84 }
85
86 public function matchesCraftingGrid(CraftingGrid $grid) : bool{
87 //don't pack the ingredients - shapeless recipes require that each ingredient be in a separate slot
88 $input = $grid->getContents();
89
90 foreach($this->ingredients as $ingredient){
91 foreach($input as $j => $haveItem){
92 if($ingredient->accepts($haveItem)){
93 unset($input[$j]);
94 continue 2;
95 }
96 }
97
98 return false; //failed to match the needed item to a given item
99 }
100
101 return count($input) === 0; //crafting grid should be empty apart from the given ingredient stacks
102 }
103}
__construct(array $ingredients, array $results, ShapelessRecipeType $type)