Crafting: nuke

This commit brings in a much-needed rewrite of crafting transaction handling.

The following classes have been removed:
- CraftingTransferMaterialAction
- CraftingTakeResultAction

The following classes have significant changes:
- CraftingTransaction
	- All API methods have been removed and are now handled in CraftItemEvent
- CraftItemEvent
	- added the following:
		- getInputs()
		- getOutputs()
		- getRepetitions() (tells how many times a recipe was crafted in this event)
- Recipe interface:
	- Removed getResult() (individual recipes may handle this differently)
- CraftingRecipe interface
	- removed the following:
		- matchItems()
		- getExtraResults()
		- getAllResults()
	- added the following
		- getResults()
		- getIngredientList() : Item[], which must return a 1D array of items that should be consumed (wildcards accepted).
		- matchesCraftingGrid(CraftingGrid)
- ShapedRecipe
	- constructor now accepts string[], Item[], Item[]
- ShapelessRecipe
	- constructor now accepts Item[], Item[]
This commit is contained in:
Dylan K. Taylor
2018-03-26 13:23:28 +01:00
parent bc836aaec1
commit 8572e9e560
14 changed files with 437 additions and 521 deletions

View File

@ -23,6 +23,8 @@ declare(strict_types=1);
namespace pocketmine\inventory;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\Player;
class CraftingGrid extends BaseInventory{
@ -34,6 +36,15 @@ class CraftingGrid extends BaseInventory{
/** @var int */
private $gridWidth;
/** @var int|null */
private $startX;
/** @var int|null */
private $xLen;
/** @var int|null */
private $startY;
/** @var int|null */
private $yLen;
public function __construct(Player $holder, int $gridWidth){
$this->holder = $holder;
$this->gridWidth = $gridWidth;
@ -56,6 +67,16 @@ class CraftingGrid extends BaseInventory{
return "Crafting";
}
public function setItem(int $index, Item $item, bool $send = true) : bool{
if(parent::setItem($index, $item, $send)){
$this->seekRecipeBounds();
return true;
}
return false;
}
public function sendSlot(int $index, $target) : void{
//we can't send a slot of a client-sided inventory window
}
@ -70,4 +91,70 @@ class CraftingGrid extends BaseInventory{
public function getHolder(){
return $this->holder;
}
private function seekRecipeBounds() : void{
$minX = PHP_INT_MAX;
$maxX = 0;
$minY = PHP_INT_MAX;
$maxY = 0;
$empty = true;
for($y = 0; $y < $this->gridWidth; ++$y){
for($x = 0; $x < $this->gridWidth; ++$x){
if(!$this->isSlotEmpty($y * $this->gridWidth + $x)){
$minX = min($minX, $x);
$maxX = max($maxX, $x);
$minY = min($minY, $y);
$maxY = max($maxY, $y);
$empty = false;
}
}
}
if(!$empty){
$this->startX = $minX;
$this->xLen = $maxX - $minX + 1;
$this->startY = $minY;
$this->yLen = $maxY - $minY + 1;
}else{
$this->startX = $this->xLen = $this->startY = $this->yLen = null;
}
}
/**
* Returns the item at offset x,y, offset by where the starts of the recipe rectangle are.
*
* @param int $x
* @param int $y
*
* @return Item
*/
public function getIngredient(int $x, int $y) : Item{
if($this->startX !== null and $this->startY !== null){
return $this->getItem(($y + $this->startY) * $this->gridWidth + ($x + $this->startX));
}
throw new \InvalidStateException("No ingredients found in grid");
}
/**
* Returns the width of the recipe we're trying to craft, based on items currently in the grid.
*
* @return int
*/
public function getRecipeWidth() : int{
return $this->xLen ?? 0;
}
/**
* Returns the height of the recipe we're trying to craft, based on items currently in the grid.
* @return int
*/
public function getRecipeHeight() : int{
return $this->yLen ?? 0;
}
}