PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
EntityDataHelper.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
33use function count;
34use function is_infinite;
35use function is_nan;
36
37final class EntityDataHelper{
38
39 private function __construct(){
40 //NOOP
41 }
42
46 private static function validateFloat(string $tagName, string $component, float $value) : void{
47 if(is_infinite($value)){
48 throw new SavedDataLoadingException("$component component of '$tagName' contains invalid infinite value");
49 }
50 if(is_nan($value)){
51 throw new SavedDataLoadingException("$component component of '$tagName' contains invalid NaN value");
52 }
53 }
54
58 public static function parseLocation(CompoundTag $nbt, World $world) : Location{
59 $pos = self::parseVec3($nbt, Entity::TAG_POS, false);
60
61 $generic = $nbt->getTag(Entity::TAG_ROTATION);
62 if(!($generic instanceof ListTag) || ($yawPitch = $generic->cast(FloatTag::class)) === null){
63 throw new SavedDataLoadingException("'" . Entity::TAG_ROTATION . "' should be a List<Float>");
64 }
65 $values = $yawPitch->getValue();
66 if(count($values) !== 2){
67 throw new SavedDataLoadingException("Expected exactly 2 entries for 'Rotation'");
68 }
69 self::validateFloat(Entity::TAG_ROTATION, "yaw", $values[0]->getValue());
70 self::validateFloat(Entity::TAG_ROTATION, "pitch", $values[1]->getValue());
71
72 return Location::fromObject($pos, $world, $values[0]->getValue(), $values[1]->getValue());
73 }
74
78 public static function parseVec3(CompoundTag $nbt, string $tagName, bool $optional) : Vector3{
79 $generic = $nbt->getTag($tagName);
80 if($generic === null && $optional){
81 return Vector3::zero();
82 }
83 if(!($generic instanceof ListTag) || ($pos = $generic->cast(DoubleTag::class) ?? $generic->cast(FloatTag::class)) === null){
84 throw new SavedDataLoadingException("'$tagName' should be a List<Double> or List<Float>");
85 }
86 $values = $pos->getValue();
87 if(count($values) !== 3){
88 throw new SavedDataLoadingException("Expected exactly 3 entries in '$tagName' tag");
89 }
90
91 $x = $values[0]->getValue();
92 $y = $values[1]->getValue();
93 $z = $values[2]->getValue();
94
95 self::validateFloat($tagName, "x", $x);
96 self::validateFloat($tagName, "y", $y);
97 self::validateFloat($tagName, "z", $z);
98
99 return new Vector3($x, $y, $z);
100 }
101}
static parseVec3(CompoundTag $nbt, string $tagName, bool $optional)
static parseLocation(CompoundTag $nbt, World $world)
static fromObject(Vector3 $pos, ?World $world, float $yaw=0.0, float $pitch=0.0)
Definition Location.php:44