Drop useless numeric IDs for attributes, use strings instead

This commit is contained in:
Dylan K. Taylor 2018-10-19 13:43:01 +01:00
parent 20b87b7875
commit d88b32da91
5 changed files with 46 additions and 67 deletions

View File

@ -24,26 +24,26 @@ declare(strict_types=1);
namespace pocketmine\entity; namespace pocketmine\entity;
class Attribute{ class Attribute{
public const MC_PREFIX = "minecraft:";
public const ABSORPTION = 0; public const ABSORPTION = self::MC_PREFIX . "absorption";
public const SATURATION = 1; public const SATURATION = self::MC_PREFIX . "player.saturation";
public const EXHAUSTION = 2; public const EXHAUSTION = self::MC_PREFIX . "player.exhaustion";
public const KNOCKBACK_RESISTANCE = 3; public const KNOCKBACK_RESISTANCE = self::MC_PREFIX . "knockback_resistance";
public const HEALTH = 4; public const HEALTH = self::MC_PREFIX . "health";
public const MOVEMENT_SPEED = 5; public const MOVEMENT_SPEED = self::MC_PREFIX . "movement";
public const FOLLOW_RANGE = 6; public const FOLLOW_RANGE = self::MC_PREFIX . "follow_range";
public const HUNGER = 7; public const HUNGER = self::MC_PREFIX . "player.hunger";
public const FOOD = 7; public const FOOD = self::HUNGER;
public const ATTACK_DAMAGE = 8; public const ATTACK_DAMAGE = self::MC_PREFIX . "attack_damage";
public const EXPERIENCE_LEVEL = 9; public const EXPERIENCE_LEVEL = self::MC_PREFIX . "player.level";
public const EXPERIENCE = 10; public const EXPERIENCE = self::MC_PREFIX . "player.experience";
private $id; protected $id;
protected $minValue; protected $minValue;
protected $maxValue; protected $maxValue;
protected $defaultValue; protected $defaultValue;
protected $currentValue; protected $currentValue;
protected $name;
protected $shouldSend; protected $shouldSend;
protected $desynchronized = true; protected $desynchronized = true;
@ -52,24 +52,23 @@ class Attribute{
protected static $attributes = []; protected static $attributes = [];
public static function init() : void{ public static function init() : void{
self::addAttribute(self::ABSORPTION, "minecraft:absorption", 0.00, 340282346638528859811704183484516925440.00, 0.00); self::addAttribute(self::ABSORPTION, 0.00, 340282346638528859811704183484516925440.00, 0.00);
self::addAttribute(self::SATURATION, "minecraft:player.saturation", 0.00, 20.00, 20.00); self::addAttribute(self::SATURATION, 0.00, 20.00, 20.00);
self::addAttribute(self::EXHAUSTION, "minecraft:player.exhaustion", 0.00, 5.00, 0.0); self::addAttribute(self::EXHAUSTION, 0.00, 5.00, 0.0);
self::addAttribute(self::KNOCKBACK_RESISTANCE, "minecraft:knockback_resistance", 0.00, 1.00, 0.00); self::addAttribute(self::KNOCKBACK_RESISTANCE, 0.00, 1.00, 0.00);
self::addAttribute(self::HEALTH, "minecraft:health", 0.00, 20.00, 20.00); self::addAttribute(self::HEALTH, 0.00, 20.00, 20.00);
self::addAttribute(self::MOVEMENT_SPEED, "minecraft:movement", 0.00, 340282346638528859811704183484516925440.00, 0.10); self::addAttribute(self::MOVEMENT_SPEED, 0.00, 340282346638528859811704183484516925440.00, 0.10);
self::addAttribute(self::FOLLOW_RANGE, "minecraft:follow_range", 0.00, 2048.00, 16.00, false); self::addAttribute(self::FOLLOW_RANGE, 0.00, 2048.00, 16.00, false);
self::addAttribute(self::HUNGER, "minecraft:player.hunger", 0.00, 20.00, 20.00); self::addAttribute(self::HUNGER, 0.00, 20.00, 20.00);
self::addAttribute(self::ATTACK_DAMAGE, "minecraft:attack_damage", 0.00, 340282346638528859811704183484516925440.00, 1.00, false); self::addAttribute(self::ATTACK_DAMAGE, 0.00, 340282346638528859811704183484516925440.00, 1.00, false);
self::addAttribute(self::EXPERIENCE_LEVEL, "minecraft:player.level", 0.00, 24791.00, 0.00); self::addAttribute(self::EXPERIENCE_LEVEL, 0.00, 24791.00, 0.00);
self::addAttribute(self::EXPERIENCE, "minecraft:player.experience", 0.00, 1.00, 0.00); self::addAttribute(self::EXPERIENCE, 0.00, 1.00, 0.00);
//TODO: minecraft:luck (for fishing?) //TODO: minecraft:luck (for fishing?)
//TODO: minecraft:fall_damage //TODO: minecraft:fall_damage
} }
/** /**
* @param int $id * @param string $id
* @param string $name
* @param float $minValue * @param float $minValue
* @param float $maxValue * @param float $maxValue
* @param float $defaultValue * @param float $defaultValue
@ -79,41 +78,25 @@ class Attribute{
* *
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public static function addAttribute(int $id, string $name, float $minValue, float $maxValue, float $defaultValue, bool $shouldSend = true) : Attribute{ public static function addAttribute(string $id, float $minValue, float $maxValue, float $defaultValue, bool $shouldSend = true) : Attribute{
if($minValue > $maxValue or $defaultValue > $maxValue or $defaultValue < $minValue){ if($minValue > $maxValue or $defaultValue > $maxValue or $defaultValue < $minValue){
throw new \InvalidArgumentException("Invalid ranges: min value: $minValue, max value: $maxValue, $defaultValue: $defaultValue"); throw new \InvalidArgumentException("Invalid ranges: min value: $minValue, max value: $maxValue, $defaultValue: $defaultValue");
} }
return self::$attributes[$id] = new Attribute($id, $name, $minValue, $maxValue, $defaultValue, $shouldSend); return self::$attributes[$id] = new Attribute($id, $minValue, $maxValue, $defaultValue, $shouldSend);
} }
/** /**
* @param int $id * @param string $id
* *
* @return Attribute|null * @return Attribute|null
*/ */
public static function getAttribute(int $id) : ?Attribute{ public static function getAttribute(string $id) : ?Attribute{
return isset(self::$attributes[$id]) ? clone self::$attributes[$id] : null; return isset(self::$attributes[$id]) ? clone self::$attributes[$id] : null;
} }
/** private function __construct(string $id, float $minValue, float $maxValue, float $defaultValue, bool $shouldSend = true){
* @param string $name
*
* @return Attribute|null
*/
public static function getAttributeByName(string $name) : ?Attribute{
foreach(self::$attributes as $a){
if($a->getName() === $name){
return clone $a;
}
}
return null;
}
private function __construct(int $id, string $name, float $minValue, float $maxValue, float $defaultValue, bool $shouldSend = true){
$this->id = $id; $this->id = $id;
$this->name = $name;
$this->minValue = $minValue; $this->minValue = $minValue;
$this->maxValue = $maxValue; $this->maxValue = $maxValue;
$this->defaultValue = $defaultValue; $this->defaultValue = $defaultValue;
@ -203,11 +186,7 @@ class Attribute{
return $this; return $this;
} }
public function getName() : string{ public function getId() : string{
return $this->name;
}
public function getId() : int{
return $this->id; return $this->id;
} }

View File

@ -32,11 +32,11 @@ class AttributeMap implements \ArrayAccess{
} }
/** /**
* @param int $id * @param string $id
* *
* @return Attribute|null * @return Attribute|null
*/ */
public function getAttribute(int $id) : ?Attribute{ public function getAttribute(string $id) : ?Attribute{
return $this->attributes[$id] ?? null; return $this->attributes[$id] ?? null;
} }
@ -61,7 +61,7 @@ class AttributeMap implements \ArrayAccess{
} }
/** /**
* @param int $offset * @param string $offset
* *
* @return float * @return float
*/ */
@ -70,7 +70,7 @@ class AttributeMap implements \ArrayAccess{
} }
/** /**
* @param int $offset * @param string $offset
* @param float $value * @param float $value
*/ */
public function offsetSet($offset, $value) : void{ public function offsetSet($offset, $value) : void{

View File

@ -236,9 +236,9 @@ class NetworkBinaryStream extends BinaryStream{
$max = $this->getLFloat(); $max = $this->getLFloat();
$current = $this->getLFloat(); $current = $this->getLFloat();
$default = $this->getLFloat(); $default = $this->getLFloat();
$name = $this->getString(); $id = $this->getString();
$attr = Attribute::getAttributeByName($name); $attr = Attribute::getAttribute($id);
if($attr !== null){ if($attr !== null){
$attr->setMinValue($min); $attr->setMinValue($min);
$attr->setMaxValue($max); $attr->setMaxValue($max);
@ -247,7 +247,7 @@ class NetworkBinaryStream extends BinaryStream{
$list[] = $attr; $list[] = $attr;
}else{ }else{
throw new \UnexpectedValueException("Unknown attribute type \"$name\""); throw new \UnexpectedValueException("Unknown attribute type \"$id\"");
} }
} }
@ -266,7 +266,7 @@ class NetworkBinaryStream extends BinaryStream{
$this->putLFloat($attribute->getMaxValue()); $this->putLFloat($attribute->getMaxValue());
$this->putLFloat($attribute->getValue()); $this->putLFloat($attribute->getValue());
$this->putLFloat($attribute->getDefaultValue()); $this->putLFloat($attribute->getDefaultValue());
$this->putString($attribute->getName()); $this->putString($attribute->getId());
} }
} }

