PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
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
18use function count;
19
20final class EnchantOption{
26 public function __construct(
27 private int $cost,
28 private int $slotFlags,
29 private array $equipActivatedEnchantments,
30 private array $heldActivatedEnchantments,
31 private array $selfActivatedEnchantments,
32 private string $name,
33 private int $optionId
34 ){}
35
36 public function getCost() : int{ return $this->cost; }
37
38 public function getSlotFlags() : int{ return $this->slotFlags; }
39
41 public function getEquipActivatedEnchantments() : array{ return $this->equipActivatedEnchantments; }
42
44 public function getHeldActivatedEnchantments() : array{ return $this->heldActivatedEnchantments; }
45
47 public function getSelfActivatedEnchantments() : array{ return $this->selfActivatedEnchantments; }
48
49 public function getName() : string{ return $this->name; }
50
51 public function getOptionId() : int{ return $this->optionId; }
52
56 private static function readEnchantList(PacketSerializer $in) : array{
57 $result = [];
58 for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){
59 $result[] = Enchant::read($in);
60 }
61 return $result;
62 }
63
67 private static function writeEnchantList(PacketSerializer $out, array $list) : void{
68 $out->putUnsignedVarInt(count($list));
69 foreach($list as $item){
70 $item->write($out);
71 }
72 }
73
74 public static function read(PacketSerializer $in) : self{
75 $cost = $in->getUnsignedVarInt();
76
77 $slotFlags = $in->getLInt();
78 $equipActivatedEnchants = self::readEnchantList($in);
79 $heldActivatedEnchants = self::readEnchantList($in);
80 $selfActivatedEnchants = self::readEnchantList($in);
81
82 $name = $in->getString();
83 $optionId = $in->readRecipeNetId();
84
85 return new self($cost, $slotFlags, $equipActivatedEnchants, $heldActivatedEnchants, $selfActivatedEnchants, $name, $optionId);
86 }
87
88 public function write(PacketSerializer $out) : void{
89 $out->putUnsignedVarInt($this->cost);
90
91 $out->putLInt($this->slotFlags);
92 self::writeEnchantList($out, $this->equipActivatedEnchantments);
93 self::writeEnchantList($out, $this->heldActivatedEnchantments);
94 self::writeEnchantList($out, $this->selfActivatedEnchantments);
95
96 $out->putString($this->name);
97 $out->writeRecipeNetId($this->optionId);
98 }
99}
__construct(private int $cost, private int $slotFlags, private array $equipActivatedEnchantments, private array $heldActivatedEnchantments, private array $selfActivatedEnchantments, private string $name, private int $optionId)