From fd33a65e3b36a27d08aa0cbfeac26ec1b1e31b01 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 20 Sep 2017 09:34:00 +0100 Subject: [PATCH] Small cleanup of recipe UUID handling (furnace recipes don't need UUIDs) --- src/pocketmine/inventory/CraftingManager.php | 24 ++++++------ src/pocketmine/inventory/CraftingRecipe.php | 39 ++++++++++++++++++++ src/pocketmine/inventory/FurnaceRecipe.php | 25 +------------ src/pocketmine/inventory/Recipe.php | 12 +----- src/pocketmine/inventory/ShapedRecipe.php | 6 +-- src/pocketmine/inventory/ShapelessRecipe.php | 6 +-- 6 files changed, 59 insertions(+), 53 deletions(-) create mode 100644 src/pocketmine/inventory/CraftingRecipe.php diff --git a/src/pocketmine/inventory/CraftingManager.php b/src/pocketmine/inventory/CraftingManager.php index 02d7d37d1..dd91a4c65 100644 --- a/src/pocketmine/inventory/CraftingManager.php +++ b/src/pocketmine/inventory/CraftingManager.php @@ -34,10 +34,10 @@ use pocketmine\utils\UUID; class CraftingManager{ - /** @var Recipe[] */ + /** @var CraftingRecipe[] */ public $recipes = []; - /** @var Recipe[][] */ + /** @var CraftingRecipe[][] */ protected $recipeLookup = []; /** @var FurnaceRecipe[] */ @@ -148,7 +148,7 @@ class CraftingManager{ /** * @param UUID $id - * @return Recipe|null + * @return CraftingRecipe|null */ public function getRecipe(UUID $id){ $index = $id->toBinary(); @@ -183,7 +183,8 @@ class CraftingManager{ */ public function registerShapedRecipe(ShapedRecipe $recipe){ $result = $recipe->getResult(); - $this->recipes[$recipe->getId()->toBinary()] = $recipe; + + /** @var Item[][] $ingredients */ $ingredients = $recipe->getIngredientMap(); $hash = ""; foreach($ingredients as $v){ @@ -206,7 +207,6 @@ class CraftingManager{ */ public function registerShapelessRecipe(ShapelessRecipe $recipe){ $result = $recipe->getResult(); - $this->recipes[$recipe->getId()->toBinary()] = $recipe; $hash = ""; $ingredients = $recipe->getIngredientList(); usort($ingredients, [$this, "sort"]); @@ -288,15 +288,13 @@ class CraftingManager{ * @param Recipe $recipe */ public function registerRecipe(Recipe $recipe){ - $recipe->setId(UUID::fromData((string) ++self::$RECIPE_COUNT, (string) $recipe->getResult()->getId(), (string) $recipe->getResult()->getDamage(), (string) $recipe->getResult()->getCount(), $recipe->getResult()->getCompoundTag())); - - if($recipe instanceof ShapedRecipe){ - $this->registerShapedRecipe($recipe); - }elseif($recipe instanceof ShapelessRecipe){ - $this->registerShapelessRecipe($recipe); - }elseif($recipe instanceof FurnaceRecipe){ - $this->registerFurnaceRecipe($recipe); + if($recipe instanceof CraftingRecipe){ + $result = $recipe->getResult(); + $recipe->setId($uuid = UUID::fromData((string) ++self::$RECIPE_COUNT, (string) $result->getId(), (string) $result->getDamage(), (string) $result->getCount(), $result->getCompoundTag())); + $this->recipes[$uuid->toBinary()] = $recipe; } + + $recipe->registerToCraftingManager($this); } } diff --git a/src/pocketmine/inventory/CraftingRecipe.php b/src/pocketmine/inventory/CraftingRecipe.php new file mode 100644 index 000000000..644b0e3f5 --- /dev/null +++ b/src/pocketmine/inventory/CraftingRecipe.php @@ -0,0 +1,39 @@ +ingredient = clone $ingredient; } - /** - * @return UUID|null - */ - public function getId(){ - return $this->id; - } - - /** - * @param UUID $id - */ - public function setId(UUID $id){ - if($this->id !== null){ - throw new \InvalidStateException("Id is already set"); - } - - $this->id = $id; - } - /** * @param Item $item */ @@ -86,7 +65,7 @@ class FurnaceRecipe implements Recipe{ return clone $this->output; } - public function registerToCraftingManager(){ - Server::getInstance()->getCraftingManager()->registerFurnaceRecipe($this); + public function registerToCraftingManager(CraftingManager $manager){ + $manager->registerFurnaceRecipe($this); } } \ No newline at end of file diff --git a/src/pocketmine/inventory/Recipe.php b/src/pocketmine/inventory/Recipe.php index 4328a5ff7..282c99f2c 100644 --- a/src/pocketmine/inventory/Recipe.php +++ b/src/pocketmine/inventory/Recipe.php @@ -33,15 +33,5 @@ interface Recipe{ */ public function getResult() : Item; - public function registerToCraftingManager(); - - /** - * @return UUID|null - */ - public function getId(); - - /** - * @param UUID $id - */ - public function setId(UUID $id); + public function registerToCraftingManager(CraftingManager $manager); } \ No newline at end of file diff --git a/src/pocketmine/inventory/ShapedRecipe.php b/src/pocketmine/inventory/ShapedRecipe.php index bcdeb7914..5fdf7c83b 100644 --- a/src/pocketmine/inventory/ShapedRecipe.php +++ b/src/pocketmine/inventory/ShapedRecipe.php @@ -28,7 +28,7 @@ use pocketmine\item\ItemFactory; use pocketmine\Server; use pocketmine\utils\UUID; -class ShapedRecipe implements Recipe{ +class ShapedRecipe implements CraftingRecipe{ /** @var Item */ private $output; @@ -168,7 +168,7 @@ class ShapedRecipe implements Recipe{ return $this->shape; } - public function registerToCraftingManager(){ - Server::getInstance()->getCraftingManager()->registerShapedRecipe($this); + public function registerToCraftingManager(CraftingManager $manager){ + $manager->registerShapedRecipe($this); } } diff --git a/src/pocketmine/inventory/ShapelessRecipe.php b/src/pocketmine/inventory/ShapelessRecipe.php index ba00a0efd..d601ab675 100644 --- a/src/pocketmine/inventory/ShapelessRecipe.php +++ b/src/pocketmine/inventory/ShapelessRecipe.php @@ -27,7 +27,7 @@ use pocketmine\item\Item; use pocketmine\Server; use pocketmine\utils\UUID; -class ShapelessRecipe implements Recipe{ +class ShapelessRecipe implements CraftingRecipe{ /** @var Item */ private $output; @@ -129,7 +129,7 @@ class ShapelessRecipe implements Recipe{ return $count; } - public function registerToCraftingManager(){ - Server::getInstance()->getCraftingManager()->registerShapelessRecipe($this); + public function registerToCraftingManager(CraftingManager $manager){ + $manager->registerShapelessRecipe($this); } } \ No newline at end of file