PocketMine-MP 5.43.2 git-a137a986d01d9af23452b2e741699a770c7ae112
Loading...
Searching...
No Matches
CraftingDataPacket.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;
16
17use pmmp\encoding\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\VarInt;
31use function count;
32
34 public const NETWORK_ID = ProtocolInfo::CRAFTING_DATA_PACKET;
35
36 public const ENTRY_SHAPELESS = 0;
37 public const ENTRY_SHAPED = 1;
38 public const ENTRY_MULTI = 4;
39 public const ENTRY_USER_DATA_SHAPELESS = 5;
40 public const ENTRY_SHAPELESS_CHEMISTRY = 6;
41 public const ENTRY_SHAPED_CHEMISTRY = 7;
42 public const ENTRY_SMITHING_TRANSFORM = 8;
43 public const ENTRY_SMITHING_TRIM = 9;
44
46 public array $recipesWithTypeIds = [];
48 public array $potionTypeRecipes = [];
50 public array $potionContainerRecipes = [];
52 public array $materialReducerRecipes = [];
53 public bool $cleanRecipes = false;
54
62 public static function create(array $recipesWithTypeIds, array $potionTypeRecipes, array $potionContainerRecipes, array $materialReducerRecipes, bool $cleanRecipes) : self{
63 $result = new self;
64 $result->recipesWithTypeIds = $recipesWithTypeIds;
65 $result->potionTypeRecipes = $potionTypeRecipes;
66 $result->potionContainerRecipes = $potionContainerRecipes;
67 $result->materialReducerRecipes = $materialReducerRecipes;
68 $result->cleanRecipes = $cleanRecipes;
69 return $result;
70 }
71
72 protected function decodePayload(ByteBufferReader $in) : void{
73 $recipeCount = VarInt::readUnsignedInt($in);
74 $previousType = "none";
75 for($i = 0; $i < $recipeCount; ++$i){
76 $recipeType = VarInt::readSignedInt($in);
77
78 $this->recipesWithTypeIds[] = match($recipeType){
79 self::ENTRY_SHAPELESS, self::ENTRY_USER_DATA_SHAPELESS, self::ENTRY_SHAPELESS_CHEMISTRY => ShapelessRecipe::decode($recipeType, $in),
80 self::ENTRY_SHAPED, self::ENTRY_SHAPED_CHEMISTRY => ShapedRecipe::decode($recipeType, $in),
81 self::ENTRY_MULTI => MultiRecipe::decode($recipeType, $in),
82 self::ENTRY_SMITHING_TRANSFORM => SmithingTransformRecipe::decode($recipeType, $in),
83 self::ENTRY_SMITHING_TRIM => SmithingTrimRecipe::decode($recipeType, $in),
84 default => throw new PacketDecodeException("Unhandled recipe type $recipeType (previous was $previousType)"),
85 };
86 $previousType = $recipeType;
87 }
88 for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){
89 $inputId = VarInt::readSignedInt($in);
90 $inputMeta = VarInt::readSignedInt($in);
91 $ingredientId = VarInt::readSignedInt($in);
92 $ingredientMeta = VarInt::readSignedInt($in);
93 $outputId = VarInt::readSignedInt($in);
94 $outputMeta = VarInt::readSignedInt($in);
95 $this->potionTypeRecipes[] = new PotionTypeRecipe($inputId, $inputMeta, $ingredientId, $ingredientMeta, $outputId, $outputMeta);
96 }
97 for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){
98 $input = VarInt::readSignedInt($in);
99 $ingredient = VarInt::readSignedInt($in);
100 $output = VarInt::readSignedInt($in);
101 $this->potionContainerRecipes[] = new PotionContainerChangeRecipe($input, $ingredient, $output);
102 }
103 for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){
104 $inputIdAndData = VarInt::readSignedInt($in);
105 [$inputId, $inputMeta] = [$inputIdAndData >> 16, $inputIdAndData & 0x7fff];
106 $outputs = [];
107 for($j = 0, $outputCount = VarInt::readUnsignedInt($in); $j < $outputCount; ++$j){
108 $outputItemId = VarInt::readSignedInt($in);
109 $outputItemCount = VarInt::readSignedInt($in);
110 $outputs[] = new MaterialReducerRecipeOutput($outputItemId, $outputItemCount);
111 }
112 $this->materialReducerRecipes[] = new MaterialReducerRecipe($inputId, $inputMeta, $outputs);
113 }
114 $this->cleanRecipes = CommonTypes::getBool($in);
115 }
116
117 protected function encodePayload(ByteBufferWriter $out) : void{
118 VarInt::writeUnsignedInt($out, count($this->recipesWithTypeIds));
119 foreach($this->recipesWithTypeIds as $d){
120 VarInt::writeSignedInt($out, $d->getTypeId());
121 $d->encode($out);
122 }
123 VarInt::writeUnsignedInt($out, count($this->potionTypeRecipes));
124 foreach($this->potionTypeRecipes as $recipe){
125 VarInt::writeSignedInt($out, $recipe->getInputItemId());
126 VarInt::writeSignedInt($out, $recipe->getInputItemMeta());
127 VarInt::writeSignedInt($out, $recipe->getIngredientItemId());
128 VarInt::writeSignedInt($out, $recipe->getIngredientItemMeta());
129 VarInt::writeSignedInt($out, $recipe->getOutputItemId());
130 VarInt::writeSignedInt($out, $recipe->getOutputItemMeta());
131 }
132 VarInt::writeUnsignedInt($out, count($this->potionContainerRecipes));
133 foreach($this->potionContainerRecipes as $recipe){
134 VarInt::writeSignedInt($out, $recipe->getInputItemId());
135 VarInt::writeSignedInt($out, $recipe->getIngredientItemId());
136 VarInt::writeSignedInt($out, $recipe->getOutputItemId());
137 }
138 VarInt::writeUnsignedInt($out, count($this->materialReducerRecipes));
139 foreach($this->materialReducerRecipes as $recipe){
140 VarInt::writeSignedInt($out, ($recipe->getInputItemId() << 16) | $recipe->getInputItemMeta());
141 VarInt::writeUnsignedInt($out, count($recipe->getOutputs()));
142 foreach($recipe->getOutputs() as $output){
143 VarInt::writeSignedInt($out, $output->getItemId());
144 VarInt::writeSignedInt($out, $output->getCount());
145 }
146 }
147 CommonTypes::putBool($out, $this->cleanRecipes);
148 }
149
150 public function handle(PacketHandlerInterface $handler) : bool{
151 return $handler->handleCraftingData($this);
152 }
153}
static create(array $recipesWithTypeIds, array $potionTypeRecipes, array $potionContainerRecipes, array $materialReducerRecipes, bool $cleanRecipes)