PocketMine-MP 5.18.1 git-9381fc4172e5dce4cada1cb356050c8a2ab57b94
vendor/pocketmine/bedrock-protocol/src/types/recipe/ShapedRecipe.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol\types\recipe;
16
19use Ramsey\Uuid\UuidInterface;
20use function count;
21
22final class ShapedRecipe extends RecipeWithTypeId{
23 private string $blockName;
24
29 public function __construct(
30 int $typeId,
31 private string $recipeId,
32 private array $input,
33 private array $output,
34 private UuidInterface $uuid,
35 string $blockType, //TODO: rename this
36 private int $priority,
37 private bool $symmetric,
38 private RecipeUnlockingRequirement $unlockingRequirement,
39 private int $recipeNetId
40 ){
41 parent::__construct($typeId);
42 $rows = count($input);
43 if($rows < 1 or $rows > 3){
44 throw new \InvalidArgumentException("Expected 1, 2 or 3 input rows");
45 }
46 $columns = null;
47 foreach($input as $rowNumber => $row){
48 if($columns === null){
49 $columns = count($row);
50 }elseif(count($row) !== $columns){
51 throw new \InvalidArgumentException("Expected each row to be $columns columns, but have " . count($row) . " in row $rowNumber");
52 }
53 }
54 $this->blockName = $blockType;
55 }
56
57 public function getRecipeId() : string{
58 return $this->recipeId;
59 }
60
61 public function getWidth() : int{
62 return count($this->input[0]);
63 }
64
65 public function getHeight() : int{
66 return count($this->input);
67 }
68
72 public function getInput() : array{
73 return $this->input;
74 }
75
79 public function getOutput() : array{
80 return $this->output;
81 }
82
83 public function getUuid() : UuidInterface{
84 return $this->uuid;
85 }
86
87 public function getBlockName() : string{
88 return $this->blockName;
89 }
90
91 public function getPriority() : int{
92 return $this->priority;
93 }
94
95 public function isSymmetric() : bool{ return $this->symmetric; }
96
97 public function getUnlockingRequirement() : RecipeUnlockingRequirement{ return $this->unlockingRequirement; }
98
99 public function getRecipeNetId() : int{
100 return $this->recipeNetId;
101 }
102
103 public static function decode(int $recipeType, PacketSerializer $in) : self{
104 $recipeId = $in->getString();
105 $width = $in->getVarInt();
106 $height = $in->getVarInt();
107 $input = [];
108 for($row = 0; $row < $height; ++$row){
109 for($column = 0; $column < $width; ++$column){
110 $input[$row][$column] = $in->getRecipeIngredient();
111 }
112 }
113
114 $output = [];
115 for($k = 0, $resultCount = $in->getUnsignedVarInt(); $k < $resultCount; ++$k){
116 $output[] = $in->getItemStackWithoutStackId();
117 }
118 $uuid = $in->getUUID();
119 $block = $in->getString();
120 $priority = $in->getVarInt();
121 $symmetric = $in->getBool();
122 $unlockingRequirement = RecipeUnlockingRequirement::read($in);
123
124 $recipeNetId = $in->readRecipeNetId();
125
126 return new self($recipeType, $recipeId, $input, $output, $uuid, $block, $priority, $symmetric, $unlockingRequirement, $recipeNetId);
127 }
128
129 public function encode(PacketSerializer $out) : void{
130 $out->putString($this->recipeId);
131 $out->putVarInt($this->getWidth());
132 $out->putVarInt($this->getHeight());
133 foreach($this->input as $row){
134 foreach($row as $ingredient){
135 $out->putRecipeIngredient($ingredient);
136 }
137 }
138
139 $out->putUnsignedVarInt(count($this->output));
140 foreach($this->output as $item){
141 $out->putItemStackWithoutStackId($item);
142 }
143
144 $out->putUUID($this->uuid);
145 $out->putString($this->blockName);
146 $out->putVarInt($this->priority);
147 $out->putBool($this->symmetric);
148 $this->unlockingRequirement->write($out);
149
150 $out->writeRecipeNetId($this->recipeNetId);
151 }
152}
__construct(int $typeId, private string $recipeId, private array $input, private array $output, private UuidInterface $uuid, string $blockType, private int $priority, private bool $symmetric, private RecipeUnlockingRequirement $unlockingRequirement, private int $recipeNetId)