View File

@ -69,11 +69,11 @@ class AddEntityPacket extends DataPacket{
$attrCount = $this->getUnsignedVarInt(); $attrCount = $this->getUnsignedVarInt();
for($i = 0; $i < $attrCount; ++$i){ for($i = 0; $i < $attrCount; ++$i){
$name = $this->getString(); $id = $this->getString();
$min = $this->getLFloat(); $min = $this->getLFloat();
$current = $this->getLFloat(); $current = $this->getLFloat();
$max = $this->getLFloat(); $max = $this->getLFloat();
$attr = Attribute::getAttributeByName($name); $attr = Attribute::getAttribute($id);
if($attr !== null){ if($attr !== null){
$attr->setMinValue($min); $attr->setMinValue($min);
@ -81,7 +81,7 @@ class AddEntityPacket extends DataPacket{
$attr->setValue($current); $attr->setValue($current);
$this->attributes[] = $attr; $this->attributes[] = $attr;
}else{ }else{
throw new \UnexpectedValueException("Unknown attribute type \"$name\""); throw new \UnexpectedValueException("Unknown attribute type \"$id\"");
} }
} }
@ -104,7 +104,7 @@ class AddEntityPacket extends DataPacket{
$this->putUnsignedVarInt(count($this->attributes)); $this->putUnsignedVarInt(count($this->attributes));
foreach($this->attributes as $attribute){ foreach($this->attributes as $attribute){
$this->putString($attribute->getName()); $this->putString($attribute->getId());
$this->putLFloat($attribute->getMinValue()); $this->putLFloat($attribute->getMinValue());
$this->putLFloat($attribute->getValue()); $this->putLFloat($attribute->getValue());
$this->putLFloat($attribute->getMaxValue()); $this->putLFloat($attribute->getMaxValue());

View File

@ -44,7 +44,7 @@ class UpdateAttributesPacket extends DataPacket{
protected function encodePayload() : void{ protected function encodePayload() : void{
$this->putEntityRuntimeId($this->entityRuntimeId); $this->putEntityRuntimeId($this->entityRuntimeId);
$this->putAttributeList(...$this->entries); $this->putAttributeList(...array_values($this->entries));
} }
public function handle(SessionHandler $handler) : bool{ public function handle(SessionHandler $handler) : bool{