PocketMine-MP 5.17.1 git-df4ada81e5d74a14046f27cf44a37dcee69d657e
src/entity/Attribute.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\entity;
25
26use function max;
27use function min;
28
30 public const MC_PREFIX = "minecraft:";
31
32 public const ABSORPTION = self::MC_PREFIX . "absorption";
33 public const SATURATION = self::MC_PREFIX . "player.saturation";
34 public const EXHAUSTION = self::MC_PREFIX . "player.exhaustion";
35 public const KNOCKBACK_RESISTANCE = self::MC_PREFIX . "knockback_resistance";
36 public const HEALTH = self::MC_PREFIX . "health";
37 public const MOVEMENT_SPEED = self::MC_PREFIX . "movement";
38 public const FOLLOW_RANGE = self::MC_PREFIX . "follow_range";
39 public const HUNGER = self::MC_PREFIX . "player.hunger";
40 public const FOOD = self::HUNGER;
41 public const ATTACK_DAMAGE = self::MC_PREFIX . "attack_damage";
42 public const EXPERIENCE_LEVEL = self::MC_PREFIX . "player.level";
43 public const EXPERIENCE = self::MC_PREFIX . "player.experience";
44 public const UNDERWATER_MOVEMENT = self::MC_PREFIX . "underwater_movement";
45 public const LUCK = self::MC_PREFIX . "luck";
46 public const FALL_DAMAGE = self::MC_PREFIX . "fall_damage";
47 public const HORSE_JUMP_STRENGTH = self::MC_PREFIX . "horse.jump_strength";
48 public const ZOMBIE_SPAWN_REINFORCEMENTS = self::MC_PREFIX . "zombie.spawn_reinforcements";
49 public const LAVA_MOVEMENT = self::MC_PREFIX . "lava_movement";
50
51 protected float $currentValue;
52 protected bool $desynchronized = true;
53
54 public function __construct(
55 protected string $id,
56 protected float $minValue,
57 protected float $maxValue,
58 protected float $defaultValue,
59 protected bool $shouldSend = true
60 ){
61 if($minValue > $maxValue || $defaultValue > $maxValue || $defaultValue < $minValue){
62 throw new \InvalidArgumentException("Invalid ranges: min value: $minValue, max value: $maxValue, $defaultValue: $defaultValue");
63 }
64 $this->currentValue = $this->defaultValue;
65 }
66
67 public function getMinValue() : float{
68 return $this->minValue;
69 }
70
74 public function setMinValue(float $minValue){
75 if($minValue > ($max = $this->getMaxValue())){
76 throw new \InvalidArgumentException("Minimum $minValue is greater than the maximum $max");
77 }
78
79 if($this->minValue != $minValue){
80 $this->desynchronized = true;
81 $this->minValue = $minValue;
82 }
83 return $this;
84 }
85
86 public function getMaxValue() : float{
87 return $this->maxValue;
88 }
89
93 public function setMaxValue(float $maxValue){
94 if($maxValue < ($min = $this->getMinValue())){
95 throw new \InvalidArgumentException("Maximum $maxValue is less than the minimum $min");
96 }
97
98 if($this->maxValue != $maxValue){
99 $this->desynchronized = true;
100 $this->maxValue = $maxValue;
101 }
102 return $this;
103 }
104
105 public function getDefaultValue() : float{
106 return $this->defaultValue;
107 }
108
112 public function setDefaultValue(float $defaultValue){
113 if($defaultValue > $this->getMaxValue() || $defaultValue < $this->getMinValue()){
114 throw new \InvalidArgumentException("Default $defaultValue is outside the range " . $this->getMinValue() . " - " . $this->getMaxValue());
115 }
116
117 if($this->defaultValue !== $defaultValue){
118 $this->desynchronized = true;
119 $this->defaultValue = $defaultValue;
120 }
121 return $this;
122 }
123
124 public function resetToDefault() : void{
125 $this->setValue($this->getDefaultValue(), true);
126 }
127
128 public function getValue() : float{
129 return $this->currentValue;
130 }
131
135 public function setValue(float $value, bool $fit = false, bool $forceSend = false){
136 if($value > $this->getMaxValue() || $value < $this->getMinValue()){
137 if(!$fit){
138 throw new \InvalidArgumentException("Value $value is outside the range " . $this->getMinValue() . " - " . $this->getMaxValue());
139 }
140 $value = min(max($value, $this->getMinValue()), $this->getMaxValue());
141 }
142
143 if($this->currentValue != $value){
144 $this->desynchronized = true;
145 $this->currentValue = $value;
146 }elseif($forceSend){
147 $this->desynchronized = true;
148 }
149
150 return $this;
151 }
152
153 public function getId() : string{
154 return $this->id;
155 }
156
157 public function isSyncable() : bool{
158 return $this->shouldSend;
159 }
160
161 public function isDesynchronized() : bool{
162 return $this->shouldSend && $this->desynchronized;
163 }
164
165 public function markSynchronized(bool $synced = true) : void{
166 $this->desynchronized = !$synced;
167 }
168}
setValue(float $value, bool $fit=false, bool $forceSend=false)
setDefaultValue(float $defaultValue)