Small cleanup of recipe UUID handling (furnace recipes don't need UUIDs)

This commit is contained in:
Dylan K. Taylor 2017-09-20 09:34:00 +01:00
parent ca23864e4c
commit fd33a65e3b
6 changed files with 59 additions and 53 deletions

View File

@ -34,10 +34,10 @@ use pocketmine\utils\UUID;
class CraftingManager{ class CraftingManager{
/** @var Recipe[] */ /** @var CraftingRecipe[] */
public $recipes = []; public $recipes = [];
/** @var Recipe[][] */ /** @var CraftingRecipe[][] */
protected $recipeLookup = []; protected $recipeLookup = [];
/** @var FurnaceRecipe[] */ /** @var FurnaceRecipe[] */
@ -148,7 +148,7 @@ class CraftingManager{
/** /**
* @param UUID $id * @param UUID $id
* @return Recipe|null * @return CraftingRecipe|null
*/ */
public function getRecipe(UUID $id){ public function getRecipe(UUID $id){
$index = $id->toBinary(); $index = $id->toBinary();
@ -183,7 +183,8 @@ class CraftingManager{
*/ */
public function registerShapedRecipe(ShapedRecipe $recipe){ public function registerShapedRecipe(ShapedRecipe $recipe){
$result = $recipe->getResult(); $result = $recipe->getResult();
$this->recipes[$recipe->getId()->toBinary()] = $recipe;
/** @var Item[][] $ingredients */
$ingredients = $recipe->getIngredientMap(); $ingredients = $recipe->getIngredientMap();
$hash = ""; $hash = "";
foreach($ingredients as $v){ foreach($ingredients as $v){
@ -206,7 +207,6 @@ class CraftingManager{
*/ */
public function registerShapelessRecipe(ShapelessRecipe $recipe){ public function registerShapelessRecipe(ShapelessRecipe $recipe){
$result = $recipe->getResult(); $result = $recipe->getResult();
$this->recipes[$recipe->getId()->toBinary()] = $recipe;
$hash = ""; $hash = "";
$ingredients = $recipe->getIngredientList(); $ingredients = $recipe->getIngredientList();
usort($ingredients, [$this, "sort"]); usort($ingredients, [$this, "sort"]);
@ -288,15 +288,13 @@ class CraftingManager{
* @param Recipe $recipe * @param Recipe $recipe
*/ */
public function registerRecipe(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 CraftingRecipe){
$result = $recipe->getResult();
if($recipe instanceof ShapedRecipe){ $recipe->setId($uuid = UUID::fromData((string) ++self::$RECIPE_COUNT, (string) $result->getId(), (string) $result->getDamage(), (string) $result->getCount(), $result->getCompoundTag()));
$this->registerShapedRecipe($recipe); $this->recipes[$uuid->toBinary()] = $recipe;
}elseif($recipe instanceof ShapelessRecipe){
$this->registerShapelessRecipe($recipe);
}elseif($recipe instanceof FurnaceRecipe){
$this->registerFurnaceRecipe($recipe);
} }
$recipe->registerToCraftingManager($this);
} }
} }

View File

@ -0,0 +1,39 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\inventory;
use pocketmine\utils\UUID;
interface CraftingRecipe extends Recipe{
/**
* @return UUID|null
*/
public function getId();
/**
* @param UUID $id
*/
public function setId(UUID $id);
}

View File

@ -29,9 +29,6 @@ use pocketmine\utils\UUID;
class FurnaceRecipe implements Recipe{ class FurnaceRecipe implements Recipe{
/** @var UUID|null */
private $id = null;
/** @var Item */ /** @var Item */
private $output; private $output;
@ -47,24 +44,6 @@ class FurnaceRecipe implements Recipe{
$this->ingredient = clone $ingredient; $this->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 * @param Item $item
*/ */
@ -86,7 +65,7 @@ class FurnaceRecipe implements Recipe{
return clone $this->output; return clone $this->output;
} }
public function registerToCraftingManager(){ public function registerToCraftingManager(CraftingManager $manager){
Server::getInstance()->getCraftingManager()->registerFurnaceRecipe($this); $manager->registerFurnaceRecipe($this);
} }
} }

View File

@ -33,15 +33,5 @@ interface Recipe{
*/ */
public function getResult() : Item; public function getResult() : Item;
public function registerToCraftingManager(); public function registerToCraftingManager(CraftingManager $manager);
/**
* @return UUID|null
*/
public function getId();
/**
* @param UUID $id
*/
public function setId(UUID $id);
} }

View File

@ -28,7 +28,7 @@ use pocketmine\item\ItemFactory;
use pocketmine\Server; use pocketmine\Server;
use pocketmine\utils\UUID; use pocketmine\utils\UUID;
class ShapedRecipe implements Recipe{ class ShapedRecipe implements CraftingRecipe{
/** @var Item */ /** @var Item */
private $output; private $output;
@ -168,7 +168,7 @@ class ShapedRecipe implements Recipe{
return $this->shape; return $this->shape;
} }
public function registerToCraftingManager(){ public function registerToCraftingManager(CraftingManager $manager){
Server::getInstance()->getCraftingManager()->registerShapedRecipe($this); $manager->registerShapedRecipe($this);
} }
} }

View File

@ -27,7 +27,7 @@ use pocketmine\item\Item;
use pocketmine\Server; use pocketmine\Server;
use pocketmine\utils\UUID; use pocketmine\utils\UUID;
class ShapelessRecipe implements Recipe{ class ShapelessRecipe implements CraftingRecipe{
/** @var Item */ /** @var Item */
private $output; private $output;
@ -129,7 +129,7 @@ class ShapelessRecipe implements Recipe{
return $count; return $count;
} }
public function registerToCraftingManager(){ public function registerToCraftingManager(CraftingManager $manager){
Server::getInstance()->getCraftingManager()->registerShapelessRecipe($this); $manager->registerShapelessRecipe($this);
} }
} }