-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement unregister functions in CraftingManager #5809
Open
ShockedPlot7560
wants to merge
5
commits into
pmmp:minor-next
Choose a base branch
from
ShockedPlot7560:feat/craft-unregister
base: minor-next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+120
−0
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e141ca4
implement unregister functions in CraftingManager
ShockedPlot7560 59b0d73
use specific observer for recipe deregistration
ShockedPlot7560 c9fee5a
use specific observer for FurnaceRecipe deregistering
ShockedPlot7560 4690f7e
fix PHPstan + function placement
ShockedPlot7560 185a742
Merge branch 'minor-next' into feat/craft-unregister
dktapps File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ | |
use pocketmine\utils\BinaryStream; | ||
use pocketmine\utils\DestructorCallbackTrait; | ||
use pocketmine\utils\ObjectSet; | ||
use function array_search; | ||
use function count; | ||
use function usort; | ||
|
||
class CraftingManager{ | ||
|
@@ -78,25 +80,39 @@ class CraftingManager{ | |
/** @phpstan-var ObjectSet<\Closure() : void> */ | ||
private ObjectSet $recipeRegisteredCallbacks; | ||
|
||
/** @phpstan-var ObjectSet<\Closure() : void> */ | ||
private ObjectSet $recipeUnregisteredCallbacks; | ||
|
||
public function __construct(){ | ||
$this->recipeRegisteredCallbacks = new ObjectSet(); | ||
$this->recipeUnregisteredCallbacks = new ObjectSet(); | ||
|
||
foreach(FurnaceType::getAll() as $furnaceType){ | ||
$this->furnaceRecipeManagers[$furnaceType->id()] = new FurnaceRecipeManager(); | ||
} | ||
|
||
$recipeRegisteredCallbacks = $this->recipeRegisteredCallbacks; | ||
$recipeUnregisteredCallbacks = $this->recipeUnregisteredCallbacks; | ||
foreach($this->furnaceRecipeManagers as $furnaceRecipeManager){ | ||
$furnaceRecipeManager->getRecipeRegisteredCallbacks()->add(static function(FurnaceRecipe $recipe) use ($recipeRegisteredCallbacks) : void{ | ||
foreach($recipeRegisteredCallbacks as $callback){ | ||
$callback(); | ||
} | ||
}); | ||
$furnaceRecipeManager->getRecipeUnregisteredCallbacks()->add(static function(FurnaceRecipe $recipe) use ($recipeUnregisteredCallbacks) : void{ | ||
foreach($recipeUnregisteredCallbacks as $callback){ | ||
$callback(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** @phpstan-return ObjectSet<\Closure() : void> */ | ||
public function getRecipeRegisteredCallbacks() : ObjectSet{ return $this->recipeRegisteredCallbacks; } | ||
|
||
/** @phpstan-return ObjectSet<\Closure() : void> */ | ||
public function getRecipeUnregisteredCallbacks() : ObjectSet{ return $this->recipeUnregisteredCallbacks; } | ||
|
||
/** | ||
* Function used to arrange Shapeless Recipe ingredient lists into a consistent order. | ||
*/ | ||
|
@@ -205,6 +221,34 @@ public function registerShapedRecipe(ShapedRecipe $recipe) : void{ | |
} | ||
} | ||
|
||
public function unregisterShapedRecipe(ShapedRecipe $recipe) : void{ | ||
$edited = false; | ||
$hash = self::hashOutputs($recipe->getResults()); | ||
|
||
foreach($this->shapedRecipes[$hash] ?? [] as $i => $r){ | ||
if($r === $recipe){ | ||
unset($this->shapedRecipes[$hash][$i]); | ||
if(count($this->shapedRecipes[$hash]) === 0){ | ||
unset($this->shapedRecipes[$hash]); | ||
$edited = true; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
$index = array_search($recipe, $this->craftingRecipeIndex, true); | ||
if($index !== false){ | ||
unset($this->craftingRecipeIndex[$index]); | ||
$edited = true; | ||
} | ||
|
||
if($edited){ | ||
foreach($this->recipeUnregisteredCallbacks as $callback){ | ||
$callback(); | ||
} | ||
} | ||
} | ||
|
||
public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{ | ||
$this->shapelessRecipes[self::hashOutputs($recipe->getResults())][] = $recipe; | ||
$this->craftingRecipeIndex[] = $recipe; | ||
|
@@ -214,6 +258,34 @@ public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{ | |
} | ||
} | ||
|
||
public function unregisterShapelessRecipe(ShapelessRecipe $recipe) : void{ | ||
$edited = false; | ||
$hash = self::hashOutputs($recipe->getResults()); | ||
|
||
foreach($this->shapelessRecipes[$hash] ?? [] as $i => $r){ | ||
if($r === $recipe){ | ||
unset($this->shapelessRecipes[$hash][$i]); | ||
if(count($this->shapelessRecipes[$hash]) === 0){ | ||
unset($this->shapelessRecipes[$hash]); | ||
$edited = true; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
$index = array_search($recipe, $this->craftingRecipeIndex, true); | ||
dktapps marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if($index !== false){ | ||
unset($this->craftingRecipeIndex[$index]); | ||
$edited = true; | ||
} | ||
|
||
if($edited){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same again here |
||
foreach($this->recipeUnregisteredCallbacks as $callback){ | ||
$callback(); | ||
} | ||
} | ||
} | ||
|
||
public function registerPotionTypeRecipe(PotionTypeRecipe $recipe) : void{ | ||
$this->potionTypeRecipes[] = $recipe; | ||
|
||
|
@@ -222,6 +294,17 @@ public function registerPotionTypeRecipe(PotionTypeRecipe $recipe) : void{ | |
} | ||
} | ||
|
||
public function unregisterPotionTypeRecipe(PotionTypeRecipe $recipe) : void{ | ||
$recipeIndex = array_search($recipe, $this->potionTypeRecipes, true); | ||
if($recipeIndex !== false){ | ||
unset($this->potionTypeRecipes[$recipeIndex]); | ||
|
||
foreach($this->recipeUnregisteredCallbacks as $callback){ | ||
$callback(); | ||
} | ||
} | ||
} | ||
|
||
public function registerPotionContainerChangeRecipe(PotionContainerChangeRecipe $recipe) : void{ | ||
$this->potionContainerChangeRecipes[] = $recipe; | ||
|
||
|
@@ -230,6 +313,17 @@ public function registerPotionContainerChangeRecipe(PotionContainerChangeRecipe | |
} | ||
} | ||
|
||
public function unregisterPotionContainerChangeRecipe(PotionContainerChangeRecipe $recipe) : void{ | ||
$recipeIndex = array_search($recipe, $this->potionContainerChangeRecipes, true); | ||
if($recipeIndex !== false){ | ||
unset($this->potionContainerChangeRecipes[$recipeIndex]); | ||
|
||
foreach($this->recipeUnregisteredCallbacks as $callback){ | ||
$callback(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param Item[] $outputs | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$changed
would be more consistent with code convention