41 use DestructorCallbackTrait;
47 protected array $shapedRecipes = [];
52 protected array $shapelessRecipes = [];
58 private array $craftingRecipeIndex = [];
64 protected array $furnaceRecipeManagers = [];
70 protected array $potionTypeRecipes = [];
76 protected array $potionContainerChangeRecipes = [];
82 private array $brewingRecipeCache = [];
85 private ObjectSet $recipeRegisteredCallbacks;
87 public function __construct(){
88 $this->recipeRegisteredCallbacks =
new ObjectSet();
89 foreach(FurnaceType::cases() as $furnaceType){
93 $recipeRegisteredCallbacks = $this->recipeRegisteredCallbacks;
94 foreach($this->furnaceRecipeManagers as $furnaceRecipeManager){
95 $furnaceRecipeManager->getRecipeRegisteredCallbacks()->add(
static function(
FurnaceRecipe $recipe) use ($recipeRegisteredCallbacks) :
void{
96 foreach($recipeRegisteredCallbacks as $callback){
106 private static function hashOutput(
Item $output) : string{
107 $write = new ByteBufferWriter();
108 VarInt::writeSignedInt($write, $output->getStateId());
113 return $write->getData();
119 private static function hashOutputs(array $outputs) : string{
120 if(count($outputs) === 1){
121 return self::hashOutput(array_shift($outputs));
124 foreach($outputs as $o){
127 $hash = self::hashOutput($o);
128 $unique[$hash] = $hash;
130 ksort($unique, SORT_STRING);
131 return implode(
"", $unique);
139 return $this->shapelessRecipes;
147 return $this->shapedRecipes;
155 return $this->craftingRecipeIndex;
158 public function getCraftingRecipeFromIndex(
int $index) : ?
CraftingRecipe{
159 return $this->craftingRecipeIndex[$index] ?? null;
162 public function getFurnaceRecipeManager(FurnaceType $furnaceType) : FurnaceRecipeManager{
163 return $this->furnaceRecipeManagers[spl_object_id($furnaceType)];
171 return $this->potionTypeRecipes;
179 return $this->potionContainerChangeRecipes;
182 public function registerShapedRecipe(
ShapedRecipe $recipe) : void{
183 $this->shapedRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
184 $this->craftingRecipeIndex[] = $recipe;
186 foreach($this->recipeRegisteredCallbacks as $callback){
191 public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{
192 $this->shapelessRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
193 $this->craftingRecipeIndex[] = $recipe;
195 foreach($this->recipeRegisteredCallbacks as $callback){
200 public function registerPotionTypeRecipe(PotionTypeRecipe $recipe) : void{
201 $this->potionTypeRecipes[] = $recipe;
203 foreach($this->recipeRegisteredCallbacks as $callback){
208 public function registerPotionContainerChangeRecipe(PotionContainerChangeRecipe $recipe) : void{
209 $this->potionContainerChangeRecipes[] = $recipe;
211 foreach($this->recipeRegisteredCallbacks as $callback){
222 $outputHash = self::hashOutputs($outputs);
224 if(isset($this->shapedRecipes[$outputHash])){
225 foreach($this->shapedRecipes[$outputHash] as $recipe){
226 if($recipe->matchesCraftingGrid($grid)){
232 if(isset($this->shapelessRecipes[$outputHash])){
233 foreach($this->shapelessRecipes[$outputHash] as $recipe){
234 if($recipe->matchesCraftingGrid($grid)){
252 $outputHash = self::hashOutputs($outputs);
254 if(isset($this->shapedRecipes[$outputHash])){
255 foreach($this->shapedRecipes[$outputHash] as $recipe){
260 if(isset($this->shapelessRecipes[$outputHash])){
261 foreach($this->shapelessRecipes[$outputHash] as $recipe){
267 public function matchBrewingRecipe(Item $input, Item $ingredient) : ?BrewingRecipe{
268 $inputHash = $input->getStateId();
269 $ingredientHash = $ingredient->getStateId();
270 $cached = $this->brewingRecipeCache[$inputHash][$ingredientHash] ??
null;
271 if($cached !==
null){
275 foreach($this->potionContainerChangeRecipes as $recipe){
276 if($recipe->getIngredient()->accepts($ingredient) && $recipe->getResultFor($input) !==
null){
277 return $this->brewingRecipeCache[$inputHash][$ingredientHash] = $recipe;
281 foreach($this->potionTypeRecipes as $recipe){
282 if($recipe->getIngredient()->accepts($ingredient) && $recipe->getResultFor($input) !==
null){
283 return $this->brewingRecipeCache[$inputHash][$ingredientHash] = $recipe;