Skip to content

Commit 9c3e11a

Browse files
committed
1 parent 6a25e66 commit 9c3e11a

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Additions
4949
> https://github.com/pmmp/PocketMine-MP/pull/5583
5050
> https://github.com/pmmp/PocketMine-MP/pull/5800
5151
> https://github.com/pmmp/PocketMine-MP/pull/5805
52+
> https://github.com/pmmp/PocketMine-MP/pull/5809
5253
5354

5455
## What is this?

src/crafting/CraftingManager.php

+92
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,39 @@ class CraftingManager{
7979
/** @phpstan-var ObjectSet<\Closure() : void> */
8080
private ObjectSet $recipeRegisteredCallbacks;
8181

82+
/** @phpstan-var ObjectSet<\Closure() : void> */
83+
private ObjectSet $recipeUnregisteredCallbacks;
84+
8285
public function __construct(){
8386
$this->recipeRegisteredCallbacks = new ObjectSet();
87+
$this->recipeUnregisteredCallbacks = new ObjectSet();
88+
8489
foreach(FurnaceType::cases() as $furnaceType){
8590
$this->furnaceRecipeManagers[spl_object_id($furnaceType)] = new FurnaceRecipeManager();
8691
}
8792

8893
$recipeRegisteredCallbacks = $this->recipeRegisteredCallbacks;
94+
$recipeUnregisteredCallbacks = $this->recipeUnregisteredCallbacks;
8995
foreach($this->furnaceRecipeManagers as $furnaceRecipeManager){
9096
$furnaceRecipeManager->getRecipeRegisteredCallbacks()->add(static function(FurnaceRecipe $recipe) use ($recipeRegisteredCallbacks) : void{
9197
foreach($recipeRegisteredCallbacks as $callback){
9298
$callback();
9399
}
94100
});
101+
$furnaceRecipeManager->getRecipeUnregisteredCallbacks()->add(static function(FurnaceRecipe $recipe) use ($recipeUnregisteredCallbacks) : void{
102+
foreach($recipeUnregisteredCallbacks as $callback){
103+
$callback();
104+
}
105+
});
95106
}
96107
}
97108

98109
/** @phpstan-return ObjectSet<\Closure() : void> */
99110
public function getRecipeRegisteredCallbacks() : ObjectSet{ return $this->recipeRegisteredCallbacks; }
100111

112+
/** @phpstan-return ObjectSet<\Closure() : void> */
113+
public function getRecipeUnregisteredCallbacks() : ObjectSet{ return $this->recipeUnregisteredCallbacks; }
114+
101115
/**
102116
* Function used to arrange Shapeless Recipe ingredient lists into a consistent order.
103117
*/
@@ -206,6 +220,34 @@ public function registerShapedRecipe(ShapedRecipe $recipe) : void{
206220
}
207221
}
208222

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+
209251
public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{
210252
$this->shapelessRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
211253
$this->craftingRecipeIndex[] = $recipe;
@@ -215,6 +257,34 @@ public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{
215257
}
216258
}
217259

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+
218288
public function registerPotionTypeRecipe(PotionTypeRecipe $recipe) : void{
219289
$this->potionTypeRecipes[] = $recipe;
220290

@@ -223,6 +293,17 @@ public function registerPotionTypeRecipe(PotionTypeRecipe $recipe) : void{
223293
}
224294
}
225295

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+
226307
public function registerPotionContainerChangeRecipe(PotionContainerChangeRecipe $recipe) : void{
227308
$this->potionContainerChangeRecipes[] = $recipe;
228309

@@ -231,6 +312,17 @@ public function registerPotionContainerChangeRecipe(PotionContainerChangeRecipe
231312
}
232313
}
233314

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+
234326
/**
235327
* @param Item[] $outputs
236328
*/

src/crafting/FurnaceRecipeManager.php

+22
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ final class FurnaceRecipeManager{
3939
/** @phpstan-var ObjectSet<\Closure(FurnaceRecipe) : void> */
4040
private ObjectSet $recipeRegisteredCallbacks;
4141

42+
/** @phpstan-var ObjectSet<\Closure(FurnaceRecipe) : void> */
43+
private ObjectSet $recipeUnregisteredCallbacks;
44+
4245
public function __construct(){
4346
$this->recipeRegisteredCallbacks = new ObjectSet();
47+
$this->recipeUnregisteredCallbacks = new ObjectSet();
4448
}
4549

4650
/**
@@ -50,6 +54,13 @@ public function getRecipeRegisteredCallbacks() : ObjectSet{
5054
return $this->recipeRegisteredCallbacks;
5155
}
5256

57+
/**
58+
* @phpstan-return ObjectSet<\Closure(FurnaceRecipe) : void>
59+
*/
60+
public function getRecipeUnregisteredCallbacks() : ObjectSet{
61+
return $this->recipeUnregisteredCallbacks;
62+
}
63+
5364
/**
5465
* @return FurnaceRecipe[]
5566
*/
@@ -64,6 +75,17 @@ public function register(FurnaceRecipe $recipe) : void{
6475
}
6576
}
6677

78+
public function unregister(FurnaceRecipe $recipe) : void {
79+
$index = array_search($recipe, $this->furnaceRecipes, true);
80+
if ($index !== false) {
81+
unset($this->furnaceRecipes[$index]);
82+
83+
foreach($this->recipeUnregisteredCallbacks as $callback) {
84+
$callback($recipe);
85+
}
86+
}
87+
}
88+
6789
public function match(Item $input) : ?FurnaceRecipe{
6890
$index = $input->getStateId();
6991
$simpleRecipe = $this->lookupCache[$index] ?? null;

src/network/mcpe/cache/CraftingDataCache.php

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public function getCache(CraftingManager $manager) : CraftingDataPacket{
6565
$manager->getRecipeRegisteredCallbacks()->add(function() use ($id) : void{
6666
unset($this->caches[$id]);
6767
});
68+
$manager->getRecipeUnregisteredCallbacks()->add(function() use ($id) : void {
69+
unset($this->caches[$id]);
70+
});
6871
$this->caches[$id] = $this->buildCraftingDataCache($manager);
6972
}
7073
return $this->caches[$id];

0 commit comments

Comments
 (0)