extract FurnaceRecipeManager unit from CraftingManager

I'd like to have this directly provided to Furnace, but I don't know how to short of making into a singleton. Since I want to have per-furnace recipe managers (e.g. for stuff like blast furnace vs regular furnace etc), a singleton isn't really an option.
This commit is contained in:
Dylan K. Taylor 2020-06-03 18:59:59 +01:00
parent b58c425189
commit 017afead3b
4 changed files with 83 additions and 20 deletions

View File

@ -155,7 +155,7 @@ class Furnace extends Spawnable implements Container, Nameable{
$fuel = $this->inventory->getFuel(); $fuel = $this->inventory->getFuel();
$raw = $this->inventory->getSmelting(); $raw = $this->inventory->getSmelting();
$product = $this->inventory->getResult(); $product = $this->inventory->getResult();
$smelt = $this->pos->getWorldNonNull()->getServer()->getCraftingManager()->matchFurnaceRecipe($raw); $smelt = $this->pos->getWorldNonNull()->getServer()->getCraftingManager()->getFurnaceRecipeManager()->match($raw);
$canSmelt = ($smelt instanceof FurnaceRecipe and $raw->getCount() > 0 and (($smelt->getResult()->equals($product) and $product->getCount() < $product->getMaxStackSize()) or $product->isNull())); $canSmelt = ($smelt instanceof FurnaceRecipe and $raw->getCount() > 0 and (($smelt->getResult()->equals($product) and $product->getCount() < $product->getMaxStackSize()) or $product->isNull()));
if($this->remainingFuelTime <= 0 and $canSmelt and $fuel->getFuelTime() > 0 and $fuel->getCount() > 0){ if($this->remainingFuelTime <= 0 and $canSmelt and $fuel->getFuelTime() > 0 and $fuel->getCount() > 0){

View File

@ -48,12 +48,20 @@ class CraftingManager{
protected $shapedRecipes = []; protected $shapedRecipes = [];
/** @var ShapelessRecipe[][] */ /** @var ShapelessRecipe[][] */
protected $shapelessRecipes = []; protected $shapelessRecipes = [];
/** @var FurnaceRecipe[] */
protected $furnaceRecipes = []; /** @var FurnaceRecipeManager */
protected $furnaceRecipeManager;
/** @var CompressBatchPromise[] */ /** @var CompressBatchPromise[] */
private $craftingDataCaches = []; private $craftingDataCaches = [];
public function __construct(){
$this->furnaceRecipeManager = new FurnaceRecipeManager();
$this->furnaceRecipeManager->getRecipeRegisteredCallbacks()->add(function(FurnaceRecipe $recipe) : void{
$this->craftingDataCaches = [];
});
}
/** /**
* Rebuilds the cached CraftingDataPacket. * Rebuilds the cached CraftingDataPacket.
*/ */
@ -105,7 +113,7 @@ class CraftingManager{
} }
} }
foreach($this->furnaceRecipes as $recipe){ foreach($this->furnaceRecipeManager->getAll() as $recipe){
$input = $converter->coreItemStackToNet($recipe->getInput()); $input = $converter->coreItemStackToNet($recipe->getInput());
$pk->entries[] = new ProtocolFurnaceRecipe( $pk->entries[] = new ProtocolFurnaceRecipe(
CraftingDataPacket::ENTRY_FURNACE_DATA, CraftingDataPacket::ENTRY_FURNACE_DATA,
@ -198,11 +206,8 @@ class CraftingManager{
return $this->shapedRecipes; return $this->shapedRecipes;
} }
/** public function getFurnaceRecipeManager() : FurnaceRecipeManager{
* @return FurnaceRecipe[] return $this->furnaceRecipeManager;
*/
public function getFurnaceRecipes() : array{
return $this->furnaceRecipes;
} }
public function registerShapedRecipe(ShapedRecipe $recipe) : void{ public function registerShapedRecipe(ShapedRecipe $recipe) : void{
@ -217,12 +222,6 @@ class CraftingManager{
$this->craftingDataCaches = []; $this->craftingDataCaches = [];
} }
public function registerFurnaceRecipe(FurnaceRecipe $recipe) : void{
$input = $recipe->getInput();
$this->furnaceRecipes[$input->getId() . ":" . ($input->hasAnyDamageValue() ? "?" : $input->getMeta())] = $recipe;
$this->craftingDataCaches = [];
}
/** /**
* @param Item[] $outputs * @param Item[] $outputs
*/ */
@ -273,8 +272,4 @@ class CraftingManager{
} }
} }
} }
public function matchFurnaceRecipe(Item $input) : ?FurnaceRecipe{
return $this->furnaceRecipes[$input->getId() . ":" . $input->getMeta()] ?? $this->furnaceRecipes[$input->getId() . ":?"] ?? null;
}
} }

View File

@ -60,7 +60,7 @@ final class CraftingManagerFromDataHelper{
if($recipe["block"] !== "furnace"){ //TODO: filter others out for now to avoid breaking economics if($recipe["block"] !== "furnace"){ //TODO: filter others out for now to avoid breaking economics
break; break;
} }
$result->registerFurnaceRecipe(new FurnaceRecipe( $result->getFurnaceRecipeManager()->register(new FurnaceRecipe(
Item::jsonDeserialize($recipe["output"]), Item::jsonDeserialize($recipe["output"]),
Item::jsonDeserialize($recipe["input"])) Item::jsonDeserialize($recipe["input"]))
); );

View File

@ -0,0 +1,68 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\crafting;
use Ds\Set;
use pocketmine\item\Item;
final class FurnaceRecipeManager{
/** @var FurnaceRecipe[] */
protected $furnaceRecipes = [];
/**
* @var Set
* @phpstan-var Set<\Closure(FurnaceRecipe) : void>
*/
private $recipeRegisteredCallbacks;
public function __construct(){
$this->recipeRegisteredCallbacks = new Set();
}
/**
* @phpstan-return Set<\Closure(FurnaceRecipe) : void>
*/
public function getRecipeRegisteredCallbacks() : Set{
return $this->recipeRegisteredCallbacks;
}
/**
* @return FurnaceRecipe[]
*/
public function getAll() : array{
return $this->furnaceRecipes;
}
public function register(FurnaceRecipe $recipe) : void{
$input = $recipe->getInput();
$this->furnaceRecipes[$input->getId() . ":" . ($input->hasAnyDamageValue() ? "?" : $input->getMeta())] = $recipe;
foreach($this->recipeRegisteredCallbacks as $callback){
$callback($recipe);
}
}
public function match(Item $input) : ?FurnaceRecipe{
return $this->furnaceRecipes[$input->getId() . ":" . $input->getMeta()] ?? $this->furnaceRecipes[$input->getId() . ":?"] ?? null;
}
}