Testing handling multiple result items for ShapedRecipes

this doesn't work yet, I wanted to see how glitchy it is with cakes. The answer is: very glitchy.
This commit is contained in:
Dylan K. Taylor 2017-09-20 11:14:09 +01:00
parent 4a1fc1bdf7
commit 7996a7b08c
5 changed files with 63 additions and 12 deletions

View File

@ -66,13 +66,14 @@ class CraftingManager{
$this->registerRecipe($result);
break;
case 1:
// TODO: handle multiple result items
$first = $recipe["output"][0];
$outputs = $recipe["output"];
$first = array_shift($outputs);
$this->registerRecipe(new ShapedRecipe(
Item::jsonDeserialize($first),
$recipe["shape"],
array_map(function(array $data) : Item{ return Item::jsonDeserialize($data); }, $recipe["input"])
array_map(function(array $data) : Item{ return Item::jsonDeserialize($data); }, $recipe["input"]),
array_map(function(array $data) : Item{ return Item::jsonDeserialize($data); }, $recipe["output"])
));
break;
case 2:

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\inventory;
use pocketmine\item\Item;
use pocketmine\utils\UUID;
interface CraftingRecipe extends Recipe{
@ -38,4 +39,14 @@ interface CraftingRecipe extends Recipe{
public function setId(UUID $id);
public function requiresCraftingTable() : bool;
/**
* @return Item[]
*/
public function getExtraResults() : array;
/**
* @return Item[]
*/
public function getAllResults() : array;
}

View File

@ -30,7 +30,9 @@ use pocketmine\utils\UUID;
class ShapedRecipe implements CraftingRecipe{
/** @var Item */
private $output;
private $primaryResult;
/** @var Item[] */
private $extraResults = [];
/** @var UUID|null */
private $id = null;
@ -43,7 +45,7 @@ class ShapedRecipe implements CraftingRecipe{
/**
* Constructs a ShapedRecipe instance.
*
* @param Item $result
* @param Item $primaryResult
* @param string[] $shape<br>
* Array of 1, 2, or 3 strings representing the rows of the recipe.
* This accepts an array of 1, 2 or 3 strings. Each string should be of the same length and must be at most 3
@ -52,10 +54,13 @@ class ShapedRecipe implements CraftingRecipe{
* Char => Item map of items to be set into the shape.
* This accepts an array of Items, indexed by character. Every unique character (except space) in the shape
* array MUST have a corresponding item in this list. Space character is automatically treated as air.
* @param Item[] $extraResults<br>
* List of additional result items to leave in the crafting grid afterwards. Used for things like cake recipe
* empty buckets.
*
* Note: Recipes **do not** need to be square. Do NOT add padding for empty rows/columns.
*/
public function __construct(Item $result, array $shape, array $ingredients){
public function __construct(Item $primaryResult, array $shape, array $ingredients, array $extraResults = []){
$rowCount = count($shape);
if($rowCount > 3 or $rowCount <= 0){
throw new \InvalidArgumentException("Shaped recipes may only have 1, 2 or 3 rows, not $rowCount");
@ -79,7 +84,11 @@ class ShapedRecipe implements CraftingRecipe{
}
}
}
$this->output = clone $result;
$this->primaryResult = clone $primaryResult;
foreach($extraResults as $item){
$this->extraResults[] = clone $item;
}
$this->shape = $shape;
foreach($ingredients as $char => $i){
@ -99,7 +108,23 @@ class ShapedRecipe implements CraftingRecipe{
* @return Item
*/
public function getResult() : Item{
return $this->output;
return $this->primaryResult;
}
/**
* @return Item[]
*/
public function getExtraResults() : array{
return $this->extraResults;
}
/**
* @return Item[]
*/
public function getAllResults() : array{
$results = $this->extraResults;
array_unshift($results, $this->primaryResult);
return $results;
}
/**

View File

@ -63,6 +63,14 @@ class ShapelessRecipe implements CraftingRecipe{
return clone $this->output;
}
public function getExtraResults() : array{
return []; //TODO
}
public function getAllResults() : array{
return [$this->getResult()]; //TODO
}
/**
* @param Item $item
*

View File

@ -133,8 +133,11 @@ class CraftingDataPacket extends DataPacket{
$stream->putSlot($item);
}
$stream->putUnsignedVarInt(1);
$stream->putSlot($recipe->getResult());
$results = $recipe->getAllResults();
$stream->putUnsignedVarInt(count($results));
foreach($results as $item){
$stream->putSlot($item);
}
$stream->putUUID($recipe->getId());
@ -151,8 +154,11 @@ class CraftingDataPacket extends DataPacket{
}
}
$stream->putUnsignedVarInt(1);
$stream->putSlot($recipe->getResult());
$results = $recipe->getAllResults();
$stream->putUnsignedVarInt(count($results));
foreach($results as $item){
$stream->putSlot($item);
}
$stream->putUUID($recipe->getId());