36 use DestructorCallbackTrait;
42 protected array $shapedRecipes = [];
47 protected array $shapelessRecipes = [];
53 private array $craftingRecipeIndex = [];
59 protected array $furnaceRecipeManagers = [];
65 protected array $potionTypeRecipes = [];
71 protected array $potionContainerChangeRecipes = [];
77 private array $brewingRecipeCache = [];
80 private ObjectSet $recipeRegisteredCallbacks;
82 public function __construct(){
83 $this->recipeRegisteredCallbacks =
new ObjectSet();
84 foreach(FurnaceType::cases() as $furnaceType){
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){
106 ($retval = $i1->getStateId() <=> $i2->getStateId()) === 0 && ($retval = $i1->getCount() <=> $i2->getCount()) === 0;
118 private static function pack(array $items) : array{
121 foreach($items as $item){
122 foreach($result as $otherItem){
123 if($item->canStackWith($otherItem)){
124 $otherItem->setCount($otherItem->getCount() + $item->getCount());
130 $result[] = clone $item;
140 private static function hashOutputs(array $outputs) : string{
141 $outputs = self::pack($outputs);
142 usort($outputs, [self::class,
"sort"]);
143 $result =
new BinaryStream();
144 foreach($outputs as $o){
147 $result->putVarInt($o->getStateId());
148 $result->put((
new LittleEndianNbtSerializer())->write(
new TreeRoot($o->getNamedTag())));
151 return $result->getBuffer();
159 return $this->shapelessRecipes;
167 return $this->shapedRecipes;
175 return $this->craftingRecipeIndex;
178 public function getCraftingRecipeFromIndex(
int $index) : ?
CraftingRecipe{
179 return $this->craftingRecipeIndex[$index] ?? null;
182 public function getFurnaceRecipeManager(FurnaceType $furnaceType) : FurnaceRecipeManager{
183 return $this->furnaceRecipeManagers[spl_object_id($furnaceType)];
191 return $this->potionTypeRecipes;
199 return $this->potionContainerChangeRecipes;
202 public function registerShapedRecipe(
ShapedRecipe $recipe) : void{
203 $this->shapedRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
204 $this->craftingRecipeIndex[] = $recipe;
206 foreach($this->recipeRegisteredCallbacks as $callback){
211 public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{
212 $this->shapelessRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
213 $this->craftingRecipeIndex[] = $recipe;
215 foreach($this->recipeRegisteredCallbacks as $callback){
220 public function registerPotionTypeRecipe(PotionTypeRecipe $recipe) : void{
221 $this->potionTypeRecipes[] = $recipe;
223 foreach($this->recipeRegisteredCallbacks as $callback){
228 public function registerPotionContainerChangeRecipe(PotionContainerChangeRecipe $recipe) : void{
229 $this->potionContainerChangeRecipes[] = $recipe;
231 foreach($this->recipeRegisteredCallbacks as $callback){
242 $outputHash = self::hashOutputs($outputs);
244 if(isset($this->shapedRecipes[$outputHash])){
245 foreach($this->shapedRecipes[$outputHash] as $recipe){
246 if($recipe->matchesCraftingGrid($grid)){
252 if(isset($this->shapelessRecipes[$outputHash])){
253 foreach($this->shapelessRecipes[$outputHash] as $recipe){
254 if($recipe->matchesCraftingGrid($grid)){
272 $outputHash = self::hashOutputs($outputs);
274 if(isset($this->shapedRecipes[$outputHash])){
275 foreach($this->shapedRecipes[$outputHash] as $recipe){
280 if(isset($this->shapelessRecipes[$outputHash])){
281 foreach($this->shapelessRecipes[$outputHash] as $recipe){
287 public function matchBrewingRecipe(Item $input, Item $ingredient) : ?BrewingRecipe{
288 $inputHash = $input->getStateId();
289 $ingredientHash = $ingredient->getStateId();
290 $cached = $this->brewingRecipeCache[$inputHash][$ingredientHash] ??
null;
291 if($cached !==
null){
295 foreach($this->potionContainerChangeRecipes as $recipe){
296 if($recipe->getIngredient()->accepts($ingredient) && $recipe->getResultFor($input) !==
null){
297 return $this->brewingRecipeCache[$inputHash][$ingredientHash] = $recipe;
301 foreach($this->potionTypeRecipes as $recipe){
302 if($recipe->getIngredient()->accepts($ingredient) && $recipe->getResultFor($input) !==
null){
303 return $this->brewingRecipeCache[$inputHash][$ingredientHash] = $recipe;