40 use DestructorCallbackTrait;
46 protected array $shapedRecipes = [];
51 protected array $shapelessRecipes = [];
57 private array $craftingRecipeIndex = [];
63 protected array $furnaceRecipeManagers = [];
69 protected array $potionTypeRecipes = [];
75 protected array $potionContainerChangeRecipes = [];
81 private array $brewingRecipeCache = [];
84 private ObjectSet $recipeRegisteredCallbacks;
86 public function __construct(){
87 $this->recipeRegisteredCallbacks =
new ObjectSet();
88 foreach(FurnaceType::cases() as $furnaceType){
92 $recipeRegisteredCallbacks = $this->recipeRegisteredCallbacks;
93 foreach($this->furnaceRecipeManagers as $furnaceRecipeManager){
94 $furnaceRecipeManager->getRecipeRegisteredCallbacks()->add(
static function(
FurnaceRecipe $recipe) use ($recipeRegisteredCallbacks) :
void{
95 foreach($recipeRegisteredCallbacks as $callback){
105 private static function hashOutput(
Item $output) : string{
107 $write->
putVarInt($output->getStateId());
110 return $write->getBuffer();
116 private static function hashOutputs(array $outputs) : string{
117 if(count($outputs) === 1){
118 return self::hashOutput(array_shift($outputs));
121 foreach($outputs as $o){
124 $hash = self::hashOutput($o);
125 $unique[$hash] = $hash;
127 ksort($unique, SORT_STRING);
128 return implode(
"", $unique);
136 return $this->shapelessRecipes;
144 return $this->shapedRecipes;
152 return $this->craftingRecipeIndex;
155 public function getCraftingRecipeFromIndex(
int $index) : ?
CraftingRecipe{
156 return $this->craftingRecipeIndex[$index] ?? null;
159 public function getFurnaceRecipeManager(FurnaceType $furnaceType) : FurnaceRecipeManager{
160 return $this->furnaceRecipeManagers[spl_object_id($furnaceType)];
168 return $this->potionTypeRecipes;
176 return $this->potionContainerChangeRecipes;
179 public function registerShapedRecipe(
ShapedRecipe $recipe) : void{
180 $this->shapedRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
181 $this->craftingRecipeIndex[] = $recipe;
183 foreach($this->recipeRegisteredCallbacks as $callback){
188 public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{
189 $this->shapelessRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
190 $this->craftingRecipeIndex[] = $recipe;
192 foreach($this->recipeRegisteredCallbacks as $callback){
197 public function registerPotionTypeRecipe(PotionTypeRecipe $recipe) : void{
198 $this->potionTypeRecipes[] = $recipe;
200 foreach($this->recipeRegisteredCallbacks as $callback){
205 public function registerPotionContainerChangeRecipe(PotionContainerChangeRecipe $recipe) : void{
206 $this->potionContainerChangeRecipes[] = $recipe;
208 foreach($this->recipeRegisteredCallbacks as $callback){
219 $outputHash = self::hashOutputs($outputs);
221 if(isset($this->shapedRecipes[$outputHash])){
222 foreach($this->shapedRecipes[$outputHash] as $recipe){
223 if($recipe->matchesCraftingGrid($grid)){
229 if(isset($this->shapelessRecipes[$outputHash])){
230 foreach($this->shapelessRecipes[$outputHash] as $recipe){
231 if($recipe->matchesCraftingGrid($grid)){
249 $outputHash = self::hashOutputs($outputs);
251 if(isset($this->shapedRecipes[$outputHash])){
252 foreach($this->shapedRecipes[$outputHash] as $recipe){
257 if(isset($this->shapelessRecipes[$outputHash])){
258 foreach($this->shapelessRecipes[$outputHash] as $recipe){
264 public function matchBrewingRecipe(Item $input, Item $ingredient) : ?BrewingRecipe{
265 $inputHash = $input->getStateId();
266 $ingredientHash = $ingredient->getStateId();
267 $cached = $this->brewingRecipeCache[$inputHash][$ingredientHash] ??
null;
268 if($cached !==
null){
272 foreach($this->potionContainerChangeRecipes as $recipe){
273 if($recipe->getIngredient()->accepts($ingredient) && $recipe->getResultFor($input) !==
null){
274 return $this->brewingRecipeCache[$inputHash][$ingredientHash] = $recipe;
278 foreach($this->potionTypeRecipes as $recipe){
279 if($recipe->getIngredient()->accepts($ingredient) && $recipe->getResultFor($input) !==
null){
280 return $this->brewingRecipeCache[$inputHash][$ingredientHash] = $recipe;