3){ throw new \InvalidStateException("Crafting rows should be 1, 2, 3 wide, not $width"); } $this->ingredients[] = array_fill(0, $width, null); } $this->output = clone $result; } public function getWidth() : int{ return count($this->ingredients[0]); } public function getHeight() : int{ return count($this->ingredients); } /** * @return Item */ public function getResult() : Item{ return $this->output; } /** * @return UUID|null */ public function getId(){ return $this->id; } public function setId(UUID $id){ if($this->id !== null){ throw new \InvalidStateException("Id is already set"); } $this->id = $id; } /** * @param int $x * @param int $y * @param Item $item * * @return $this */ public function addIngredient(int $x, int $y, Item $item){ $this->ingredients[$y][$x] = clone $item; return $this; } /** * @param string $key * @param Item $item * * @return $this * @throws \Exception */ public function setIngredient(string $key, Item $item){ if(!array_key_exists($key, $this->shape)){ throw new \Exception("Symbol does not appear in the shape: " . $key); } $this->fixRecipe($key, $item); return $this; } /** * @param string $key * @param Item $item */ protected function fixRecipe(string $key, Item $item){ foreach($this->shapeItems[$key] as $entry){ $this->ingredients[$entry->y][$entry->x] = clone $item; } } /** * @return Item[][] */ public function getIngredientMap() : array{ $ingredients = []; foreach($this->ingredients as $y => $row){ $ingredients[$y] = []; foreach($row as $x => $ingredient){ if($ingredient !== null){ $ingredients[$y][$x] = clone $ingredient; }else{ $ingredients[$y][$x] = Item::get(Item::AIR); } } } return $ingredients; } /** * @param int $x * @param int $y * * @return Item */ public function getIngredient(int $x, int $y) : Item{ return $this->ingredients[$y][$x] ?? Item::get(Item::AIR); } /** * @return string[] */ public function getShape() : array{ return $this->shape; } public function registerToCraftingManager(){ Server::getInstance()->getCraftingManager()->registerShapedRecipe($this); } }