PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
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
29use function count;
30
32 public const NETWORK_ID = ProtocolInfo::CRAFTING_DATA_PACKET;
33
34 public const ENTRY_SHAPELESS = 0;
35 public const ENTRY_SHAPED = 1;
36 public const ENTRY_FURNACE = 2;
37 public const ENTRY_FURNACE_DATA = 3;
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(PacketSerializer $in) : void{
73 $recipeCount = $in->getUnsignedVarInt();
74 $previousType = "none";
75 for($i = 0; $i < $recipeCount; ++$i){
76 $recipeType = $in->getVarInt();
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_FURNACE, self::ENTRY_FURNACE_DATA => FurnaceRecipe::decode($recipeType, $in),
82 self::ENTRY_MULTI => MultiRecipe::decode($recipeType, $in),
83 self::ENTRY_SMITHING_TRANSFORM => SmithingTransformRecipe::decode($recipeType, $in),
84 self::ENTRY_SMITHING_TRIM => SmithingTrimRecipe::decode($recipeType, $in),
85 default => throw new PacketDecodeException("Unhandled recipe type $recipeType (previous was $previousType)"),
86 };
87 $previousType = $recipeType;
88 }
89 for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){
90 $inputId = $in->getVarInt();
91 $inputMeta = $in->getVarInt();
92 $ingredientId = $in->getVarInt();
93 $ingredientMeta = $in->getVarInt();
94 $outputId = $in->getVarInt();
95 $outputMeta = $in->getVarInt();
96 $this->potionTypeRecipes[] = new PotionTypeRecipe($inputId, $inputMeta, $ingredientId, $ingredientMeta, $outputId, $outputMeta);
97 }
98 for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){
99 $input = $in->getVarInt();
100 $ingredient = $in->getVarInt();
101 $output = $in->getVarInt();
102 $this->potionContainerRecipes[] = new PotionContainerChangeRecipe($input, $ingredient, $output);
103 }
104 for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){
105 $inputIdAndData = $in->getVarInt();
106 [$inputId, $inputMeta] = [$inputIdAndData >> 16, $inputIdAndData & 0x7fff];
107 $outputs = [];
108 for($j = 0, $outputCount = $in->getUnsignedVarInt(); $j < $outputCount; ++$j){
109 $outputItemId = $in->getVarInt();
110 $outputItemCount = $in->getVarInt();
111 $outputs[] = new MaterialReducerRecipeOutput($outputItemId, $outputItemCount);
112 }
113 $this->materialReducerRecipes[] = new MaterialReducerRecipe($inputId, $inputMeta, $outputs);
114 }
115 $this->cleanRecipes = $in->getBool();
116 }
117
118 protected function encodePayload(PacketSerializer $out) : void{
119 $out->putUnsignedVarInt(count($this->recipesWithTypeIds));
120 foreach($this->recipesWithTypeIds as $d){
121 $out->putVarInt($d->getTypeId());
122 $d->encode($out);
123 }
124 $out->putUnsignedVarInt(count($this->potionTypeRecipes));
125 foreach($this->potionTypeRecipes as $recipe){
126 $out->putVarInt($recipe->getInputItemId());
127 $out->putVarInt($recipe->getInputItemMeta());
128 $out->putVarInt($recipe->getIngredientItemId());
129 $out->putVarInt($recipe->getIngredientItemMeta());
130 $out->putVarInt($recipe->getOutputItemId());
131 $out->putVarInt($recipe->getOutputItemMeta());
132 }
133 $out->putUnsignedVarInt(count($this->potionContainerRecipes));
134 foreach($this->potionContainerRecipes as $recipe){
135 $out->putVarInt($recipe->getInputItemId());
136 $out->putVarInt($recipe->getIngredientItemId());
137 $out->putVarInt($recipe->getOutputItemId());
138 }
139 $out->putUnsignedVarInt(count($this->materialReducerRecipes));
140 foreach($this->materialReducerRecipes as $recipe){
141 $out->putVarInt(($recipe->getInputItemId() << 16) | $recipe->getInputItemMeta());
142 $out->putUnsignedVarInt(count($recipe->getOutputs()));
143 foreach($recipe->getOutputs() as $output){
144 $out->putVarInt($output->getItemId());
145 $out->putVarInt($output->getCount());
146 }
147 }
148 $out->putBool($this->cleanRecipes);
149 }
150
151 public function handle(PacketHandlerInterface $handler) : bool{
152 return $handler->handleCraftingData($this);
153 }
154}
static create(array $recipesWithTypeIds, array $potionTypeRecipes, array $potionContainerRecipes, array $materialReducerRecipes, bool $cleanRecipes)