Skip to content
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
wants to merge 5 commits into
base: minor-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/crafting/CraftingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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){
Copy link
Member

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

foreach($this->recipeUnregisteredCallbacks as $callback){
$callback();
}
}
}

public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{
$this->shapelessRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
$this->craftingRecipeIndex[] = $recipe;
Expand All @@ -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){
Copy link
Member

Choose a reason for hiding this comment

The 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;

Expand All @@ -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;

Expand All @@ -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
*/
Expand Down
23 changes: 23 additions & 0 deletions src/crafting/FurnaceRecipeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use pocketmine\item\Item;
use pocketmine\utils\ObjectSet;
use function array_search;

final class FurnaceRecipeManager{
/** @var FurnaceRecipe[] */
Expand All @@ -39,8 +40,12 @@ final class FurnaceRecipeManager{
/** @phpstan-var ObjectSet<\Closure(FurnaceRecipe) : void> */
private ObjectSet $recipeRegisteredCallbacks;

/** @phpstan-var ObjectSet<\Closure(FurnaceRecipe) : void> */
private ObjectSet $recipeUnregisteredCallbacks;

public function __construct(){
$this->recipeRegisteredCallbacks = new ObjectSet();
$this->recipeUnregisteredCallbacks = new ObjectSet();
}

/**
Expand All @@ -50,6 +55,13 @@ public function getRecipeRegisteredCallbacks() : ObjectSet{
return $this->recipeRegisteredCallbacks;
}

/**
* @phpstan-return ObjectSet<\Closure(FurnaceRecipe) : void>
*/
public function getRecipeUnregisteredCallbacks() : ObjectSet{
return $this->recipeUnregisteredCallbacks;
}

/**
* @return FurnaceRecipe[]
*/
Expand All @@ -64,6 +76,17 @@ public function register(FurnaceRecipe $recipe) : void{
}
}

public function unregister(FurnaceRecipe $recipe) : void{
$index = array_search($recipe, $this->furnaceRecipes, true);
if($index !== false){
unset($this->furnaceRecipes[$index]);

foreach($this->recipeUnregisteredCallbacks as $callback){
$callback($recipe);
dktapps marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

public function match(Item $input) : ?FurnaceRecipe{
$index = $input->getStateId();
$simpleRecipe = $this->lookupCache[$index] ?? null;
Expand Down
3 changes: 3 additions & 0 deletions src/network/mcpe/cache/CraftingDataCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public function getCache(CraftingManager $manager) : CraftingDataPacket{
$manager->getRecipeRegisteredCallbacks()->add(function() use ($id) : void{
unset($this->caches[$id]);
});
$manager->getRecipeUnregisteredCallbacks()->add(function() use ($id) : void{
unset($this->caches[$id]);
});
$this->caches[$id] = $this->buildCraftingDataCache($manager);
}
return $this->caches[$id];
Expand Down