PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
CraftingManager.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
32use function spl_object_id;
33use function usort;
34
36 use DestructorCallbackTrait;
37
42 protected array $shapedRecipes = [];
47 protected array $shapelessRecipes = [];
48
53 private array $craftingRecipeIndex = [];
54
59 protected array $furnaceRecipeManagers = [];
60
65 protected array $potionTypeRecipes = [];
66
71 protected array $potionContainerChangeRecipes = [];
72
77 private array $brewingRecipeCache = [];
78
80 private ObjectSet $recipeRegisteredCallbacks;
81
82 public function __construct(){
83 $this->recipeRegisteredCallbacks = new ObjectSet();
84 foreach(FurnaceType::cases() as $furnaceType){
85 $this->furnaceRecipeManagers[spl_object_id($furnaceType)] = new FurnaceRecipeManager();
86 }
87
88 $recipeRegisteredCallbacks = $this->recipeRegisteredCallbacks;
89 foreach($this->furnaceRecipeManagers as $furnaceRecipeManager){
90 $furnaceRecipeManager->getRecipeRegisteredCallbacks()->add(static function(FurnaceRecipe $recipe) use ($recipeRegisteredCallbacks) : void{
91 foreach($recipeRegisteredCallbacks as $callback){
92 $callback();
93 }
94 });
95 }
96 }
97
99 public function getRecipeRegisteredCallbacks() : ObjectSet{ return $this->recipeRegisteredCallbacks; }
100
104 public static function sort(Item $i1, Item $i2) : int{
105 //Use spaceship operator to compare each property, then try the next one if they are equivalent.
106 ($retval = $i1->getStateId() <=> $i2->getStateId()) === 0 && ($retval = $i1->getCount() <=> $i2->getCount()) === 0;
107
108 return $retval;
109 }
110
116 private static function pack(array $items) : array{
118 $result = [];
119
120 foreach($items as $i => $item){
121 foreach($result as $otherItem){
122 if($item->canStackWith($otherItem)){
123 $otherItem->setCount($otherItem->getCount() + $item->getCount());
124 continue 2;
125 }
126 }
127
128 //No matching item found
129 $result[] = clone $item;
130 }
131
132 return $result;
133 }
134
138 private static function hashOutputs(array $outputs) : string{
139 $outputs = self::pack($outputs);
140 usort($outputs, [self::class, "sort"]);
141 $result = new BinaryStream();
142 foreach($outputs as $o){
143 //count is not written because the outputs might be from multiple repetitions of a single recipe
144 //this reduces the accuracy of the hash, but it won't matter in most cases.
145 $result->putVarInt($o->getStateId());
146 $result->put((new LittleEndianNbtSerializer())->write(new TreeRoot($o->getNamedTag())));
147 }
148
149 return $result->getBuffer();
150 }
151
156 public function getShapelessRecipes() : array{
157 return $this->shapelessRecipes;
158 }
159
164 public function getShapedRecipes() : array{
165 return $this->shapedRecipes;
166 }
167
172 public function getCraftingRecipeIndex() : array{
173 return $this->craftingRecipeIndex;
174 }
175
176 public function getCraftingRecipeFromIndex(int $index) : ?CraftingRecipe{
177 return $this->craftingRecipeIndex[$index] ?? null;
178 }
179
180 public function getFurnaceRecipeManager(FurnaceType $furnaceType) : FurnaceRecipeManager{
181 return $this->furnaceRecipeManagers[spl_object_id($furnaceType)];
182 }
183
188 public function getPotionTypeRecipes() : array{
189 return $this->potionTypeRecipes;
190 }
191
196 public function getPotionContainerChangeRecipes() : array{
197 return $this->potionContainerChangeRecipes;
198 }
199
200 public function registerShapedRecipe(ShapedRecipe $recipe) : void{
201 $this->shapedRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
202 $this->craftingRecipeIndex[] = $recipe;
203
204 foreach($this->recipeRegisteredCallbacks as $callback){
205 $callback();
206 }
207 }
208
209 public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{
210 $this->shapelessRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
211 $this->craftingRecipeIndex[] = $recipe;
212
213 foreach($this->recipeRegisteredCallbacks as $callback){
214 $callback();
215 }
216 }
217
218 public function registerPotionTypeRecipe(PotionTypeRecipe $recipe) : void{
219 $this->potionTypeRecipes[] = $recipe;
220
221 foreach($this->recipeRegisteredCallbacks as $callback){
222 $callback();
223 }
224 }
225
226 public function registerPotionContainerChangeRecipe(PotionContainerChangeRecipe $recipe) : void{
227 $this->potionContainerChangeRecipes[] = $recipe;
228
229 foreach($this->recipeRegisteredCallbacks as $callback){
230 $callback();
231 }
232 }
233
237 public function matchRecipe(CraftingGrid $grid, array $outputs) : ?CraftingRecipe{
238 //TODO: try to match special recipes before anything else (first they need to be implemented!)
239
240 $outputHash = self::hashOutputs($outputs);
241
242 if(isset($this->shapedRecipes[$outputHash])){
243 foreach($this->shapedRecipes[$outputHash] as $recipe){
244 if($recipe->matchesCraftingGrid($grid)){
245 return $recipe;
246 }
247 }
248 }
249
250 if(isset($this->shapelessRecipes[$outputHash])){
251 foreach($this->shapelessRecipes[$outputHash] as $recipe){
252 if($recipe->matchesCraftingGrid($grid)){
253 return $recipe;
254 }
255 }
256 }
257
258 return null;
259 }
260
267 public function matchRecipeByOutputs(array $outputs) : \Generator{
268 //TODO: try to match special recipes before anything else (first they need to be implemented!)
269
270 $outputHash = self::hashOutputs($outputs);
271
272 if(isset($this->shapedRecipes[$outputHash])){
273 foreach($this->shapedRecipes[$outputHash] as $recipe){
274 yield $recipe;
275 }
276 }
277
278 if(isset($this->shapelessRecipes[$outputHash])){
279 foreach($this->shapelessRecipes[$outputHash] as $recipe){
280 yield $recipe;
281 }
282 }
283 }
284
285 public function matchBrewingRecipe(Item $input, Item $ingredient) : ?BrewingRecipe{
286 $inputHash = $input->getStateId();
287 $ingredientHash = $ingredient->getStateId();
288 $cached = $this->brewingRecipeCache[$inputHash][$ingredientHash] ?? null;
289 if($cached !== null){
290 return $cached;
291 }
292
293 foreach($this->potionContainerChangeRecipes as $recipe){
294 if($recipe->getIngredient()->accepts($ingredient) && $recipe->getResultFor($input) !== null){
295 return $this->brewingRecipeCache[$inputHash][$ingredientHash] = $recipe;
296 }
297 }
298
299 foreach($this->potionTypeRecipes as $recipe){
300 if($recipe->getIngredient()->accepts($ingredient) && $recipe->getResultFor($input) !== null){
301 return $this->brewingRecipeCache[$inputHash][$ingredientHash] = $recipe;
302 }
303 }
304
305 return null;
306 }
307}
static sort(Item $i1, Item $i2)
matchRecipe(CraftingGrid $grid, array $outputs)
add(object ... $objects)
Definition: ObjectSet.php:41