PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
Armor.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\item;
25
36use function lcg_value;
37use function mt_rand;
38
39class Armor extends Durable{
40
41 public const TAG_CUSTOM_COLOR = "customColor"; //TAG_Int
42
43 private ArmorTypeInfo $armorInfo;
44
45 protected ?Color $customColor = null;
46
50 public function __construct(ItemIdentifier $identifier, string $name, ArmorTypeInfo $info, array $enchantmentTags = []){
51 parent::__construct($identifier, $name, $enchantmentTags);
52 $this->armorInfo = $info;
53 }
54
55 public function getMaxDurability() : int{
56 return $this->armorInfo->getMaxDurability();
57 }
58
59 public function getDefensePoints() : int{
60 return $this->armorInfo->getDefensePoints();
61 }
62
66 public function getArmorSlot() : int{
67 return $this->armorInfo->getArmorSlot();
68 }
69
70 public function getMaxStackSize() : int{
71 return 1;
72 }
73
74 public function isFireProof() : bool{
75 return $this->armorInfo->isFireProof();
76 }
77
78 public function getMaterial() : ArmorMaterial{
79 return $this->armorInfo->getMaterial();
80 }
81
82 public function getEnchantability() : int{
83 return $this->armorInfo->getMaterial()->getEnchantability();
84 }
85
89 public function getCustomColor() : ?Color{
90 return $this->customColor;
91 }
92
98 public function setCustomColor(Color $color) : self{
99 $this->customColor = $color;
100 return $this;
101 }
102
104 public function clearCustomColor() : self{
105 $this->customColor = null;
106 return $this;
107 }
108
114 $epf = 0;
115
116 foreach($this->getEnchantments() as $enchantment){
117 $type = $enchantment->getType();
118 if($type instanceof ProtectionEnchantment && $type->isApplicable($event)){
119 $epf += $type->getProtectionFactor($enchantment->getLevel());
120 }
121 }
122
123 return $epf;
124 }
125
126 protected function getUnbreakingDamageReduction(int $amount) : int{
127 if(($unbreakingLevel = $this->getEnchantmentLevel(VanillaEnchantments::UNBREAKING())) > 0){
128 $negated = 0;
129
130 $chance = 1 / ($unbreakingLevel + 1);
131 for($i = 0; $i < $amount; ++$i){
132 if(mt_rand(1, 100) > 60 && lcg_value() > $chance){ //unbreaking only applies to armor 40% of the time at best
133 $negated++;
134 }
135 }
136
137 return $negated;
138 }
139
140 return 0;
141 }
142
143 public function onClickAir(Player $player, Vector3 $directionVector, array &$returnedItems) : ItemUseResult{
144 $existing = $player->getArmorInventory()->getItem($this->getArmorSlot());
145 $thisCopy = clone $this;
146 $new = $thisCopy->pop();
147 $player->getArmorInventory()->setItem($this->getArmorSlot(), $new);
148 $player->getInventory()->setItemInHand($existing);
149 $sound = $new->getMaterial()->getEquipSound();
150 if($sound !== null){
151 $player->broadcastSound($sound);
152 }
153 if(!$thisCopy->isNull()){
154 //if the stack size was bigger than 1 (usually won't happen, but might be caused by plugins)
155 $returnedItems[] = $thisCopy;
156 }
157 return ItemUseResult::SUCCESS;
158 }
159
160 protected function deserializeCompoundTag(CompoundTag $tag) : void{
161 parent::deserializeCompoundTag($tag);
162 if(($colorTag = $tag->getTag(self::TAG_CUSTOM_COLOR)) instanceof IntTag){
163 $this->customColor = Color::fromARGB(Binary::unsignInt($colorTag->getValue()));
164 }else{
165 $this->customColor = null;
166 }
167 }
168
169 protected function serializeCompoundTag(CompoundTag $tag) : void{
170 parent::serializeCompoundTag($tag);
171 $this->customColor !== null ?
172 $tag->setInt(self::TAG_CUSTOM_COLOR, Binary::signInt($this->customColor->toARGB())) :
173 $tag->removeTag(self::TAG_CUSTOM_COLOR);
174 }
175}
deserializeCompoundTag(CompoundTag $tag)
Definition: Armor.php:160
getEnchantmentProtectionFactor(EntityDamageEvent $event)
Definition: Armor.php:113
__construct(ItemIdentifier $identifier, string $name, ArmorTypeInfo $info, array $enchantmentTags=[])
Definition: Armor.php:50
onClickAir(Player $player, Vector3 $directionVector, array &$returnedItems)
Definition: Armor.php:143
setCustomColor(Color $color)
Definition: Armor.php:98
broadcastSound(Sound $sound, ?array $targets=null)
Definition: Player.php:2499