PocketMine-MP 5.18.1 git-9381fc4172e5dce4cada1cb356050c8a2ab57b94
FurnaceRecipeManager.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
28
31 protected array $furnaceRecipes = [];
32
37 private array $lookupCache = [];
38
40 private ObjectSet $recipeRegisteredCallbacks;
41
42 public function __construct(){
43 $this->recipeRegisteredCallbacks = new ObjectSet();
44 }
45
50 return $this->recipeRegisteredCallbacks;
51 }
52
56 public function getAll() : array{
57 return $this->furnaceRecipes;
58 }
59
60 public function register(FurnaceRecipe $recipe) : void{
61 $this->furnaceRecipes[] = $recipe;
62 foreach($this->recipeRegisteredCallbacks as $callback){
63 $callback($recipe);
64 }
65 }
66
67 public function match(Item $input) : ?FurnaceRecipe{
68 $index = $input->getStateId();
69 $simpleRecipe = $this->lookupCache[$index] ?? null;
70 if($simpleRecipe !== null){
71 return $simpleRecipe;
72 }
73
74 foreach($this->furnaceRecipes as $recipe){
75 if($recipe->getInput()->accepts($input)){
76 //remember that this item is accepted by this recipe, so we don't need to bruteforce it again
77 $this->lookupCache[$index] = $recipe;
78 return $recipe;
79 }
80 }
81
82 return null;
83 }
84}