PocketMine-MP 5.43.3 git-4e2b0ce88fd28aa124b51efe324b94ceebb999fe
Loading...
Searching...
No Matches
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
17use pmmp\encoding\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\VarInt;
22use Ramsey\Uuid\UuidInterface;
23use function count;
24
25final class ShapedRecipe extends RecipeWithTypeId{
26 private string $blockName;
27
34 public function __construct(
35 int $typeId,
36 private string $recipeId,
37 private array $input,
38 private array $output,
39 private UuidInterface $uuid,
40 string $blockType, //TODO: rename this
41 private int $priority,
42 private bool $symmetric,
43 private RecipeUnlockingRequirement $unlockingRequirement,
44 private int $recipeNetId
45 ){
46 parent::__construct($typeId);
47 $rows = count($input);
48 if($rows < 1 or $rows > 3){
49 throw new \InvalidArgumentException("Expected 1, 2 or 3 input rows");
50 }
51 $columns = null;
52 foreach($input as $rowNumber => $row){
53 if($columns === null){
54 $columns = count($row);
55 }elseif(count($row) !== $columns){
56 throw new \InvalidArgumentException("Expected each row to be $columns columns, but have " . count($row) . " in row $rowNumber");
57 }
58 }
59 $this->blockName = $blockType;
60 }
61
62 public function getRecipeId() : string{
63 return $this->recipeId;
64 }
65
66 public function getWidth() : int{
67 return count($this->input[0]);
68 }
69
70 public function getHeight() : int{
71 return count($this->input);
72 }
73
78 public function getInput() : array{
79 return $this->input;
80 }
81
86 public function getOutput() : array{
87 return $this->output;
88 }
89
90 public function getUuid() : UuidInterface{
91 return $this->uuid;
92 }
93
94 public function getBlockName() : string{
95 return $this->blockName;
96 }
97
98 public function getPriority() : int{
99 return $this->priority;
100 }
101
102 public function isSymmetric() : bool{ return $this->symmetric; }
103
104 public function getUnlockingRequirement() : RecipeUnlockingRequirement{ return $this->unlockingRequirement; }
105
106 public function getRecipeNetId() : int{
107 return $this->recipeNetId;
108 }
109
110 public static function decode(int $recipeType, ByteBufferReader $in) : self{
111 $recipeId = CommonTypes::getString($in);
112 $width = VarInt::readSignedInt($in);
113 $height = VarInt::readSignedInt($in);
114 $input = [];
115 for($row = 0; $row < $height; ++$row){
116 for($column = 0; $column < $width; ++$column){
117 $input[$row][$column] = CommonTypes::getRecipeIngredient($in);
118 }
119 }
120
121 $output = [];
122 for($k = 0, $resultCount = VarInt::readUnsignedInt($in); $k < $resultCount; ++$k){
123 $output[] = CommonTypes::getItemStackWithoutStackId($in);
124 }
125 $uuid = CommonTypes::getUUID($in);
126 $block = CommonTypes::getString($in);
127 $priority = VarInt::readSignedInt($in);
128 $symmetric = CommonTypes::getBool($in);
129 $unlockingRequirement = RecipeUnlockingRequirement::read($in);
130
131 $recipeNetId = CommonTypes::readRecipeNetId($in);
132
133 return new self($recipeType, $recipeId, $input, $output, $uuid, $block, $priority, $symmetric, $unlockingRequirement, $recipeNetId);
134 }
135
136 public function encode(ByteBufferWriter $out) : void{
137 CommonTypes::putString($out, $this->recipeId);
138 VarInt::writeSignedInt($out, $this->getWidth());
139 VarInt::writeSignedInt($out, $this->getHeight());
140 foreach($this->input as $row){
141 foreach($row as $ingredient){
142 CommonTypes::putRecipeIngredient($out, $ingredient);
143 }
144 }
145
146 VarInt::writeUnsignedInt($out, count($this->output));
147 foreach($this->output as $item){
148 CommonTypes::putItemStackWithoutStackId($out, $item);
149 }
150
151 CommonTypes::putUUID($out, $this->uuid);
152 CommonTypes::putString($out, $this->blockName);
153 VarInt::writeSignedInt($out, $this->priority);
154 CommonTypes::putBool($out, $this->symmetric);
155 $this->unlockingRequirement->write($out);
156
157 CommonTypes::writeRecipeNetId($out, $this->recipeNetId);
158 }
159}
__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)