From 8093a94e5ddfe76f6a330d28d2b459faaf11e05b Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 28 Apr 2020 18:56:27 +0100 Subject: [PATCH] do not hardcode data deserialization into CraftingManager --- src/Server.php | 3 +- src/crafting/CraftingManager.php | 47 ------------ .../CraftingManagerFromDataHelper.php | 75 +++++++++++++++++++ 3 files changed, 77 insertions(+), 48 deletions(-) create mode 100644 src/crafting/CraftingManagerFromDataHelper.php diff --git a/src/Server.php b/src/Server.php index 71012b4e1..fa604d6d9 100644 --- a/src/Server.php +++ b/src/Server.php @@ -33,6 +33,7 @@ use pocketmine\command\ConsoleCommandSender; use pocketmine\command\PluginIdentifiableCommand; use pocketmine\command\SimpleCommandMap; use pocketmine\crafting\CraftingManager; +use pocketmine\crafting\CraftingManagerFromDataHelper; use pocketmine\event\HandlerListManager; use pocketmine\event\player\PlayerDataSaveEvent; use pocketmine\event\server\CommandEvent; @@ -984,7 +985,7 @@ class Server{ Enchantment::init(); Biome::init(); - $this->craftingManager = new CraftingManager(); + $this->craftingManager = CraftingManagerFromDataHelper::make(\pocketmine\RESOURCE_PATH . '/vanilla/recipes.json'); $this->resourceManager = new ResourcePackManager($this->getDataPath() . "resource_packs" . DIRECTORY_SEPARATOR, $this->logger); diff --git a/src/crafting/CraftingManager.php b/src/crafting/CraftingManager.php index c97dc10f0..fe51038e7 100644 --- a/src/crafting/CraftingManager.php +++ b/src/crafting/CraftingManager.php @@ -38,13 +38,10 @@ use pocketmine\timings\Timings; use pocketmine\utils\Binary; use pocketmine\utils\UUID; use function array_map; -use function file_get_contents; -use function json_decode; use function json_encode; use function spl_object_id; use function str_repeat; use function usort; -use const DIRECTORY_SEPARATOR; class CraftingManager{ /** @var ShapedRecipe[][] */ @@ -57,50 +54,6 @@ class CraftingManager{ /** @var CompressBatchPromise[] */ private $craftingDataCaches = []; - public function __construct(){ - $this->init(); - } - - public function init() : void{ - $recipes = json_decode(file_get_contents(\pocketmine\RESOURCE_PATH . "vanilla" . DIRECTORY_SEPARATOR . "recipes.json"), true); - - $itemDeserializerFunc = \Closure::fromCallable([Item::class, 'jsonDeserialize']); - foreach($recipes as $recipe){ - switch($recipe["type"]){ - case "shapeless": - if($recipe["block"] !== "crafting_table"){ //TODO: filter others out for now to avoid breaking economics - break; - } - $this->registerShapelessRecipe(new ShapelessRecipe( - array_map($itemDeserializerFunc, $recipe["input"]), - array_map($itemDeserializerFunc, $recipe["output"]) - )); - break; - case "shaped": - if($recipe["block"] !== "crafting_table"){ //TODO: filter others out for now to avoid breaking economics - break; - } - $this->registerShapedRecipe(new ShapedRecipe( - $recipe["shape"], - array_map($itemDeserializerFunc, $recipe["input"]), - array_map($itemDeserializerFunc, $recipe["output"]) - )); - break; - case "smelting": - if($recipe["block"] !== "furnace"){ //TODO: filter others out for now to avoid breaking economics - break; - } - $this->registerFurnaceRecipe(new FurnaceRecipe( - Item::jsonDeserialize($recipe["output"]), - Item::jsonDeserialize($recipe["input"])) - ); - break; - default: - break; - } - } - } - /** * Rebuilds the cached CraftingDataPacket. */ diff --git a/src/crafting/CraftingManagerFromDataHelper.php b/src/crafting/CraftingManagerFromDataHelper.php new file mode 100644 index 000000000..84ac260a3 --- /dev/null +++ b/src/crafting/CraftingManagerFromDataHelper.php @@ -0,0 +1,75 @@ +registerShapelessRecipe(new ShapelessRecipe( + array_map($itemDeserializerFunc, $recipe["input"]), + array_map($itemDeserializerFunc, $recipe["output"]) + )); + break; + case "shaped": + if($recipe["block"] !== "crafting_table"){ //TODO: filter others out for now to avoid breaking economics + break; + } + $result->registerShapedRecipe(new ShapedRecipe( + $recipe["shape"], + array_map($itemDeserializerFunc, $recipe["input"]), + array_map($itemDeserializerFunc, $recipe["output"]) + )); + break; + case "smelting": + if($recipe["block"] !== "furnace"){ //TODO: filter others out for now to avoid breaking economics + break; + } + $result->registerFurnaceRecipe(new FurnaceRecipe( + Item::jsonDeserialize($recipe["output"]), + Item::jsonDeserialize($recipe["input"])) + ); + break; + default: + break; + } + } + + return $result; + } +} \ No newline at end of file