PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
UnlockedRecipesPacket.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\LE;
20use pmmp\encoding\VarInt;
22use function count;
23
25 public const NETWORK_ID = ProtocolInfo::UNLOCKED_RECIPES_PACKET;
26
27 public const TYPE_EMPTY = 0;
28 public const TYPE_INITIALLY_UNLOCKED = 1;
29 public const TYPE_NEWLY_UNLOCKED = 2;
30 public const TYPE_REMOVE = 3;
31 public const TYPE_REMOVE_ALL = 4;
32
33 private int $type;
35 private array $recipes;
36
41 public static function create(int $type, array $recipes) : self{
42 $result = new self;
43 $result->type = $type;
44 $result->recipes = $recipes;
45 return $result;
46 }
47
48 public function getType() : int{ return $this->type; }
49
53 public function getRecipes() : array{ return $this->recipes; }
54
55 protected function decodePayload(ByteBufferReader $in) : void{
56 $this->type = LE::readUnsignedInt($in);
57 $this->recipes = [];
58 for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; $i++){
59 $this->recipes[] = CommonTypes::getString($in);
60 }
61 }
62
63 protected function encodePayload(ByteBufferWriter $out) : void{
64 LE::writeUnsignedInt($out, $this->type);
65 VarInt::writeUnsignedInt($out, count($this->recipes));
66 foreach($this->recipes as $recipe){
67 CommonTypes::putString($out, $recipe);
68 }
69 }
70
71 public function handle(PacketHandlerInterface $handler) : bool{
72 return $handler->handleUnlockedRecipes($this);
73 }
74}