mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-13 15:05:33 +00:00
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:
parent
4a1fc1bdf7
commit
7996a7b08c
@ -66,13 +66,14 @@ class CraftingManager{
|
|||||||
$this->registerRecipe($result);
|
$this->registerRecipe($result);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
// TODO: handle multiple result items
|
$outputs = $recipe["output"];
|
||||||
$first = $recipe["output"][0];
|
$first = array_shift($outputs);
|
||||||
|
|
||||||
$this->registerRecipe(new ShapedRecipe(
|
$this->registerRecipe(new ShapedRecipe(
|
||||||
Item::jsonDeserialize($first),
|
Item::jsonDeserialize($first),
|
||||||
$recipe["shape"],
|
$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;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\inventory;
|
namespace pocketmine\inventory;
|
||||||
|
|
||||||
|
use pocketmine\item\Item;
|
||||||
use pocketmine\utils\UUID;
|
use pocketmine\utils\UUID;
|
||||||
|
|
||||||
interface CraftingRecipe extends Recipe{
|
interface CraftingRecipe extends Recipe{
|
||||||
@ -38,4 +39,14 @@ interface CraftingRecipe extends Recipe{
|
|||||||
public function setId(UUID $id);
|
public function setId(UUID $id);
|
||||||
|
|
||||||
public function requiresCraftingTable() : bool;
|
public function requiresCraftingTable() : bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Item[]
|
||||||
|
*/
|
||||||
|
public function getExtraResults() : array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Item[]
|
||||||
|
*/
|
||||||
|
public function getAllResults() : array;
|
||||||
}
|
}
|
@ -30,7 +30,9 @@ use pocketmine\utils\UUID;
|
|||||||
|
|
||||||
class ShapedRecipe implements CraftingRecipe{
|
class ShapedRecipe implements CraftingRecipe{
|
||||||
/** @var Item */
|
/** @var Item */
|
||||||
private $output;
|
private $primaryResult;
|
||||||
|
/** @var Item[] */
|
||||||
|
private $extraResults = [];
|
||||||
|
|
||||||
/** @var UUID|null */
|
/** @var UUID|null */
|
||||||
private $id = null;
|
private $id = null;
|
||||||
@ -43,7 +45,7 @@ class ShapedRecipe implements CraftingRecipe{
|
|||||||
/**
|
/**
|
||||||
* Constructs a ShapedRecipe instance.
|
* Constructs a ShapedRecipe instance.
|
||||||
*
|
*
|
||||||
* @param Item $result
|
* @param Item $primaryResult
|
||||||
* @param string[] $shape<br>
|
* @param string[] $shape<br>
|
||||||
* Array of 1, 2, or 3 strings representing the rows of the recipe.
|
* 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
|
* 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.
|
* 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
|
* 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.
|
* 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.
|
* 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);
|
$rowCount = count($shape);
|
||||||
if($rowCount > 3 or $rowCount <= 0){
|
if($rowCount > 3 or $rowCount <= 0){
|
||||||
throw new \InvalidArgumentException("Shaped recipes may only have 1, 2 or 3 rows, not $rowCount");
|
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;
|
$this->shape = $shape;
|
||||||
|
|
||||||
foreach($ingredients as $char => $i){
|
foreach($ingredients as $char => $i){
|
||||||
@ -99,7 +108,23 @@ class ShapedRecipe implements CraftingRecipe{
|
|||||||
* @return Item
|
* @return Item
|
||||||
*/
|
*/
|
||||||
public function getResult() : 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -63,6 +63,14 @@ class ShapelessRecipe implements CraftingRecipe{
|
|||||||
return clone $this->output;
|
return clone $this->output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getExtraResults() : array{
|
||||||
|
return []; //TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAllResults() : array{
|
||||||
|
return [$this->getResult()]; //TODO
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Item $item
|
* @param Item $item
|
||||||
*
|
*
|
||||||
|
@ -133,8 +133,11 @@ class CraftingDataPacket extends DataPacket{
|
|||||||
$stream->putSlot($item);
|
$stream->putSlot($item);
|
||||||
}
|
}
|
||||||
|
|
||||||
$stream->putUnsignedVarInt(1);
|
$results = $recipe->getAllResults();
|
||||||
$stream->putSlot($recipe->getResult());
|
$stream->putUnsignedVarInt(count($results));
|
||||||
|
foreach($results as $item){
|
||||||
|
$stream->putSlot($item);
|
||||||
|
}
|
||||||
|
|
||||||
$stream->putUUID($recipe->getId());
|
$stream->putUUID($recipe->getId());
|
||||||
|
|
||||||
@ -151,8 +154,11 @@ class CraftingDataPacket extends DataPacket{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$stream->putUnsignedVarInt(1);
|
$results = $recipe->getAllResults();
|
||||||
$stream->putSlot($recipe->getResult());
|
$stream->putUnsignedVarInt(count($results));
|
||||||
|
foreach($results as $item){
|
||||||
|
$stream->putSlot($item);
|
||||||
|
}
|
||||||
|
|
||||||
$stream->putUUID($recipe->getId());
|
$stream->putUUID($recipe->getId());
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user