@@ -79,25 +79,39 @@ class CraftingManager{
79
79
/** @phpstan-var ObjectSet<\Closure() : void> */
80
80
private ObjectSet $ recipeRegisteredCallbacks ;
81
81
82
+ /** @phpstan-var ObjectSet<\Closure() : void> */
83
+ private ObjectSet $ recipeUnregisteredCallbacks ;
84
+
82
85
public function __construct (){
83
86
$ this ->recipeRegisteredCallbacks = new ObjectSet ();
87
+ $ this ->recipeUnregisteredCallbacks = new ObjectSet ();
88
+
84
89
foreach (FurnaceType::cases () as $ furnaceType ){
85
90
$ this ->furnaceRecipeManagers [spl_object_id ($ furnaceType )] = new FurnaceRecipeManager ();
86
91
}
87
92
88
93
$ recipeRegisteredCallbacks = $ this ->recipeRegisteredCallbacks ;
94
+ $ recipeUnregisteredCallbacks = $ this ->recipeUnregisteredCallbacks ;
89
95
foreach ($ this ->furnaceRecipeManagers as $ furnaceRecipeManager ){
90
96
$ furnaceRecipeManager ->getRecipeRegisteredCallbacks ()->add (static function (FurnaceRecipe $ recipe ) use ($ recipeRegisteredCallbacks ) : void {
91
97
foreach ($ recipeRegisteredCallbacks as $ callback ){
92
98
$ callback ();
93
99
}
94
100
});
101
+ $ furnaceRecipeManager ->getRecipeUnregisteredCallbacks ()->add (static function (FurnaceRecipe $ recipe ) use ($ recipeUnregisteredCallbacks ) : void {
102
+ foreach ($ recipeUnregisteredCallbacks as $ callback ){
103
+ $ callback ();
104
+ }
105
+ });
95
106
}
96
107
}
97
108
98
109
/** @phpstan-return ObjectSet<\Closure() : void> */
99
110
public function getRecipeRegisteredCallbacks () : ObjectSet { return $ this ->recipeRegisteredCallbacks ; }
100
111
112
+ /** @phpstan-return ObjectSet<\Closure() : void> */
113
+ public function getRecipeUnregisteredCallbacks () : ObjectSet { return $ this ->recipeUnregisteredCallbacks ; }
114
+
101
115
/**
102
116
* Function used to arrange Shapeless Recipe ingredient lists into a consistent order.
103
117
*/
@@ -206,6 +220,34 @@ public function registerShapedRecipe(ShapedRecipe $recipe) : void{
206
220
}
207
221
}
208
222
223
+ public function unregisterShapedRecipe (ShapedRecipe $ recipe ) : void {
224
+ $ edited = false ;
225
+ $ hash = self ::hashOutputs ($ recipe ->getResults ());
226
+
227
+ foreach ($ this ->shapedRecipes [$ hash ] ?? [] as $ i => $ r ){
228
+ if ($ r === $ recipe ){
229
+ unset($ this ->shapedRecipes [$ hash ][$ i ]);
230
+ if (count ($ this ->shapedRecipes [$ hash ]) === 0 ){
231
+ unset($ this ->shapedRecipes [$ hash ]);
232
+ $ edited = true ;
233
+ }
234
+ break ;
235
+ }
236
+ }
237
+
238
+ $ index = array_search ($ recipe , $ this ->craftingRecipeIndex , true );
239
+ if ($ index !== false ){
240
+ unset($ this ->craftingRecipeIndex [$ index ]);
241
+ $ edited = true ;
242
+ }
243
+
244
+ if ($ edited ){
245
+ foreach ($ this ->recipeUnregisteredCallbacks as $ callback ){
246
+ $ callback ();
247
+ }
248
+ }
249
+ }
250
+
209
251
public function registerShapelessRecipe (ShapelessRecipe $ recipe ) : void {
210
252
$ this ->shapelessRecipes [self ::hashOutputs ($ recipe ->getResults ())][] = $ recipe ;
211
253
$ this ->craftingRecipeIndex [] = $ recipe ;
@@ -215,6 +257,34 @@ public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{
215
257
}
216
258
}
217
259
260
+ public function unregisterShapelessRecipe (ShapelessRecipe $ recipe ) : void {
261
+ $ edited = false ;
262
+ $ hash = self ::hashOutputs ($ recipe ->getResults ());
263
+
264
+ foreach ($ this ->shapelessRecipes [$ hash ] ?? [] as $ i => $ r ){
265
+ if ($ r === $ recipe ){
266
+ unset($ this ->shapelessRecipes [$ hash ][$ i ]);
267
+ if (count ($ this ->shapelessRecipes [$ hash ]) === 0 ){
268
+ unset($ this ->shapelessRecipes [$ hash ]);
269
+ $ edited = true ;
270
+ }
271
+ break ;
272
+ }
273
+ }
274
+
275
+ $ index = array_search ($ recipe , $ this ->craftingRecipeIndex , true );
276
+ if ($ index !== false ){
277
+ unset($ this ->craftingRecipeIndex [$ index ]);
278
+ $ edited = true ;
279
+ }
280
+
281
+ if ($ edited ){
282
+ foreach ($ this ->recipeUnregisteredCallbacks as $ callback ){
283
+ $ callback ();
284
+ }
285
+ }
286
+ }
287
+
218
288
public function registerPotionTypeRecipe (PotionTypeRecipe $ recipe ) : void {
219
289
$ this ->potionTypeRecipes [] = $ recipe ;
220
290
@@ -223,6 +293,17 @@ public function registerPotionTypeRecipe(PotionTypeRecipe $recipe) : void{
223
293
}
224
294
}
225
295
296
+ public function unregisterPotionTypeRecipe (PotionTypeRecipe $ recipe ) : void {
297
+ $ recipeIndex = array_search ($ recipe , $ this ->potionTypeRecipes , true );
298
+ if ($ recipeIndex !== false ){
299
+ unset($ this ->potionTypeRecipes [$ recipeIndex ]);
300
+
301
+ foreach ($ this ->recipeUnregisteredCallbacks as $ callback ){
302
+ $ callback ();
303
+ }
304
+ }
305
+ }
306
+
226
307
public function registerPotionContainerChangeRecipe (PotionContainerChangeRecipe $ recipe ) : void {
227
308
$ this ->potionContainerChangeRecipes [] = $ recipe ;
228
309
@@ -231,6 +312,17 @@ public function registerPotionContainerChangeRecipe(PotionContainerChangeRecipe
231
312
}
232
313
}
233
314
315
+ public function unregisterPotionContainerChangeRecipe (PotionContainerChangeRecipe $ recipe ) : void {
316
+ $ recipeIndex = array_search ($ recipe , $ this ->potionContainerChangeRecipes , true );
317
+ if ($ recipeIndex !== false ){
318
+ unset($ this ->potionContainerChangeRecipes [$ recipeIndex ]);
319
+
320
+ foreach ($ this ->recipeUnregisteredCallbacks as $ callback ){
321
+ $ callback ();
322
+ }
323
+ }
324
+ }
325
+
234
326
/**
235
327
* @param Item[] $outputs
236
328
*/
0 commit comments