forked from pmmp/PocketMine-MP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttribute.php
181 lines (153 loc) · 5.21 KB
/
Attribute.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\entity;
use function max;
use function min;
class Attribute{
public const MC_PREFIX = "minecraft:";
public const ABSORPTION = self::MC_PREFIX . "absorption";
public const SATURATION = self::MC_PREFIX . "player.saturation";
public const EXHAUSTION = self::MC_PREFIX . "player.exhaustion";
public const KNOCKBACK_RESISTANCE = self::MC_PREFIX . "knockback_resistance";
public const HEALTH = self::MC_PREFIX . "health";
public const MOVEMENT_SPEED = self::MC_PREFIX . "movement";
public const FOLLOW_RANGE = self::MC_PREFIX . "follow_range";
public const HUNGER = self::MC_PREFIX . "player.hunger";
public const FOOD = self::HUNGER;
public const ATTACK_DAMAGE = self::MC_PREFIX . "attack_damage";
public const EXPERIENCE_LEVEL = self::MC_PREFIX . "player.level";
public const EXPERIENCE = self::MC_PREFIX . "player.experience";
public const UNDERWATER_MOVEMENT = self::MC_PREFIX . "underwater_movement";
public const LUCK = self::MC_PREFIX . "luck";
public const FALL_DAMAGE = self::MC_PREFIX . "fall_damage";
public const HORSE_JUMP_STRENGTH = self::MC_PREFIX . "horse.jump_strength";
public const ZOMBIE_SPAWN_REINFORCEMENTS = self::MC_PREFIX . "zombie.spawn_reinforcements";
public const LAVA_MOVEMENT = self::MC_PREFIX . "lava_movement";
/** @var string */
protected $id;
/** @var float */
protected $minValue;
/** @var float */
protected $maxValue;
/** @var float */
protected $defaultValue;
/** @var float */
protected $currentValue;
/** @var bool */
protected $shouldSend;
/** @var bool */
protected $desynchronized = true;
public function __construct(string $id, float $minValue, float $maxValue, float $defaultValue, bool $shouldSend = true){
if($minValue > $maxValue || $defaultValue > $maxValue || $defaultValue < $minValue){
throw new \InvalidArgumentException("Invalid ranges: min value: $minValue, max value: $maxValue, $defaultValue: $defaultValue");
}
$this->id = $id;
$this->minValue = $minValue;
$this->maxValue = $maxValue;
$this->defaultValue = $defaultValue;
$this->shouldSend = $shouldSend;
$this->currentValue = $this->defaultValue;
}
public function getMinValue() : float{
return $this->minValue;
}
/**
* @return $this
*/
public function setMinValue(float $minValue){
if($minValue > ($max = $this->getMaxValue())){
throw new \InvalidArgumentException("Minimum $minValue is greater than the maximum $max");
}
if($this->minValue != $minValue){
$this->desynchronized = true;
$this->minValue = $minValue;
}
return $this;
}
public function getMaxValue() : float{
return $this->maxValue;
}
/**
* @return $this
*/
public function setMaxValue(float $maxValue){
if($maxValue < ($min = $this->getMinValue())){
throw new \InvalidArgumentException("Maximum $maxValue is less than the minimum $min");
}
if($this->maxValue != $maxValue){
$this->desynchronized = true;
$this->maxValue = $maxValue;
}
return $this;
}
public function getDefaultValue() : float{
return $this->defaultValue;
}
/**
* @return $this
*/
public function setDefaultValue(float $defaultValue){
if($defaultValue > $this->getMaxValue() || $defaultValue < $this->getMinValue()){
throw new \InvalidArgumentException("Default $defaultValue is outside the range " . $this->getMinValue() . " - " . $this->getMaxValue());
}
if($this->defaultValue !== $defaultValue){
$this->desynchronized = true;
$this->defaultValue = $defaultValue;
}
return $this;
}
public function resetToDefault() : void{
$this->setValue($this->getDefaultValue(), true);
}
public function getValue() : float{
return $this->currentValue;
}
/**
* @return $this
*/
public function setValue(float $value, bool $fit = false, bool $forceSend = false){
if($value > $this->getMaxValue() || $value < $this->getMinValue()){
if(!$fit){
throw new \InvalidArgumentException("Value $value is outside the range " . $this->getMinValue() . " - " . $this->getMaxValue());
}
$value = min(max($value, $this->getMinValue()), $this->getMaxValue());
}
if($this->currentValue != $value){
$this->desynchronized = true;
$this->currentValue = $value;
}elseif($forceSend){
$this->desynchronized = true;
}
return $this;
}
public function getId() : string{
return $this->id;
}
public function isSyncable() : bool{
return $this->shouldSend;
}
public function isDesynchronized() : bool{
return $this->shouldSend && $this->desynchronized;
}
public function markSynchronized(bool $synced = true) : void{
$this->desynchronized = !$synced;
}
}