From ec332e3e60b8533d2b4547f4fbb10ad70ee1b2b7 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 28 Mar 2018 20:03:04 +0100 Subject: [PATCH] Fill null UUIDs in CraftingDataPacket, remove all UUID things from CraftingRecipe This allows deleting lots of code, and additionally provides a huge reduction in the compressed size of CraftingDataPacket. Since we don't care about these UUIDs (they are only used in CraftingEventPacket, which is broken and unused in PM) we fill them with zeros instead. --- src/pocketmine/inventory/CraftingManager.php | 38 ++++--------------- src/pocketmine/inventory/CraftingRecipe.php | 12 ------ src/pocketmine/inventory/ShapedRecipe.php | 19 ---------- src/pocketmine/inventory/ShapelessRecipe.php | 22 ----------- .../mcpe/protocol/CraftingDataPacket.php | 4 +- 5 files changed, 9 insertions(+), 86 deletions(-) diff --git a/src/pocketmine/inventory/CraftingManager.php b/src/pocketmine/inventory/CraftingManager.php index 103a4504c..a7f59af5a 100644 --- a/src/pocketmine/inventory/CraftingManager.php +++ b/src/pocketmine/inventory/CraftingManager.php @@ -30,13 +30,8 @@ use pocketmine\network\mcpe\protocol\CraftingDataPacket; use pocketmine\Server; use pocketmine\timings\Timings; use pocketmine\utils\Config; -use pocketmine\utils\UUID; class CraftingManager{ - - /** @var CraftingRecipe[] */ - protected $recipes = []; - /** @var ShapedRecipe[][] */ protected $shapedRecipes = []; /** @var ShapelessRecipe[][] */ @@ -44,8 +39,6 @@ class CraftingManager{ /** @var FurnaceRecipe[] */ protected $furnaceRecipes = []; - private static $RECIPE_COUNT = 0; - /** @var BatchPacket */ private $craftingDataCache; @@ -93,13 +86,16 @@ class CraftingManager{ $pk = new CraftingDataPacket(); $pk->cleanRecipes = true; - foreach($this->recipes as $recipe){ - if($recipe instanceof ShapedRecipe){ - $pk->addShapedRecipe($recipe); - }elseif($recipe instanceof ShapelessRecipe){ + foreach($this->shapelessRecipes as $list){ + foreach($list as $recipe){ $pk->addShapelessRecipe($recipe); } } + foreach($this->shapedRecipes as $list){ + foreach($list as $recipe){ + $pk->addShapedRecipe($recipe); + } + } foreach($this->furnaceRecipes as $recipe){ $pk->addFurnaceRecipe($recipe); @@ -179,22 +175,6 @@ class CraftingManager{ return json_encode($outputs); } - /** - * @param UUID $id - * @return CraftingRecipe|null - */ - public function getRecipe(UUID $id) : ?CraftingRecipe{ - $index = $id->toBinary(); - return $this->recipes[$index] ?? null; - } - - /** - * @return Recipe[] - */ - public function getRecipes() : array{ - return $this->recipes; - } - /** * @return ShapelessRecipe[][] */ @@ -220,8 +200,6 @@ class CraftingManager{ * @param ShapedRecipe $recipe */ public function registerShapedRecipe(ShapedRecipe $recipe) : void{ - $recipe->setId($uuid = UUID::fromData((string) ++self::$RECIPE_COUNT, json_encode(self::pack($recipe->getResults())))); - $this->recipes[$uuid->toBinary()] = $recipe; $this->shapedRecipes[self::hashOutputs($recipe->getResults())][] = $recipe; $this->craftingDataCache = null; @@ -231,8 +209,6 @@ class CraftingManager{ * @param ShapelessRecipe $recipe */ public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{ - $recipe->setId($uuid = UUID::fromData((string) ++self::$RECIPE_COUNT, json_encode(self::pack($recipe->getResults())))); - $this->recipes[$uuid->toBinary()] = $recipe; $this->shapelessRecipes[self::hashOutputs($recipe->getResults())][] = $recipe; $this->craftingDataCache = null; diff --git a/src/pocketmine/inventory/CraftingRecipe.php b/src/pocketmine/inventory/CraftingRecipe.php index 0f184d958..df37ca6e6 100644 --- a/src/pocketmine/inventory/CraftingRecipe.php +++ b/src/pocketmine/inventory/CraftingRecipe.php @@ -24,20 +24,8 @@ declare(strict_types=1); namespace pocketmine\inventory; use pocketmine\item\Item; -use pocketmine\utils\UUID; interface CraftingRecipe extends Recipe{ - - /** - * @return UUID|null - */ - public function getId() : ?UUID; - - /** - * @param UUID $id - */ - public function setId(UUID $id); - /** * Returns a list of items needed to craft this recipe. This MUST NOT include Air items or items with a zero count. * diff --git a/src/pocketmine/inventory/ShapedRecipe.php b/src/pocketmine/inventory/ShapedRecipe.php index f1cbf372a..836f49648 100644 --- a/src/pocketmine/inventory/ShapedRecipe.php +++ b/src/pocketmine/inventory/ShapedRecipe.php @@ -25,12 +25,8 @@ namespace pocketmine\inventory; use pocketmine\item\Item; use pocketmine\item\ItemFactory; -use pocketmine\utils\UUID; class ShapedRecipe implements CraftingRecipe{ - /** @var UUID|null */ - private $id = null; - /** @var string[] */ private $shape = []; /** @var Item[] char => Item map */ @@ -116,21 +112,6 @@ class ShapedRecipe implements CraftingRecipe{ return $this->getResults(); } - /** - * @return UUID|null - */ - public function getId() : ?UUID{ - return $this->id; - } - - public function setId(UUID $id){ - if($this->id !== null){ - throw new \InvalidStateException("Id is already set"); - } - - $this->id = $id; - } - /** * @param string $key * @param Item $item diff --git a/src/pocketmine/inventory/ShapelessRecipe.php b/src/pocketmine/inventory/ShapelessRecipe.php index 3b3597d33..a8dbcb007 100644 --- a/src/pocketmine/inventory/ShapelessRecipe.php +++ b/src/pocketmine/inventory/ShapelessRecipe.php @@ -24,12 +24,8 @@ declare(strict_types=1); namespace pocketmine\inventory; use pocketmine\item\Item; -use pocketmine\utils\UUID; class ShapelessRecipe implements CraftingRecipe{ - /** @var UUID|null */ - private $id = null; - /** @var Item[] */ private $ingredients = []; /** @var Item[] */ @@ -48,24 +44,6 @@ class ShapelessRecipe implements CraftingRecipe{ $this->results = array_map(function(Item $item) : Item{ return clone $item; }, $results); } - /** - * @return UUID|null - */ - public function getId() : ?UUID{ - 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; - } - public function getResults() : array{ return array_map(function(Item $item) : Item{ return clone $item; }, $this->results); } diff --git a/src/pocketmine/network/mcpe/protocol/CraftingDataPacket.php b/src/pocketmine/network/mcpe/protocol/CraftingDataPacket.php index 15ec5013a..eba91dea2 100644 --- a/src/pocketmine/network/mcpe/protocol/CraftingDataPacket.php +++ b/src/pocketmine/network/mcpe/protocol/CraftingDataPacket.php @@ -139,7 +139,7 @@ class CraftingDataPacket extends DataPacket{ $stream->putSlot($item); } - $stream->putUUID($recipe->getId()); + $stream->put(str_repeat("\x00", 16)); //Null UUID return CraftingDataPacket::ENTRY_SHAPELESS; } @@ -160,7 +160,7 @@ class CraftingDataPacket extends DataPacket{ $stream->putSlot($item); } - $stream->putUUID($recipe->getId()); + $stream->put(str_repeat("\x00", 16)); //Null UUID return CraftingDataPacket::ENTRY_SHAPED; }