PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
EnchantOption.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;
16
17use pmmp\encoding\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\LE;
20use pmmp\encoding\VarInt;
22use function count;
23
24final class EnchantOption{
30 public function __construct(
31 private int $cost,
32 private int $slotFlags,
33 private array $equipActivatedEnchantments,
34 private array $heldActivatedEnchantments,
35 private array $selfActivatedEnchantments,
36 private string $name,
37 private int $optionId
38 ){}
39
40 public function getCost() : int{ return $this->cost; }
41
42 public function getSlotFlags() : int{ return $this->slotFlags; }
43
45 public function getEquipActivatedEnchantments() : array{ return $this->equipActivatedEnchantments; }
46
48 public function getHeldActivatedEnchantments() : array{ return $this->heldActivatedEnchantments; }
49
51 public function getSelfActivatedEnchantments() : array{ return $this->selfActivatedEnchantments; }
52
53 public function getName() : string{ return $this->name; }
54
55 public function getOptionId() : int{ return $this->optionId; }
56
60 private static function readEnchantList(ByteBufferReader $in) : array{
61 $result = [];
62 for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){
63 $result[] = Enchant::read($in);
64 }
65 return $result;
66 }
67
71 private static function writeEnchantList(ByteBufferWriter $out, array $list) : void{
72 VarInt::writeUnsignedInt($out, count($list));
73 foreach($list as $item){
74 $item->write($out);
75 }
76 }
77
78 public static function read(ByteBufferReader $in) : self{
79 $cost = VarInt::readUnsignedInt($in);
80
81 $slotFlags = LE::readUnsignedInt($in);
82 $equipActivatedEnchants = self::readEnchantList($in);
83 $heldActivatedEnchants = self::readEnchantList($in);
84 $selfActivatedEnchants = self::readEnchantList($in);
85
86 $name = CommonTypes::getString($in);
87 $optionId = CommonTypes::readRecipeNetId($in);
88
89 return new self($cost, $slotFlags, $equipActivatedEnchants, $heldActivatedEnchants, $selfActivatedEnchants, $name, $optionId);
90 }
91
92 public function write(ByteBufferWriter $out) : void{
93 VarInt::writeUnsignedInt($out, $this->cost);
94
95 LE::writeUnsignedInt($out, $this->slotFlags);
96 self::writeEnchantList($out, $this->equipActivatedEnchantments);
97 self::writeEnchantList($out, $this->heldActivatedEnchantments);
98 self::writeEnchantList($out, $this->selfActivatedEnchantments);
99
100 CommonTypes::putString($out, $this->name);
101 CommonTypes::writeRecipeNetId($out, $this->optionId);
102 }
103}
__construct(private int $cost, private int $slotFlags, private array $equipActivatedEnchantments, private array $heldActivatedEnchantments, private array $selfActivatedEnchantments, private string $name, private int $optionId)